Sub topic 1
Images and containers: what you are running
Prerequisites
This chapter assumes you can use a terminal and know what a process and an environment variable are. It also assumes you understand ports and IP addresses at a basic level, and that Docker Engine or Docker Desktop is installed. To check the installation, run docker version. Both a Client and a Server section should appear in the output.
The core distinction
An image is a read-only package. It holds a filesystem (for example, a minimal Linux userland plus nginx) and metadata such as the default command to run. An image never changes once it is built.
A container is a running or stopped instance created from an image. Docker adds a thin writable layer on top of the image's read-only layers, then starts a process inside an isolated environment. The container sees its own filesystem, process list, and network, but it shares the host's kernel.
You can create many containers from one image. Each has its own writable layer, so changes in one do not affect the others or the image.
What a container really is
A container is an ordinary process on the host, started with restrictions. Linux namespaces limit what the process can see, such as other processes, network interfaces, and mount points. Control groups (cgroups) limit what it can use, such as memory and CPU. Docker Desktop runs containers inside a small Linux virtual machine, but the model is the same.
Two consequences follow:
- A container lives as long as its main process. When that process exits, the container stops.
- A container is not a virtual machine. It does not boot its own kernel or run a full init system by default.
Image names and tags
An image reference has the form registry/repository:tag. In nginx:1.27, the repository is nginx and the tag is 1.27. If you omit the registry, Docker uses Docker Hub. If you omit the tag, Docker uses latest.
A tag is a movable label, not a guarantee. latest can point to a different image next month. Pin a specific tag, such as postgres:16, when you want repeatable results.
Seeing both in practice
The following commands show an image, then a container created from it.
docker pull nginx:1.27
docker images nginx
docker run -d --name web1 nginx:1.27
docker run -d --name web2 nginx:1.27
docker psdocker images lists the image once. docker ps lists two containers, web1 and web2, both based on that image. Now write a file in one container and look for it in the other:
docker exec web1 sh -c "echo hello > /tmp/note.txt"
docker exec web1 cat /tmp/note.txt
docker exec web2 cat /tmp/note.txtThe second command prints hello. The third fails with "No such file or directory", because the file exists only in the writable layer of web1.
docker rm -f web1 web2Removing the containers deletes their writable layers and the files in them. The image stays on disk, ready to start new containers.
Common mistakes
- Treating a container like a server you patch by hand. Change the image or the configuration instead, then start a new container.
- Assuming
latestmeans the newest version on your machine. Docker does not re-pull a tag it already has unless you rundocker pullor start the container with--pull always. - Expecting a container to stay up when its main process finishes. A container running
echo hiprints the text and exits immediately.
Exercise
You run docker run -d --name a nginx:1.27, create a file in container a, then run docker rm -f a and start a new container b from the same image. Does b contain the file? Why or why not?
Show answer
No. The file was stored in the writable layer of container a, and removing a deleted that layer. Container b starts from the unchanged read-only image with a new, empty writable layer.
Exercise
A container started from an image runs a single command, echo done, and then shows as exited. Which rule explains this?
Show answer
A container lives only as long as its main process. Once echo done finishes, the process exits and the container stops.