Docker Home

broken image


So, if you are new to Docker, you might wonder how to run a docker container. Let me quickly show you that.

Docker Homepage

You can create and run a container with the following command:

Docker homeseer

Docker Desktop for WSL 2 Windows Home is a full version of Docker Desktop for Linux container development. It comes with the same feature set as our existing Docker Desktop WSL 2 backend. Home page for Docker's documentation. Getting Started with Docker. Take a walkthrough that covers writing your first app, data storage, networking, and swarms, and ends with your app running on production servers in the cloud. Docker Hub is the world's easiest way to create, manage, and deliver your teams' container applications. Get Started Today for Free. Docker is a virtualization technology, similar to virtual machines. Instead of virtualizing an entire operating system, Docker allows you to virtualize an application into a container for maximum portability and flexibility.

And then, if you want to enter the container (to run commands inside the container interactively), you can use the docker exec command:

Here's an example where I create a new container with Ubuntu as the base image and then I enter the running Ubuntu container and run the ls command:

Docker Homebrew

Lost? Don't worry. I'll explain in detail what the above two commands do and what is the -it option in the docker run and exec command.

Docker is an application that makes it simple and easy to run application processes in a container, which are like virtual machines, only more portable, more resource-friendly, and more dependent on the host operating system.

How to run docker container

If you want to run a docker container with a certain image and a specified command, you can do it in this fashion:

The above command will create a new container with the specified name from the specified docker image. The container name is optional.

  • The -i option means that it will be interactive mode (you can enter commands to it)
  • The -t option gives you a terminal (so that you can use it as if you used ssh to enter the container).
  • The -d option (daemon mode) keeps the container running in the background.
  • bash is the command it runs.

The reason for running bash as command here is that the container won't stop immediately.

In the above example, I didn't name the container so it was randomly named determined_blackburn.

And as you can see, the container is running bash command in the background.

What happens if you don't run it as a daemon (-d option) in the background?

If you do not use the -d option, docker run will create a new container and you'll have a terminal in interactive mode running bash shell.

As you can see in the example below, the container is created and I am automatically inside the container (bash shell).

The problem here is that if you exit the container, the container stops.

What happens if you only use the -i (interactive) option?

Let's say you only use the -i option. In that case, you'll see an interactive prompt but if you exit the interactive prompt (using Ctrl+D or exit command), you exit the container and the container stops.

What happens if you only use the -t (terminal/tty) option?

That's a tricky situation because in that case, you'll have a pseudo terminal (if you use the -t option) but you won't have the STDIN. In other words, you cannot enter anything, cannot run command inside the container like you did earlier:

I had to stop the container from another terminal in the above case.

How to run an existing container

The docker run command creates a new container from the specified image. But what happens when you already have a container?

If you want to run an existing container, you must first start the container and then you can use the exec option like this:

This example will be better for your understanding:

Why use bash all the time?

In all the above examples, I have used bash or /bin/bash as the command that runs with the container. I used it because it gives a shell and when you run the container, thanks to the shell, you can run regular commands inside the container as if you are inside a regular Linux system.

You can ask the container to run any command but keep in mind that the container exists as soon as the command completes.

As you can see in the example below, there is no interactive terminal session this time. You are not ‘inside' the container anymore because the echo command finishes almost immediately.

You can in fact run other commands as well and enter the container afterwards.

In the example below, I created a new container that runs nginx server on port 80. Since I run it as a daemon with option -d, the nginx container keeps on running.

And then I use the docker exec command to get an interactive bash shell and thus enter inside the nginx container (which is basically a Linux preconfigured with nginx).

I hope you have a better understanding about how to run docker containers and why it uses certain options.

If you have questions or suggestions, do let me know in the comment section.

Docker Home

Docker Desktop for WSL 2 Windows Home is a full version of Docker Desktop for Linux container development. It comes with the same feature set as our existing Docker Desktop WSL 2 backend. Home page for Docker's documentation. Getting Started with Docker. Take a walkthrough that covers writing your first app, data storage, networking, and swarms, and ends with your app running on production servers in the cloud. Docker Hub is the world's easiest way to create, manage, and deliver your teams' container applications. Get Started Today for Free. Docker is a virtualization technology, similar to virtual machines. Instead of virtualizing an entire operating system, Docker allows you to virtualize an application into a container for maximum portability and flexibility.

And then, if you want to enter the container (to run commands inside the container interactively), you can use the docker exec command:

Here's an example where I create a new container with Ubuntu as the base image and then I enter the running Ubuntu container and run the ls command:

Docker Homebrew

Lost? Don't worry. I'll explain in detail what the above two commands do and what is the -it option in the docker run and exec command.

Docker is an application that makes it simple and easy to run application processes in a container, which are like virtual machines, only more portable, more resource-friendly, and more dependent on the host operating system.

How to run docker container

If you want to run a docker container with a certain image and a specified command, you can do it in this fashion:

The above command will create a new container with the specified name from the specified docker image. The container name is optional.

  • The -i option means that it will be interactive mode (you can enter commands to it)
  • The -t option gives you a terminal (so that you can use it as if you used ssh to enter the container).
  • The -d option (daemon mode) keeps the container running in the background.
  • bash is the command it runs.

The reason for running bash as command here is that the container won't stop immediately.

In the above example, I didn't name the container so it was randomly named determined_blackburn.

And as you can see, the container is running bash command in the background.

What happens if you don't run it as a daemon (-d option) in the background?

If you do not use the -d option, docker run will create a new container and you'll have a terminal in interactive mode running bash shell.

As you can see in the example below, the container is created and I am automatically inside the container (bash shell).

The problem here is that if you exit the container, the container stops.

What happens if you only use the -i (interactive) option?

Let's say you only use the -i option. In that case, you'll see an interactive prompt but if you exit the interactive prompt (using Ctrl+D or exit command), you exit the container and the container stops.

What happens if you only use the -t (terminal/tty) option?

That's a tricky situation because in that case, you'll have a pseudo terminal (if you use the -t option) but you won't have the STDIN. In other words, you cannot enter anything, cannot run command inside the container like you did earlier:

I had to stop the container from another terminal in the above case.

How to run an existing container

The docker run command creates a new container from the specified image. But what happens when you already have a container?

If you want to run an existing container, you must first start the container and then you can use the exec option like this:

This example will be better for your understanding:

Why use bash all the time?

In all the above examples, I have used bash or /bin/bash as the command that runs with the container. I used it because it gives a shell and when you run the container, thanks to the shell, you can run regular commands inside the container as if you are inside a regular Linux system.

You can ask the container to run any command but keep in mind that the container exists as soon as the command completes.

As you can see in the example below, there is no interactive terminal session this time. You are not ‘inside' the container anymore because the echo command finishes almost immediately.

You can in fact run other commands as well and enter the container afterwards.

In the example below, I created a new container that runs nginx server on port 80. Since I run it as a daemon with option -d, the nginx container keeps on running.

And then I use the docker exec command to get an interactive bash shell and thus enter inside the nginx container (which is basically a Linux preconfigured with nginx).

I hope you have a better understanding about how to run docker containers and why it uses certain options.

If you have questions or suggestions, do let me know in the comment section.

Become a Member for FREE
Become a member to get the regular Linux newsletter (2-4 times a month) and access member-only contents.

Join the conversation.





broken image