Skip to the workspace
Pebellslearn by building
Containers  ›  Lesson

Docker Images and Containers

Docker 12 minA Docker image is a read-only template and a container is a disposable running copy with a writable layer, so persistent data goes in volumes and changes are made by replacing containers.

devops / Docker Images and Containers

Explore each scene at your own pace.

01 · The rule

The rule

Docker Images and Containers

You have heard "just run the Docker image". To do that confidently you need two words straight, image and container, and a handful of commands that start, inspect and clean up.

The rule

An image is a read-only template; a container is one running copy of it.

An image is a packaged, read-only filesystem plus metadata saying what to run. It is identified by a name and a tag, such as python:3.12-slim or notes-api:1.0. Images come from a registry such as Docker Hub or your company's private one.

A container is created from an image and adds a thin writable layer on top. You can start many containers from one image, just as you can run many processes from one program.

docker pull nginx:1.27                        # download an image
docker run -d --name notes -p 8080:8000 notes-api:1.0   # start a container in the background
docker ps                                     # running containers (-a includes stopped)
docker logs -f notes                          # follow its output
docker exec -it notes sh                      # open a shell inside it
docker stop notes && docker rm notes          # stop, then remove

Read -p 8080:8000 as host:container: traffic to port 8080 on the host goes to port 8000 inside the container, where notes-api listens.

The writable layer disappears with docker rm. Anything that must survive, such as a database's files or uploaded notes, belongs in a volume mounted into the container:

docker run -d --name notes -p 8080:8000 -v notes-data:/data notes-api:1.0

Settings go in as environment variables with -e, so the same image runs in testing and production with different configuration.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.