How To Run A Docker Container

Sarath Pillai's picture
Running a Docker Container

Before going ahead with this tutorial, I would recommend to read the below two tutorials to understand containers and Docker installation process. This article assumes that you have docker installed on your system and is waiting to accept commands.

 

Read: Difference between Hypervisor and Container Virtualization

Read: How To Install Docker

 

In this tutorial we will see different methods that can be used to get started with our initial containers. We will be concentrating on different Docker command line usage to run containers on your Docker workstation.

So the first step, before we get started is to verify that you have Docker daemon working correctly, this can be done by running the below command.

root@docker-workstation:~$ docker info
Containers: 17

Images: 201
Storage Driver: devicemapper
 Pool Name: docker-202:1-524942-pool
 Pool Blocksize: 65.54 kB
 Data file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata
 Data Space Used: 41.02 GB
 Data Space Total: 107.4 GB
 Metadata Space Used: 30.24 MB
 Metadata Space Total: 2.147 GB
 Library Version: 1.02.82-git (2013-10-04)
Execution Driver: native-0.2
Kernel Version: 3.13.0-36-generic
Operating System: Ubuntu 14.04.1 LTS

In the above example, we have used info command to check the status. The command output will list all containers, images, storage drivers, kernel details etc.

 

“Please remember the fact that Docker works in a client server model. And the same Docker binary is used for server as well as client side commands. So when we execute the Docker client side commands, what actually happens is it passes the commands to the Docker daemon running in the system”

 

So let’s get started with our first container. We will be using run command to run our container. The Docker run command has a wide range of options that can be passed, which pretty much includes all capabilities of Docker.

 

#docker help or man docker-run will show you the entire list of command line arguments

 

root@docker-workstation:~$ docker run -i -t ubuntu /bin/bash

 

The above command will pull the Ubuntu image (well the latest tag from the Ubuntu repository...we will be seeing what are Tags later), and run that image as a container, and then will start a bash shell inside that container.

Let's see the command line options that we used with the above docker run command. The -i and -t combined, creates the bash shell terminal for us inside the newly launched container. -i asks to keep the input open and -t asks the container to assign a terminal to it(which is why we have the bash shell opened after the run command completes).

Now you can check the containers that are currently in running state in your system using docker ps command as shown below.

 

spillai@docker-workstation:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 

 

Containers can be uniquely identified by using their id's. Each container on your system will have a unique id number. You can see the container id's by running #docker ps or #docker ps -a (giving -a switch, will show you all the containers, it will even show you the containers that are stopped).

 

