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, .dockerignore helps exclude large or sensitive files from being added to images using ADD or COPY.

    # comment

  • FROM: Defining the Base Image

    The FROM instruction sets the base image for subsequent instructions.

    FROM <image> [AS <name>]


    FROM ruby:2.6.3

  • RUN: Executing Commands

    RUN allows executing commands in two forms: shell form and exec form.

    • Shell Form:

      RUN <command>
    • Exec Form:

      RUN ["executable", "param1", "param2"]

  • CMD: Specifying Default Execution

    CMD defines the default command to run when the container starts.

    CMD ["executable", "param1", "param2"]

  • LABEL: Adding Metadata

    LABEL adds metadata to an image.

    LABEL <key>=<value> <key>=<value> <key>=<value> ...

  • EXPOSE: Specifying Network Ports

    EXPOSE informs Docker about the container’s listening ports.

    EXPOSE <port> [<port>/<protocol>...]

  • ENV: Setting Environment Variables

    ENV sets environment variables that persist throughout the container’s life.

    ENV <key> <value>


    ENV myName John Doe

  • ADD: Adding Files

    ADD copies files from the source to the destination.

    ADD [--chown=<user>:<group>] <src>... <dest>

  • COPY: Copying Files

    COPY copies files from the source to the destination.

    COPY [--chown=<user>:<group>] <src>... <dest>

  • ENTRYPOINT: Configuring Execution

    ENTRYPOINT specifies the executable and default parameters.

    ENTRYPOINT ["executable", "param1", "param2"]

  • WORKDIR: Setting the Working Directory

    WORKDIR sets the working directory for commands.

    WORKDIR /path/to/workdir


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.