Chapter 03
Images, Dockerfiles and registries
Image layers and tags
Images are layered, immutable templates. Tags are mutable references, so pin versions or digests for reproducible production builds.
docker image ls
docker pull python:3.12-slim
docker image inspect python:3.12-slimHands-on lab: Inspect the image size and entrypoint.
Build a minimal Python image
A Dockerfile specifies a base image, working directory, copied files and a default command. Use a .dockerignore and avoid copying secrets into an image.
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]
# save as Dockerfile; create app.py with print("Hello from Docker")
docker build -t training-python:1 .
docker run --rm training-python:1Hands-on lab: Build and run the image; edit the message and rebuild.
Quick check: Why use COPY instead of editing a running container?
Show answer
The build becomes reproducible and versionable.
Registries
Docker Hub is a public registry; organizations may use private registries. Authentication is required for private images. Never put registry credentials in Dockerfiles.
docker login
docker tag training-python:1 YOUR_USER/training-python:1
# docker push YOUR_USER/training-python:1Hands-on lab: Explain the difference between an image name and a registry address.