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.# comment */temp* */*/temp* temp?
FROM: Defining the Base Image
The
FROMinstruction sets the base image for subsequent instructions.FROM <image> [AS <name>]Example:
FROM ruby:2.6.3
RUN: Executing Commands
RUNallows executing commands in two forms: shell form and exec form.Shell Form:
RUN <command>Exec Form:
RUN ["executable", "param1", "param2"]
CMD: Specifying Default Execution
CMDdefines the default command to run when the container starts.CMD ["executable", "param1", "param2"]
LABEL: Adding Metadata
LABELadds metadata to an image.LABEL <key>=<value> <key>=<value> <key>=<value> ...
EXPOSE: Specifying Network Ports
EXPOSEinforms Docker about the container’s listening ports.EXPOSE <port> [<port>/<protocol>...]
ENV: Setting Environment Variables
ENVsets environment variables that persist throughout the container’s life.ENV <key> <value>Example:
ENV myName John Doe
ADD: Adding Files
ADDcopies files from the source to the destination.ADD [--chown=<user>:<group>] <src>... <dest>
COPY: Copying Files
COPYcopies files from the source to the destination.COPY [--chown=<user>:<group>] <src>... <dest>
ENTRYPOINT: Configuring Execution
ENTRYPOINTspecifies the executable and default parameters.ENTRYPOINT ["executable", "param1", "param2"]
WORKDIR: Setting the Working Directory
WORKDIRsets the working directory for commands.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.