Docker & Kubernetes · Module 02
Building Docker Images with Dockerfile
Write Dockerfiles, understand layers and the build cache, use CMD/ENTRYPOINT correctly and keep production images small with multi-stage builds.
What you will learn
- Build, tag and inspect the layers of an image from a Dockerfile
- Use FROM, RUN, WORKDIR, COPY, EXPOSE, ENV and ARG correctly
- Predict the command that runs when ENTRYPOINT, CMD and docker run arguments combine
- Shrink images and use the build cache well with multi-stage builds
Lessons
- Dockerfile and image layers
A Dockerfile is the recipe for an image; every instruction that changes the filesystem adds a layer on top of the base image.
- The main Dockerfile instructions
FROM, RUN, WORKDIR, COPY, EXPOSE, ENV and ARG — what each instruction does and the usual traps.
- CMD and ENTRYPOINT
ENTRYPOINT is the main program, CMD holds the default arguments; arguments after the image name replace CMD, not ENTRYPOINT.
- Multi-stage builds and the build cache
Split the build stage from the production stage so the final image only holds the artifact; order instructions to use the cache.