A Docker image is a standardized, read-only package of the files, binaries, libraries and configuration needed to run a container. Images are built from stacked layers, each one a set of file system changes, and a container is a running instance created from an image.
To see one on your machine, pull an image and list it. This uses the small tutorial image Docker publishes:
docker pull docker/welcome-to-docker
docker image ls
docker image history docker/welcome-to-docker
pull downloads the image, image ls lists the images stored locally, and image history prints the layers it is made of. Nothing runs yet. An image is something you start containers from, not something that runs by itself. For the bigger picture of what Docker is, see what is Docker.
What is inside a Docker image?
Docker's documentation describes an image as a package that includes all the files, binaries, libraries and configurations needed to run a container. It names two principles:
- Images are immutable. Once an image is created, it cannot be modified. If you want a change, you build a new image.
- Images are composed of layers. Each layer represents a set of file system changes that add, remove or modify files.
Those two rules let you extend an existing image instead of starting over. Layers let you reuse the base layers of other people's images, so your own image only adds what is different.
What are Docker image layers?
A layer records a change to the file system: files added, files modified, files deleted. Docker's layers guide gives a worked idea of how a Python app image might stack up, from bottom to top:
- Basic commands and a package manager such as apt.
- A Python runtime and pip.
- The application's
requirements.txtfile. - The application's dependencies.
- The application's source code.
Once a layer is created it is immutable. After a layer is downloaded, it is extracted into its own directory on the host. When a container starts, a union file system stacks those directories into a single view, and that view becomes the container's root. This is why several containers can run from the same underlying image.
The storage documentation adds that each file-system-changing instruction in a Dockerfile adds one layer to the image. You can write those instructions yourself: how to write a Dockerfile walks through it.
How to list the layers of an image
docker image history node-base
Docker's guide shows output of this shape (example output from the documentation, trimmed to two rows):
IMAGE CREATED CREATED BY SIZE COMMENT
9e274734bb25 10 seconds ago /bin/bash 157MB Add node
cd1dba651b30 7 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
Each row is a layer, newest on top. A row with 0B changed no files. In the first row, the 157 MB layer is the files added by installing Node.
Image vs container: what is the difference?
| Image | Container | |
|---|---|---|
| What it is | A read-only template with the files and settings | A runnable instance of an image |
| Can it change? | No, it is immutable | Yes, through its own writable layer |
| How many | One image | Many containers can share it |
| Made with | docker build or docker pull | docker run |
Starting a container adds a thin writable layer on top of the image's read-only layers. New, modified and deleted files from the running container all go into that top layer. Docker uses copy-on-write: a container reads files from the lower layers until it needs to change one, and only then is a copy made in its writable layer. Because of this, multiple containers can share access to the same image and still keep their own data state.
The practical result is that removing a container discards its writable layer. If the data must survive, store it in a volume or bind mount instead (see Docker volumes and bind mounts in the Docker docs). The Docker vs virtual machine article covers how this differs from a full VM disk.
How big is an image, and how big is a container?
docker image ls shows the uncompressed size of the image. Docker's documentation notes this is not the download size of the layers. For containers, docker ps -s reports two numbers:
- Size: the data used by the container's writable layer.
- Virtual size: the read-only image data plus the writable layer.
You cannot add up virtual sizes across containers to get disk use, because containers from the same or related images share layers.
What are image names, tags and digests?
You refer to an image as NAME[:TAG|@DIGEST], the syntax from the docker image pull reference.
| Form | Example | Behaviour |
|---|---|---|
| Name only | docker pull ubuntu | No tag given, so Docker Engine uses latest |
| Tag | docker pull ubuntu:24.04 | Pulls the image the tag points to. Pulling again gets the latest version of that tag. |
| Digest | docker pull ubuntu@sha256:<digest> | Pins to one exact image version |
Pulling a tag again can give you a newer image, so a tag is a moving label. A digest identifies one exact image, which the documentation says lets you pin an image and be sure it is always the same. The trade-off is also in the documentation: a pinned digest never receives updated versions, which may include security updates, until you change the digest yourself.
Images are stored and shared through registries. Docker Hub is the default one, described in the documentation as the default global marketplace for storing and distributing images. You can find images with docker search, for example docker search docker/welcome-to-docker.
How do you create a Docker image?
There are two ways. The common one is a Dockerfile, a text file of instructions that docker build turns into an image. The other, shown in Docker's layers guide, is to start a container, change it, and save the result as a new image:
docker container commit -m "Add node" base-container node-base
The same guide says most image builds do not use docker container commit. A Dockerfile automates those steps and is repeatable, so use it unless you are experimenting.
Why the order of Dockerfile instructions matters
Docker caches layers between builds. According to the build cache documentation, if a layer changes, all the layers after it are affected too and have to run again. A change early in the Dockerfile therefore rebuilds everything below it.
That rule follows directly from the stack described earlier. In the five-layer example, source code sits at the top because it changes most often. Putting the rarely changing parts, such as the runtime and dependencies, lower down means an edit to your code only rebuilds the top layer. Docker's build cache page links to a separate guide on optimizing the cache if you want specific techniques.
Common questions about images and disk space
- Deleting a file in a later layer does not shrink the image. A layer records a deletion as a change, and the earlier layer that added the file still exists in the stack. This follows from layers being immutable, and it is the reason to avoid adding large files you plan to remove later.
- Pulling a second image may download less than you expect. Layers shared with an image you already have are reused, which is why one image's size does not simply add to another's.
- Your Docker version may change what you see. Docker's storage page notes that Docker Engine 29.0 and later uses the containerd image store by default on fresh installs, so command output can differ from the examples above on those systems.
For the day-to-day commands around images, such as listing, pulling and removing them, see the Docker commands cheat sheet.
Frequently asked questions
- Is a Docker image the same as a container?
- No. An image is an immutable package of files and configuration. A container is a running instance created from an image, with its own thin writable layer on top. One image can back many containers, and removing a container does not remove the image.
- Can you change a Docker image after it is built?
- No. Docker's documentation says images are immutable: once created, an image cannot be modified. To change something you build a new image, which usually reuses the unchanged layers from the old one.
- Why does docker pull print several lines for one image?
- An image is made of layers, and Docker downloads each layer separately. Each line in the pull output represents one downloaded layer. Layers you already have locally from another image are not downloaded again.
- What does the latest tag mean on a Docker image?
- It is only the default tag. Docker's pull reference says that if you give no tag, Docker Engine uses latest. Nothing in that behaviour guarantees it is the newest version, so use an explicit version tag or a digest when you need to know exactly what you run.
- Where are Docker images stored?
- Locally, Docker keeps each downloaded layer on the host, managed by the storage driver or image store your installation uses. The exact location and format depend on that setup, so use docker image ls to list images rather than looking for files. Shared copies live in a registry such as Docker Hub.