Understanding Dockerfile: A Comprehensive Cheat Sheet
Dockerfile is the blueprint for building Docker images, and understanding its various instructions is crucial for creating efficient and functional containers. This cheat sheet provides a quick overview of essential Dockerfile instructions and their usage.
.dockerignore File: Avoiding Unnecessary Files
Just like
.gitignore,.dockerignorehelps exclude large or sensitive files from being added to images usingADDorCOPY.1 2 3 4
# comment */temp* */*/temp* temp?
FROM: Defining the Base Image
The
FROMinstruction sets the base image for subsequent instructions.1
FROM <image> [AS <name>]Example:
1
FROM ruby:2.6.3
RUN: Executing Commands
RUNallows executing commands in two forms: shell form and exec form.Shell Form:
1
RUN <command>
Exec Form:
1
RUN ["executable", "param1", "param2"]
CMD: Specifying Default Execution
CMDdefines the default command to run when the container starts.1
CMD ["executable", "param1", "param2"]
LABEL: Adding Metadata
LABELadds metadata to an image.1
LABEL <key>=<value> <key>=<value> <key>=<value> ...
EXPOSE: Specifying Network Ports
EXPOSEinforms Docker about the container’s listening ports.1
EXPOSE <port> [<port>/<protocol>...]
ENV: Setting Environment Variables
ENVsets environment variables that persist throughout the container’s life.1
ENV <key> <value>
Example:
1
ENV myName John Doe
ADD: Adding Files
ADDcopies files from the source to the destination.1
ADD [--chown=<user>:<group>] <src>... <dest>
COPY: Copying Files
COPYcopies files from the source to the destination.1
COPY [--chown=<user>:<group>] <src>... <dest>
ENTRYPOINT: Configuring Execution
ENTRYPOINTspecifies the executable and default parameters.1
ENTRYPOINT ["executable", "param1", "param2"]
WORKDIR: Setting the Working Directory
WORKDIRsets the working directory for commands.1
WORKDIR /path/to/workdir
Summary
This Dockerfile cheat sheet provides a quick reference to essential instructions and their usage. Understanding these instructions is key to efficiently build Docker images and create functional containers.
