Learn Dockerfile by Examples
Docker Engine 20.10+Docker is an open platform for developing, shipping, and running applications. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
These examples cover the essential instructions and best practices for writing efficient, secure, and maintainable Dockerfiles. You will learn how to optimize build layers, manage configuration, and structure multi-stage builds.
Base Image Selection
The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. This sample code demonstrates how to choose specific versions, digests, and minimal images.
Layer Caching Strategy
Docker caches the result of each instruction to speed up builds. This code example shows how to order instructions to maximize cache hits and optimize build times.
Multi Stage Build Layout
Multi-stage builds allow you to use a heavy image for building and a light image for running. This sample code demonstrates how to compile a Go app and ship only the binary.
Environment Variable Definition
The ENV instruction sets environment variables that persist when the container runs. This code example shows how to set configuration defaults and path variables.
Build Argument Structure
ARG defines variables that are only available during the build process, not at runtime. This sample code shows how to use them for versioning and build-time configuration.
Working Directory Configuration
The WORKDIR instruction sets the context for subsequent instructions. This code example illustrates how it simplifies paths and creates directories automatically.
File Copy Patterns
COPY and ADD are used to move files into the image. This sample code explains the differences and shows how to use .dockerignore to exclude unwanted files.
Package Installation Setup
Installing system packages is a common task. This sample code demonstrates the best practices for apt-get and apk to keep images clean and avoid caching stale lists.
Source Code Inclusion
This sample code focuses on the COPY instruction specifically for application code, highlighting the importance of .dockerignore and user permissions.
Entrypoint Instruction Format
ENTRYPOINT configures the container to run as an executable. This sample code shows how to use it to create wrapper scripts or fixed commands that accept arguments.
Command Instruction Structure
CMD provides defaults for an executing container. This sample code explains how it interacts with ENTRYPOINT and how it can be overridden.
Port Exposure Definition
The EXPOSE instruction documents which ports the container listens on. This code example shows how to define ports and protocols for documentation and linking.
Volume Mount Declaration
The VOLUME instruction creates a mount point for externally mounted volumes. This sample code shows how to define persistent storage areas within the image.
Healthcheck Instruction Schema
Docker can periodically check if your application is working correctly. This sample code demonstrates how to use HEALTHCHECK to define a custom probe command.
User Permission Configuration
Running containers as root is a security risk. This sample code shows how to create a dedicated user and switch context to enforce least privilege.
Image Optimization Techniques
Optimizing images reduces storage costs and speeds up deployments. This sample code illustrates techniques like chaining commands and cleaning caches.
Alpine Image Usage
Alpine Linux is a popular choice for base images due to its tiny size. This sample code highlights the specific package manager (apk) and compatibility considerations.
Python Application Image
Python images require specific environment variables for optimal behavior. This sample code shows how to configure buffering, bytecode, and dependency installation.
Node Application Image
Node.js images benefit from production settings and deterministic installs. This sample code shows how to use NODE_ENV and npm ci.
Go Application Image
Go applications can be compiled into static binaries, allowing the use of the scratch image. This sample code demonstrates the ultimate minimal image.

