Docker is a popular containerization tool for providing software applications with a filesystem containing everything they require to run. Because its run-time environment is ruthlessly consistent, using Docker containers ensures that the software behaves consistently regardless of where it is deployed.
In this tutorial, we’ll go over the differences between Docker images and Docker containers. Then, we’ll go over how to run, start, stop, and remove containers in greater detail.
Overview
A Docker image can be thought of as a dormant template used to create Docker containers. Images typically begin with a root filesystem and add filesystem changes and their corresponding execution parameters in layers that are ordered and read-only. Unlike a typical Linux distribution, a Docker image typically contains only the bare minimum required to run the application. The images have no state and do not change. Rather, they serve as the foundation for Docker containers.
The docker run command brings images to life by adding a read-write layer on top of the image to create a container. A union file system is a combination of read-only layers topped with a read-write layer. When you make a change to an existing file in a running container, it is copied from the read-only space into the read-write layer, where the changes are applied. The read-write layer version hides but does not remove the original file. Changes to the read-write layer are only visible within a single container instance. Any changes made to a container are lost when it is deleted unless steps are taken to preserve them.
Working with Containers
When you use the docker run command, a new container is created from the image you specify. This can be confusing, so let’s look at some examples:
Step 1: Creating Two Containers
The docker run command below will start a new container with the base Ubuntu image. -t creates a terminal, and -i allows us to interact with it. We’ll use bash, the default command in the Ubuntu base image’s Docker file, to get into a shell.
$ docker run -ti ubuntu
The command-line prompt changes to indicate that we are inside the container as the root user, followed by the container ID (12 characters).
root@11dc6548ceed:/#
We’ll make a change by echoing some text into the container’s /tmp directory, then using cat to ensure that it was saved successfully.
Docker containers terminate when the command they issued completes, so our container terminated when we exited the bash shell. We won’t see ours if we run rundocker ps, which displays running containers.
$ docker ps
Output: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
If we add the -a flag, which displays all containers, whether they are stopped or running, our container will appear on the list:
$ docker ps -a
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11dc6548ceed ubuntu “/bin/bash” 6 minutes ago Exited(127)8 seconds ago small_sinoussi
When the container was created, it was assigned a container ID and a name at random. In this case, the container ID is 11dc6548ceed, and the name is generated at random. ps -a displays these values, as well as the image from which the container was built (ubuntu), the time the container was created (six minutes ago), and the command executed in it (/bin/bash). The output also includes the container’s status (Exited) and how long the container has been in that state (6 seconds ago). If the container was still running, the status “Up” would be displayed, followed by the time it had been running.
When we run the same command again, a new container is created:
$ docker run -ti ubuntu
We can tell it’s a new container because the ID in the command prompt is different, and we won’t be able to find our Example1 file:
root@23ds98761e169:/# cat /tmp/Example1
Output:
cat: /tmp/Example1: No such file or directory
This can give the impression that the data has vanished, but this is not the case. We’ll exit the second container now to ensure that it and the file we created are both on the system.
root@23ds98761e169:/# exit
When we re-list the containers, both of them appear:
$ docker ps -a
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11dc6548ceed ubuntu “/bin/bash” About a minute ago Exited (1) 6 seconds ago kickass_borg
23ds98761e169 ubuntu “/bin/bash” 13 minutes ago Exited (127) 6 minutes ago small_sinoussi
Step 2: Restarting the First Container
We’ll use the start command with the -a flag to attach to it and the -i flag to make it interactive, followed by either the container ID or name. Make sure to replace the ID of your container in the following command:
$ docker start -ai 11dc6548ceed
We’re back at the container’s bash prompt, and when we cat the previously created file, it’s still there.
root@11dc6548ceed:/# cat /tmp/Example1.txt
Output:
Example1
We can exit the container now:
root@11dc6548ceed:/# exit
This output demonstrates that changes made within the container persist after stopping and restarting it. The content is only deleted when the container is removed. This example also shows that the modifications were limited to the individual container. When we started a second container, the image remained in its original state.
Step 3: Deleting Both Containers
We’ve made two containers and will end our quick tutorial by deleting them. The docker rm command, which only works on stopped containers, allows you to specify the name or ID of one or more containers, so we can delete both with:
$ docker rm 23ds98761e169 kickass_borg
Output:
23ds98761e169
kickass_borg
Both containers, as well as any changes we made inside them, are now gone.
Conclusion
We examined the docker run command in depth to see how it generates a new container each time it is executed. We’ve also seen how to locate, start, and connect to a stopped container.