Catalog / Dockerfile Cheat Sheet
Dockerfile Cheat Sheet
A comprehensive cheat sheet for Dockerfiles, covering essential commands, syntax, and best practices for building efficient Docker images. Includes examples and explanations for common use cases.
Dockerfile Basics
Base Image Instruction
|
Sets the base image for subsequent instructions. It’s the foundation of your image. Example:
|
Syntax |
|
Usage |
Always start with a |
Multi-stage Builds |
Use Example:
|
Metadata Instructions
|
Adds metadata to the image in key-value pairs. Example:
|
Syntax |
Keys and values should be properly quoted if they contain spaces. |
Best Practices |
Use reverse DNS notation for keys to avoid conflicts (e.g., |
Multiline Labels |
Labels can span multiple lines using backslashes. Example:
|
Environment Variables
|
Sets environment variables inside the container. Example:
|
Syntax |
The first form allows setting multiple variables at once. The second is more readable for single variables. |
Variable Usage |
Environment variables can be used in other instructions. Example:
|
Best Practices |
Use |
File Management and Execution
File Operations
|
Copies files or directories from the host to the container. Example:
|
Syntax |
|
|
Similar to Example:
|
Best Practices |
Prefer |
Execution Instructions
|
Executes commands inside the container during the build process. Example:
|
Syntax |
The exec form avoids shell interpretation and is generally preferred. |
|
Specifies the command to run when the container starts. Can be overridden by Example:
|
Entrypoint |
Configures a container that will run as an executable. Example:
|
Directory Management
|
Sets the working directory for subsequent instructions. Example:
|
Syntax |
If the directory doesn’t exist, it will be created. |
|
Creates a mount point for accessing and storing data outside the container’s file system. Example:
|
Important Notes |
Changes to the volume are not included when updating the image. Volumes are designed for persistent storage. |
Networking and Build Arguments
Networking Instruction
|
Declares the ports that the container will listen on at runtime. It’s informative but doesn’t actually publish the port. Example:
|
Syntax |
|
Publishing Ports |
To actually publish the ports, use the |
Build Arguments
|
Defines variables that can be passed during the build process using the Example:
|
Syntax |
Arguments can have default values. |
Usage |
Build arguments are useful for passing sensitive information or configuration values at build time. Example (using docker build):
|
Scope |
Arguments are only available during the build process and are not stored in the final image unless explicitly set as environment variables. |
User Instruction
|
Sets the user to use when running subsequent Example:
|
Syntax |
Can be a username or a numeric UID. |
Best Practices |
Avoid running processes as |
Advanced Dockerfile Concepts
Healthcheck Instruction
|
Configures a health check command to determine if a container is healthy. Example:
|
Syntax |
Options:
|
Return Codes |
|
Onbuild Instruction
|
Defers the execution of a command until the image is used as the base for another build. It is triggered when a downstream image is built. Example:
|
Syntax |
Any valid Dockerfile instruction can be used. |
Use Cases |
Useful for creating reusable base images that perform common tasks, such as installing dependencies or setting up configurations in derived images. |
Important Considerations |
Avoid using |
Shell Instruction
|
Overrides the default shell used for the shell form of the Example:
|
Syntax |
Specifies the executable to use as the shell. Defaults to |
Strict Mode Example |
Run commands in strict shell.
|