Card 04/ 05

ExampleDifficulty: Beginner1 min

Images and Containers Example: One Image, Three Containers

Three names in the container list, one image underneath, and nothing shared between them.

Starting three containers from one image shows how the two relate in practice: one image (the packaged files and settings a program needs) can back any number of containers (running copies started from it), and each container is separate from the others. The image used here is nginx, a web server.

-d runs each container in the background (detached), and --name gives it a name to refer to it by. docker ps then lists the running containers, formatted here to show only each name and the image it came from:

bash
docker run -d --name one nginx
docker run -d --name two nginx
docker run -d --name three nginx

docker ps --format '{{.Names}}\t{{.Image}}'
one	nginx
two	nginx
three	nginx

There are three containers and one image name. Each container has its own writable layer (the private area where that container's file changes are stored), so a file written inside one does not exist in two, and removing three leaves the other two running. The image was downloaded once and is shared by all three, and nothing any of them does can change it.

So the image is the part you share and keep, and the container is the part you run and throw away. When a container misbehaves, the usual fix is to remove it and start a new one from the same image.