How to Run Docker Containers

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

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

docker run -it -d --name container_name image_name bash

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

docker exec -it container_ID_or_name /bin/bash

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:

abhishek@nuc:~$ docker run -it -d --name my_container ubuntu bash
7e77640bca686108ad415318278a1fa148e1c84d3b180276e19ec2b970ba9b67
abhishek@nuc:~$ docker exec -it my_container bash
root@7e77640bca68:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@7e77640bca68:/# 

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.

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:

docker run -it -d --name container_name image_name bash

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.

abhishek@nuc:~$ docker run -it -d ubuntu bash
82766613e7bc0ae98887bb1c776fa0de7f3c4771db9ac7ffc5db5c2c68ab497b
abhishek@nuc:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
82766613e7bc        ubuntu              "bash"              4 seconds ago       Up 3 seconds                            determined_blackburn

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.

Docker Run vs Start vs Create: Difference Explained
For a docker beginner, terms like docker start, docker run and docker create could be confusing. This article explains the difference with examples.

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.

abhishek@nuc:~$ docker run -it ubuntu bash
root@6098c44f2407:/# echo this is a new container
this is a new container
root@6098c44f2407:/# exit
exit
abhishek@nuc:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
82766613e7bc        ubuntu              "bash"              2 minutes ago       Up 2 minutes                            determined_blackburn
abhishek@nuc:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
6098c44f2407        ubuntu              "bash"              27 seconds ago      Exited (0) 8 seconds ago                       bold_benz
82766613e7bc        ubuntu              "bash"              2 minutes ago       Up 2 minutes                                   determined_blackburn

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.

abhishek@nuc:~$ docker run -i ubuntu bash
echo "this is interactive"
this is interactive
abhishek@nuc:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
abhishek@nuc:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
b6b79a9f7789        ubuntu              "bash"              27 seconds ago      Exited (0) 9 seconds ago                       loving_galileo

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:

abhishek@nuc:~$ docker run -t ubuntu bash
root@d30e20d5fed4:/# echo hello

^C^C
root@d30e20d5fed4:/# echo "hi"  
^C^C
root@d30e20d5fed4:/# exit
^C^C
root@d30e20d5fed4:/#

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

How to Exit a Docker Container
This quick little docker tip shows how to exit a docker container.

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:

docker start existing_container_ID_or_name
docker exec -it existing_container_ID_or_name /bin/bash

This example will be better for your understanding:

abhishek@nuc:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
b6b79a9f7789        ubuntu              "bash"              7 minutes ago       Exited (0) 7 minutes ago                       loving_galileo
abhishek@nuc:~$ docker start b6b79a9f7789
b6b79a9f7789
abhishek@nuc:~$ docker exec -it b6b79a9f7789 bash
root@b6b79a9f7789:/# echo "inside existing container"
inside existing container
root@b6b79a9f7789:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@b6b79a9f7789:/# exit
exit
abhishek@nuc:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
b6b79a9f7789        ubuntu              "bash"              8 minutes ago       Up 43 seconds                           loving_galileo

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.

abhishek@nuc:~$ docker exec -it b6b79a9f7789 echo hello
hello
abhishek@nuc:~$

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).

abhishek@nuc:~$ docker container run -it -d -p 8000:80 nginx
262ca5b17bf8aac5afbfdf66af6383be164cd8488f8cd64c06ee466b4d7462e9
abhishek@nuc:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
262ca5b17bf8        nginx               "nginx -g 'daemon of…"   9 seconds ago       Up 8 seconds        0.0.0.0:8000->80/tcp   zen_kilby
b6b79a9f7789        ubuntu              "bash"                   20 minutes ago      Up 12 minutes                              loving_galileo
abhishek@nuc:~$ docker exec -it 262ca5b17bf8 bash
root@262ca5b17bf8:/# ls
bin  boot  dev	etc  home  lib	lib64  media  mnt  opt	proc  root  run  sbin  srv  sys  tmp  usr  var
root@262ca5b17bf8:/# 

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.