Actually there are three different ways using which you can identify a container uniquely. The first is the short uuid (which you can get by running #docker ps.) Also there is a longer uuid, and then docker can also be identified using their names (if you do not assign a name to your container while running it, it will get a random name automatically)

 

How to name a Docker Container?

 

You can name a docker container while running it for the first time. If a name is not provided by the user while creating a container for the first time, Docker will assign a random name to the container. Names like drunk_fermat, sad_yonath, nostalgic_kirch, cocky_stallman, distracted_torvalds are all randomly assigned names by Docker.

 

If we want to assign a particular name of our interest to a container instead of the randomly generated one, we can do that by using the --name flag while running the container as shown below.

 

root@docker-workstation:~$ docker run --name my_first_ubuntu -i -t ubuntu /bin/bash

 

 

The above command will create a container with the name my_first_ubuntu. The good thing about container names is that, you can use the name instead of id's with docker command's, to perform different actions on the container. Container names are useful, while creating connections between two containers(we will discuss linking containers later).

 

How to start/stop a Docker container?

You can start/stop a container by using its id.

 

root@docker-workstation:~$ docker start b87d675c0a4b

 

The above command will start the container with the id b87d675c0a4b. You can stop the same container using the below command.

root@docker-workstation:~$ docker stop b87d675c0a4b

 

As i told earlier, if your container is named, you can use the same name instead of id's while starting/stopping a container (simply replace the id with the container name in the above command)

 

You can attach a terminal to a container that is running using the below command.

 

root@docker-workstation:~$docker attach cebf8a62c307

 

You can use the container name instead of the id in the above command as well. Please keep in mind that the above command requires a return key after the execution, to bring up the bash shell inside the container.

 

How to create a Daemonized Container?

Till now whatever we have seen are all interactive containers. For practical use cases, we need longer running containers that can be daemonized. Please note the fact that they will not be having an interactive session as we have used till now.
 

root@docker-workstation:~$ sudo docker run --name my_daemonized -d ubuntu /bin/sh -c "while true; do echo my daemonized container; sleep 1; done "
7b487f35905f70db6e4b78d0bc60aa21daafc813e2be214f2d2bbc8a2079815c
root@docker-workstation:~$ sudo docker ps
CONTAINER ID        IMAGE                                                  COMMAND                CREATED             STATUS              PORTS               NAMES
7b487f35905f        ubuntu:14.04   "/bin/sh -c 'while t   5 seconds ago       Up 4 seconds                            my_daemonized

 

The -d flag we used while creating the container above, will make it run in the background. Also we have used a simple while loop as a container command. The main difference to note here is the fact that, after the execution of the run command, docker just gave us the container id (7b487f35905f70db6e4b78d0bc60aa21daafc813e2be214f2d2bbc8a2079815c) and returned the prompt to us. Unlike earlier, if we now run #docker ps command, it will show us our newly created daemonized container in running status.

 

root@docker-workstation:~$ docker logs my_daemonized
my daemonized container
my daemonized container
my daemonized container
...

You can see what's happening inside the container console by running the #docker logs command(Please note the fact that we have used the container name instead of id in the above command). The above command will actually show you the last few entries and return the prompt. But you can use the -f option (very much similar to tail -f to see continues console entries happening in the container from outside, as shown below.)

 

root@docker-workstation:~$ docker logs -f  my_daemonized

 

 

You can see the processes that are running inside the docker container from outside as shown below.

root@docker-workstation:~$ docker top  my_daemonized
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                16797               29861               0                   09:36               ?                   00:00:00            /bin/sh -c while true; do echo my daemonized container; sleep 1; done
root                17785               16797               0                   09:50               ?                   00:00:00            sleep 1

 

 

How to run a process inside the Docker Container from outside?

Executing process inside a running container from outside will be a common requirement. We can do that very easily using the exec flag available in docker command line.

There are basically two types of processes that we can run inside a running container. One if background and the other is interactive.

 

root@docker-workstation:~$ docker exec -d  my_daemonized mkdir /tmp/test

 

The above command will create a directory /tmp/test inside our container named my_daemonized. The -d flag makes our command to run in background. We can also run interactive tasks inside the container using the exec flag. One of the common interactive tasks will be opening a shell inside an already running container.

root@docker-workstation:~$ docker exec -i -t  my_daemonized /bin/bash   
root@7b487f35905f:/# exit

 

The above command will be the most commonly used command for practical use cases. Using the above command, you can get inside an already running daemonized container and execute your required actions, and exit the container using #exit command.

 

Automatically restarting a container on failure

 

By default docker does not restart a container if it gets stopped due to a failure. However you can configure your container to restart itself, in case of a failure.

This is done using the --restart flag in docker command line. The flag verifies the exit code of the container.

 

root@docker-workstation:~$ docker run --restart=always --name my_daemonized -d ubuntu /bin/sh -c "while true; do echo my daemonized container; sleep 1; done"

 

 

In the above docker run command, we have set restart flag to always, which means it will restart the container always, no matter what the exit code of the container is.

If you want to restart your container on exit codes other than zero, then this can be done by setting the restart flag to on-failure mode, as shown below.

 

spillai@docker-workstation:~$ sudo docker run --restart=on-failure:3 --name my_daemonized -d ubuntu /bin/sh -c "while true; do echo my daemonized container; sleep 1; done"

 

The above command will make the docker daemon to restart the container on non-zero exit codes. The no of tries will be 3 times. Please note the fact that this restart flag is not available in older versions of Docker.

 

Fetching more details about your Docker Container

You can get all the details of your container by using a command called as "inspect" with docker. The inspect command will query our required container and will return us with a detailed information about the container configuration.

You can run the inspect command as shown below.

 

root@docker-workstation:~$ docker inspect my_daemonized

 

Inspect command will give you a whole lot of details including the arguments that were used to create the container, the storage driver used, the hostname, networking details, container and image details, cpu and memory details to name a few. 

 

How to delete a Docker Container?

Deleting a container is as simple as starting it or stopping it. Its a single command operation as shown below.

 

root@docker-workstation:~$ docker stop 7b487f35905f
7b487f35905f
root@docker-workstation:~$ docker rm 7b487f35905f
7b487f35905f

 

 

In the above example, we have first stopped our container, and then requested to delete it from our system. You cannot delete a container, if its in running state.

 

How to Expose ports inside the container to outside?

 

You can expose ports inside the container to outside world, by attaching required port number on the base docker host to another port in the container. For example, if i want to expose port 8080 inside the container, then i will have to map that port to another port in the host system. This can be done using the below command while docker run.

 

root@docker-workstation:~$ docker run -p 8080:8080 -d glassfish

 

 

The above command will open port 8080 on the base system, which will same as connection to port 8080 inside the container. So basically when somebody connects to port 8080 on the docker base system, they will actually be making a connection to the container port 8080. 

You can expose multiple ports with multiple -p flag's.

Or else, you can actually attach the container to the host network directly. Using this technique, you can avoid creating a different namespace for the container's network. In simple words, network part will be same as the host itself.

 

root@docker-workstation:~$ docker run --net host -d -t 5ff9d35b02d4

 

 

The above command will create a container without name-spacing the network part. Hence ports and network actions will all happen on the host network itself. Its much easier to handle networking in this way.

 

How to Mount a volume inside a Docker Container?

You can mount a volume inside the docker container using -v flag. However you cannot mount a volume after the container is created. Mounting a local volume needs to be done during the container creation time as shown below.

 

root@docker-workstation:~$ docker run --net host -d -t -v /opt/mountpoint:/mountpoint:rw 4ff5d45b02d4

 

 

The above command will mount the directory /opt/mountpoint to /mountpoint directory inside the container. In simple words, anything written to /mountpoint directory inside the container is actually being written to /opt/mountpoint directory inside the Docker host.

Rate this article: 
Average: 4.1 (117 votes)

Comments

What is the right to build a interactive container which also has a daemon running automatically in the background?

I mean like, create a image which starts a daemon as an ENTYRPOINT and create a interactive container from that image which starts a bash, for example.

Thanks a lot :)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.