How do a Docker image and a container work together?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
A Docker image and a container are two stages of the same lifecycle. The image is the packaged application content and configuration. Docker uses it as the read-only base when it creates a container, then adds runtime configuration, isolation, networking, mounts, resource controls, and a writable layer.
For example, CI may build one orders:1.4 image. I can create orders-a on host port 8081 and orders-b on 8082 from that same image. They share the image content but have different container IDs, runtime settings, processes, and writable layers.
A container can be created, running, paused, or stopped; it is not only a running process. Stopping it normally keeps the writable layer so it can start again. Removing it discards that container layer, while the image remains available. For durable data I use a volume or external store, and for exact release identity I record a digest because a tag can move.
How do a Docker image and a container work together
Direct Answer
A Docker image is the packaged, read-only content and configuration used to create containers. Docker creates a container from that image, then adds runtime settings and a writable layer. One image can create many separate containers, and stopping a container is different from removing it.
Code Example
# Build one image and see its content identifier.
docker build -t orders:1.4 .
docker image inspect orders:1.4 --format '{{.Id}}'
# Create two separate containers from that image.
docker run -d --name orders-a -p 8081:8080 orders:1.4
docker run -d --name orders-b -p 8082:8080 orders:1.4
# Stopping does not remove the first container.
docker stop orders-a
docker start orders-a
# Removing a container does not remove the image.
docker rm -f orders-b
docker image ls orders
Common Mistakes & Pitfalls
- Editing a container as if it updates the image: a manual file change belongs only to that container's writable layer. Change the build input and create a new image instead.
- Treating stop and remove as the same action: a stopped container can normally be started again; a removed container must be created again.
- Using a tag as proof of content: a tag can move. Use or record the image digest when exact identity matters.
- Keeping important data only in the writable layer: place durable data in a volume, bind mount, or external service.
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.