Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

DK
Docker
0/10 completed
Beginner
Why containers?Install Docker and run your first containerImages vs containers
Intermediate
Writing a DockerfileVolumes and persistent dataNetworking and ports
Advanced
Docker ComposeMulti-stage builds and small images
Pro
Security and production best practicesRegistries and CI/CD
Tracks / Docker / Beginner

Images vs containers

Understanding how images are built from layers explains why Docker is fast, why images can be huge or tiny, and why changes inside a container disappear.

Images are made of layers

An image is a stack of read-only layers. Each layer is a set of file changes: install Python, copy your code, and so on. Layers are shared and cached: if ten images use python:3.12, its layers exist on disk only once.

bash
docker pull python:3.12-slim
docker history python:3.12-slim    # see the layers and their sizes
docker image inspect python:3.12-slim

Containers add a writable layer

When you start a container, Docker adds a thin writable layer on top of the image's layers. Every file you create or change goes there.

┌───────────────────────────┐
│  Container layer (R/W)    │ ← your changes, deleted with the container
├───────────────────────────┤
│  Layer: COPY app code     │
│  Layer: pip install       │  read-only image layers
│  Layer: python runtime    │  (shared by all containers)
│  Layer: debian base       │
└───────────────────────────┘

This leads to the most important rule in Docker:

Containers are disposable. Anything written inside a container is lost when it's removed. Data that must survive goes in a volume (lesson 5).

See it for yourself:

bash
docker run -it --name temp alpine sh
# inside the container:
echo "hello" > /note.txt
exit

docker start -ai temp       # same container → the file is still there
cat /note.txt
exit

docker rm temp
docker run -it alpine sh    # NEW container from the same image
cat /note.txt               # → No such file

Image names and tags

registry/namespace/repository:tag
docker.io/library/nginx:1.27-alpine
ghcr.io/my-org/my-api:v2.3.1
  • If you leave out the registry, Docker uses Docker Hub.
  • If you leave out the tag, Docker uses :latest, which is just a tag name, not a guarantee of anything. In production, always pin specific versions.
  • Variants: -alpine is a tiny Alpine Linux base, -slim is a trimmed Debian, and no suffix means a full Debian with lots of tools.
bash
docker images                        # list local images
docker pull node:22-alpine
docker tag node:22-alpine my-node:dev   # add another name to the same image
docker rmi my-node:dev                  # remove a tag/image

Inspecting containers

bash
docker run -d --name web -p 8080:80 nginx:1.27-alpine
docker inspect web            # full JSON config: IP, mounts, env, etc.
docker stats                  # live CPU / memory usage
docker top web                # processes inside the container
docker diff web               # files changed in the writable layer

Container lifecycle

created → running → paused → running → stopped (exited) → removed
            ▲                              │
            └──────── docker start ────────┘

A container lives as long as its main process (PID 1). When that process exits, the container stops. That's why docker run ubuntu with no command exits immediately: bash has no terminal attached, so it quits.

Practice

  1. Pull nginx:1.27-alpine and nginx:1.27 and compare their sizes with docker images.
  2. Run the alpine one, create a file inside it with docker exec, and view it with docker diff.
  3. Explain in one sentence why the file would be lost after docker rm.
✎ Check your understanding

1.Two containers run from the same image. One modifies /etc/config. What does the other see?

2.What does the tag latest guarantee?

← Install Docker and run your first container