Dockerfile Cheat Sheet

"Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. This page describes the commands you can use in a Dockerfile." - Docker Docs

This is meant to be a quick and dirty reference to help you get started. Reference the docker docs if you need more information, they're linked in the sources below.

Starting Point

Arguments that dictate what to use as a starting point for the dockerfile

FROM

Specifies what image to use as a base

Usage:

FROM <image>
FROM <image>:<tag>
FROM <image>@<digest>

Notes:

  • From is a required argument, and must come first
  • If no tag is specified, the "latest" tag will be used

Metadata

Arguments that don't affect how the image builds or container runs, but give information about the image

MAINTAINER (DEPRECATED)

Allows you to specify an author - REPLACED with LABEL Author="foo"

Usage:

LABEL Author="<name>"
# MAINTAINER <name>

LABEL

Adds metadata to an image

Usage:

LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."

Setup

ENV

Specifies environment variables Usage:

ENV <env_var_name> <value>

ADD

Copies files or directories to container's file system

Usage:

# ADD <host_src> <container_dest>
COPY <host_src> <container_dest>

Notes:

  • ADD can be buggy, use COPY where you can

COPY

Copies files or directories to container's file system

Usage:

COPY <host_src> <container_dest>

VOLUME

Create mount point, marks as holding external mounted items Usage:

VOLUME ["<DIR_PATH1>", "<DIR_PATH2>", ...]

WORKDIR

Set the current working directory

Usage:

WORKDIR <DIR_PATH>

SHELL

Command to change the shell that RUN uses

Usage:

SHELL ["<cmd>", "<arg1>", "<arg2>", ...]

Notes:

  • Only changes shell for following RUN commands, you can switch between shells if you so desire

Build

RUN

Command to run in order to build the image

Usage:

RUN <cmd>
RUN ["<cmd>", "<arg1>", "<arg2>", ...]

Notes:

  • The build will fail if a run command returns a non-zero code
  • Runs with the "default shell" (can be changed with the SHELL command)

Running

EXPOSE

A way of communicating/documenting which image ports should be forwarded when ran as a container

EXPOSE 80
# ^ notes that tcp port 80 should be opened
EXPOSE 53/udp
# ^ Specifies udp on port 53
EXPOSE 53/tcp
EXPOSE 53/udp
# ^ Specifies both udp and tcp

Notes:

  • Doesn't actually open the port, you'll need to add that to the docker-compose file or docker run (-p) command

CMD

Marks the command to be run automatically when a container starts if none is manually marked at runtime

Usage:

CMD ["<executable>", "<arg1>", "<arg2>"]
CMD ["<arg1>", "<arg2>"]
CMD <command> <arg1> <arg2>

Notes:

  • If you are also using ENTRYPOINT, you can use CMD to specify arguments only

ENTRYPOINT

Marks the command to be run automatically when a container is run interactively, overrides CMD Usage:

ENTRYPOINT ["<executable>", "<arg1>", "<arg2>"]
ENTRYPOINT <command> <arg1> <arg2>

Sources