02-docker.md
md3 kBIn assignment_1_solution.zip
2. Docker: images, containers, compose
Vocabulary (say it once, clearly)
- Image: a read-only template (filesystem layers + metadata). Built from a
Dockerfile. - Container: a running instance of an image, with its own writable layer, network and process tree.
- Registry / repository / tag: where images are stored / a named collection / a version label.
docker.io/alice/campus-events:backend-1.0.0= registry / namespace / repository : tag.
Read the Dockerfiles
backend/Dockerfile (two stages):
depsstage runsnpm ci --omit=dev. Copyingpackage*.jsonbefore the source means this slow layer is cached until dependencies change.- Runtime stage copies only
node_modulesandsrc, removes npm (not needed at runtime, and the main source of CVEs), runs as thenodeuser, declares aHEALTHCHECK, and uses exec-formCMDsodocker stopreaches node directly.
frontend/Dockerfile (two stages):
buildstage: Node compiles React to static files indist/.- Runtime stage:
nginx-unprivileged+dist/+nginx.conf. No Node, no source code. Non-root, so it listens on 8080.
.dockerignore keeps node_modules, .env and .git out of the build
context: faster builds and no secrets in images.
Build and run ONE container
docker build -t campus-events-frontend:1.0.0 ./frontend
docker images campus-events-frontend
docker history campus-events-frontend:1.0.0 # the layers
docker run -d --name web -p 8080:8080 campus-events-frontend:1.0.0
docker ps # STATUS shows (healthy)
curl http://localhost:8080/healthz # PowerShell: curl.exe ...
docker logs web
docker rm -f webThe page loads but API calls fail: nginx cannot find a host called backend.
A single container is not the application. That is why we need compose.
Run the whole application with compose
cp .env.example .env # PowerShell: copy .env.example .env
docker compose up --build -d
docker compose ps
docker compose logs -f backend # Ctrl+C to stop followingOpen http://localhost:8080.
What docker-compose.yml shows:
| Feature | Where |
|---|---|
| 3 services | db, backend, frontend |
| Service discovery by name | DATABASE_URL=...@db:5432, nginx proxy_pass http://backend:3000 |
| Start order with health | depends_on: condition: service_healthy |
| Persistent data | named volume db-data |
| Secrets from the environment | ${POSTGRES_PASSWORD:?...} fails fast if missing |
| Only one public port | frontend has ports:, the others don't |
Prove the isolation:
docker compose exec backend wget -qO- http://localhost:3000/api/health # works inside
curl http://localhost:3000/api/health # fails from the hostProve persistence:
docker compose down # containers and network removed, volume kept
docker compose up -d # your users and events are still there
docker compose down -v # -v also deletes the volume: data gone