The Short Answer
An image is to a container what a program file is to a running process. The image is built once and does not change. A container is created from it, gets a name and an ID, runs its processes, and can be stopped, started again, and removed. The image stays the same through all of that. Docker as a whole is introduced in what is Docker.
What an Image Contains
- Layers: read-only file system changes, one per build step, stacked on top of each other. Images built from the same base share those layers on disk.
- Configuration: the default command, environment variables, working folder, exposed ports, and labels.
- Names: one or more tags such as
nginx:alpine, and a digest that identifies exactly one build.
You make an image with a Dockerfile and docker build, or download one with docker pull.
What a Container Adds
- A writable layer: every file the container creates or changes is stored here, on top of the image's layers.
- Runtime settings: name, published ports, volumes, networks, environment overrides, restart policy, and resource limits, all given at
docker run. - A state: created, running, paused, restarting, exited, or dead.
- Processes: while running, the main process and its children, isolated on the host's kernel.
Side by Side
| Image | Container | |
|---|---|---|
| Changes | Never | Writable layer |
| Made by | Build or pull | docker run |
| Runs code | No | Yes, when started |
| Named by | Tag and digest | Name and ID |
| Listed by | docker images | docker ps -a |
| Removed by | docker rmi | docker rm |
The listing commands are covered in docker images and docker ps.
Many Containers from One Image
docker run -d --name web1 -p 8081:80 nginx:alpine
docker run -d --name web2 -p 8082:80 nginx:alpine
docker run -d --name web3 -p 8083:80 nginx:alpine
Three containers, one image on disk. Each has its own writable layer, so a file changed in web1 does not appear in the others. Unless forced, Docker refuses to remove an image while any container made from it exists, even a stopped one.
What Happens to Data
The writable layer belongs to the container. docker stop keeps it; docker rm deletes it. Anything the application wrote there, such as uploads or a database, is gone with the container. Data that must outlive a container goes into a volume or a bind mount, as explained in Docker volumes. See what a container has changed with:
docker diff web1
Removing containers safely is covered in Docker remove container.
Why Not docker commit?
docker commit turns a container's writable layer into a new image. It works, but the result cannot be rebuilt: nobody knows which commands produced it, and it carries along logs, caches, and shell history. Use it at most to save a container for inspection. For anything you run, write the steps in a Dockerfile and build the image again whenever something changes.
Tags, Digests, and Updates
A tag such as nginx:alpine is a movable name; a digest names one exact build. A running container keeps the image it was started from, even after the tag moves on. To update, pull or build the new image, then remove the container and create a new one from it, with its volumes reattached. The safe order is in how to update a Docker container, and naming images is covered in docker tag.