Introduction
Docker is a popular containerization tool used to provide software applications with a file system that contains everything they need to run. Using Docker containers ensures that the software behaves the same no matter where it is deployed, because its runtime environment is ruthlessly consistent. In this tutorial, we will provide a brief overview of the relationship between Docker images and Docker containers. Then, we will take a closer look at how to run, start, stop, and remove containers.
Overview
We can think of a Docker image as a bare metal template used to create Docker containers. Images typically start with a root filesystem and add filesystem changes and their corresponding execution parameters in neat, read-only layers. Unlike a typical Linux distribution, a Docker image typically contains only the bare metal necessary to run the application. Images are stateless and do not change. Instead, they form the starting point for Docker containers. Images are brought to life with the docker run command, which creates a container by adding a read-write layer on top of the image. This combination of read-only layers placed on top of that read-write layer is known as a union filesystem. When a change is made to a file in a running container, the file is copied from the read-only space to the read-write layer, where the changes are applied. The version in the read-write layer hides the original file but does not delete it. Changes in the read-write layer only exist in a separate container instance. When a container is deleted, any changes are lost unless steps are taken to preserve them.
How to work with Docker containers
- Create two Docker containers
- Restart the first container.
- Delete both containers.
Working with containers
Every time you use the command docker run When you use it, it creates a new container from the image you specified. This can be a source of confusion, so let's take a look at some examples:
Step 1: Create two containers
Execution command docker The following creates a new container using the base Ubuntu image. -t It gives us a terminal and -i It allows us to interact with it. We follow the default command in the Docker file of the Ubuntu base image, bashWe rely on it to leave us in a shell.
docker run -ti ubuntuThe command line will change to show that we are inside the container as the root user, followed by the 12-character container ID.
root@11cc47339ee1:/#We made a change with text reflection in the folder /tmp We create a container, then use cat We use it to confirm that it was saved successfully.
echo "Example1" > /tmp/Example1.txt
cat /tmp/Example1.txtOutput
Example1
Now let's get out of the container.
exitDocker containers don't run as soon as the command they issue completes, so our container was stopped when we exited the bash shell. If the command p.s. If we run the command to show running containers, we will not see our command.
docker psOutput
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESIf the flag -a Add the command that shows all containers, stopped or running, and our container will appear in the list:
docker ps -aOutput
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
11cc47339ee1 ubuntu "/bin/bash" 6 minutes ago Exited (127) 8 seconds ago small_sinoussiWhen the container was created, it was given a container ID and a randomly generated name. In this case, 11cc47339ee1 Container ID and small_sinoussi It is a randomly generated name. ps -a Those values, as well as the image the container was built from (ubuntu), when the container was created (six minutes ago), and the command it ran in (/bin/bash). The output also provides the container's state (Exited) and the time the container has been in that state (6 seconds ago). If the container was still running, we would see a status of "up", followed by the time it had been running.
If we run the same command again, a completely new container will be created:
docker run -ti ubuntuWe can tell it's a new container because the identifier is different on the command line, and when we look for our Example1 file, we don't find it:
cat /tmp/Example1Output
cat: /tmp/Example1: No such file or directoryThis may seem like the data has disappeared, but it hasn't. Now we'll exit the second container to see that it and our first container with the file we created are both still on the system.
exitWhen we list the containers again, both appear:
docker ps -aOutput
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e4341887b69 ubuntu "/bin/bash" About a minute ago Exited (1) 6 seconds ago kickass_borg
11cc47339ee1 ubuntu "/bin/bash" 13 minutes ago Exited (127) 6 minutes ago small_sinoussiStep 2: Restart the first container
To restart an existing container, we use the start command with the -a flag to connect to it and the -i flag to make it interactive, followed by the container ID or name. Be sure to substitute your container ID in the following command:
docker start -ai 11cc47339ee1We find ourselves once again at the container bash prompt and when we cat the file we created earlier, it is still there.
cat /tmp/Example1.txtOutput
Example1Now we can exit the container:
exitThis output shows that changes made inside the container persist through stopping and starting it. Only when the container is removed is the content removed. This example also shows that the changes were limited to the individual container. When we started the second container, it reflected the original state of the image.
Step 3: Delete both containers
We have created two containers and we will finish our short tutorial by removing 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 remove both with the following:
docker rm 11cc47339ee1 kickass_borgOutput
11cc47339ee1
kickass_borgBoth containers and any changes we made inside them are now gone.
Result
We take a close look at the recipe. docker run We've seen how it automatically creates a new container each time it runs. We've also seen how to find a stopped container, start it, and connect to it. If you want to learn more about container management, you might be interested in Docker Container Naming Guide: 3 Tips for Beginners.









