What is the difference between a Docker image and a container?
Model answer
An image is a read-only template that holds a filesystem, the application and its dependencies, plus metadata such as the default command. A container is a running, isolated instance of an image.
A common comparison is a class and an object: one image can start many containers.
An image is built from layers, each stacked on the previous one and shared between images that use the same base. When Docker starts a container, it adds a thin writable layer on top. Files the container creates or changes go there, and they disappear when the container is removed. The image itself never changes.
docker build -t myapp:1.0 . # produce an image
docker run -d --name a myapp:1.0 # first container
docker run -d --name b myapp:1.0 # second container, same image
docker images # list images
docker ps # list running containers
Isolation comes from Linux namespaces and cgroups, and the container shares the host kernel.
Build once as an image, and run it as many containers as you need.
Key points
- An image is a read-only template made of layers
- A container is a running instance of an image
- The container adds a thin writable layer that is lost on removal
- One image can start many containers
Likely follow-ups
- What happens to files written inside a container when you remove it?
Common mistakes
- Using the words image and container interchangeably
- Thinking changes made in a running container modify the image