Sub topic 1
What a Docker Image Is
Prerequisites
You should know basic command-line use, understand what a container is, and have Docker Engine or Docker Desktop installed. This chapter assumes you have used docker run.
What a Docker image is
A Docker image is an immutable package containing a filesystem, application files, metadata, and default runtime settings. A container is a running instance of an image. The image is the template; the container is the process created from that template.
Images are designed to be portable. The same image can run on a developer laptop, a test system, or a production host, provided the CPU architecture and runtime requirements match.
Layers: how images are stacked
An image consists of read-only layers. Each Dockerfile instruction usually creates a layer. Docker combines these layers into one filesystem when it starts a container.
The container adds a small writable layer above the image. Changes made while the container runs go there and disappear when the container is removed. Separate containers can share the same read-only image layers, which saves disk space.
Naming images: repositories and tags
An image reference can include a registry, repository, tag, or digest:
registry.example.com/team/api:1.4
Here, registry.example.com is the registry, team/api is the repository, and 1.4 is the tag. If the registry is omitted, Docker commonly uses Docker Hub. A tag is a movable label. A digest, such as sha256:..., identifies exact content and does not move when new images are published.
Building an image with a Dockerfile
A Dockerfile is a text file containing image-building instructions.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
CMD ["python", "app.py"]Build it with:
docker build -t example/api:1.0 .The final . is important. It selects the build context.
The build context and `.dockerignore`
The build context is the set of files Docker can send to the builder. Keep it small with .dockerignore:
.git
.env
*.log
__pycache__This reduces upload time and prevents local secrets or unnecessary files from entering the build.
Exercise
Why should a tag not be treated as an immutable version?
Show answer
A tag is a movable label. The registry can later point it to different image content, while a digest identifies one exact image.