Dockerfiles, Builds and Multistage Images
Dockerfile role
A Dockerfile is a reproducible recipe for creating an image.
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]FROM chooses the base image. RUN performs build actions. EXPOSE documents a port. CMD supplies a default command, while ENTRYPOINT establishes the main executable.
Each build step can add a layer. Build commands should be non-interactive and should not depend on files that exist only on the developer’s computer. A slim base reduces unnecessary software, but an image still needs enough tools to diagnose and run the application.
Multistage builds
Compilers and development tools are useful during building but usually unnecessary at runtime. A multistage build creates a large intermediate build image, then copies only the application and required runtime files into a new small image. Removing build files later is less effective because old layers remain part of the image history.
Retrieval
Why use multistage builds? They keep build dependencies out of the final runtime image, reducing size and attack surface.
Source: Lecture 07 - Lightweight Virtualization - LXC and Docker, PDF pages 56-63.