5.2 Creating and Running Docker Containers

You use the docker run command to run an application inside a container, for example:

# docker run -i -t --name guest oraclelinux:7-slim
bash-4.2# cat /etc/oracle-release
Oracle Linux Server release 7.7
bash-4.2# exit

This example runs an interactive bash shell using the Oracle Linux 7 image named oraclelinux:7-slim to provide the container. The /bin/bash command is the default command run for all oraclelinux base images. The -t and -i options allow you to use a pseudo-terminal to run the container interactively.

The following examples use the prompt [root@host ~] and [root@guest ~] (or similar) to represent the prompts shown by the host and by the container respectively. The actual prompt displayed by the container may be different.

The --name option specifies the name guest for the container instance.

Docker does not remove the container when it exits and we can restart it at a later time, for example:

[root@host ~]# docker start guest

If an image does not already exist on your system, the Docker Engine performs a docker pull operation to download the image from the Docker Hub (or from another repository that you specify) as shown in the following example:

[root@host ~]# docker run -i -t --rm container-registry.oracle.com/os/oraclelinux:7-slim
Unable to find image 'container-registry.oracle.com/os/oraclelinux:7-slim' locally
Trying to pull repository container-registry.oracle.com/os/oraclelinux ... 
7-slim: Pulling from container-registry.oracle.com/os/oraclelinux
Digest: sha256:267f37439471f1c5eae586394c85e743b887c7f97e4733e10e466158083c021e
Status: Downloaded newer image for container-registry.oracle.com/os/oraclelinux:7-slim
[root@guest /]# cat /etc/oracle-release 
Oracle Linux Server release 7.7
[root@guest /]# exit
[root@host ~]# 

Because we specified the --rm option instead of naming the container, Docker removes the container when it exits and we cannot restart it.

From another shell window, you can use the docker ps command to display information about the containers that are currently running, for example:

[root@host ~]# docker ps
68359521c0b7  oraclelinux:7-slim  "/bin/bash" 2 hours ago   Up 8 minutes         guest

The container named guest with the ID 68359521c0b7 is currently running the command /bin/bash. It is more convenient to manage a container by using its name than by its ID.

To display the processes that a container is running, use the docker top command:

[root@host ~]# docker top guest
UID    PID    PPID   C   STIME   TTY     TIME       CMD
root   31252  31235  0   05:59   pts/0   00:00:00   /bin/bash

You can use the docker exec command to run additional processes in a container that is already running, for example:

[root@host ~]# docker exec -i -t guest bash
[root@guest ~]#

You can also use the docker create command to set up a container that you can start at a later time, for example:

[root@host ~]# docker create -i -t --name newguest oraclelinux:7-slim
[root@host ~]# docker start -a -i newguest
[root@newguest ~]#

The -a and -i options to docker start attach the current shell's standard input, output, and error streams to those of the container and also cause all signals to be forwarded to the container.

You can exit a container by typing Ctrl-D or exit at the bash command prompt inside the container or by using the docker stop command:

[root@host ~]# docker stop guest

The -a option to docker ps displays all containers that are currently running or that have exited.

[root@host ~]# docker ps -a
b4c224f83e35  oraclelinux:7-slim ...      ...      Exited (0) About a minute ago     newguest
68359521c0b7  oraclelinux:7-slim ...      ...      Exited (137) 45 seconds ago       guest

You can use docker start to restart a stopped container. After reattaching to it, the contents remain unchanged from the last time that you used the container.

[root@host ~]# docker start -a -i guest
[root@guest ~]# touch /tmp/foobar
[root@guest ~]# exit
[root@host ~]# docker start -a -i guest
[root@guest ~]# ls -l /tmp/foobar
-rw-r--r-- 1 root root 0 Nov 26 06:27 /tmp/foobar

Because the container preserves any changes that you make to it, you can reconfigure files and install packages in the container without worrying that your changes will disappear.

You can use the docker logs command to watch what is happening inside a container, for example:

[root@host ~]# docker logs -f guest
bash-4.2# exit
bash-4.2# ls -l /tmp/foobar
-rw-r--r-- 1 root root 0 Nov 26 06:33 /tmp/foobar

The -f option causes the command to update its output as events happen in the container. Type Ctrl-C to exit the command.

You can obtain full information about a container in JSON format by using the docker inspect command. This command also allows you to retrieve specified elements of the configuration, for example:

[root@host ~]# docker inspect --format='{{ .State.Running }}' guest

If you need to remove a container permanently so that you can create a new container with the same name, use the docker rm command:

[root@host ~]# docker rm guest

If you specify the --rm option when you run a container, Docker removes the container when the container exits. You cannot combine the --rm option with the -d option.

Specifying the -f option to docker rm kills a running container before removing it. In previous versions, the same command stops the container before removing it. If you want to stop a container safely, use docker stop.