Dockerfile
The Dockerfile is a script that contains a collection of commands and instructions for building a Docker container image. This cheat sheet provides essential keys and commands to help programmers write efficient Dockerfiles.
Essential keys
Section titled “Essential keys”This instruction sets the base image for your Docker container.
FROM ubuntu:20.04The example above starts the Dockerfile by using the Ubuntu 20.04 image as the base. It’s the first instruction in a Dockerfile.
MAINTAINER
Section titled “MAINTAINER”This key sets the author field of the generated images.
MAINTAINER John Doe <johndoe@example.com>In the modern Docker versions, it’s recommended to use the LABEL instruction for this.
This key executes commands in a new layer on top of the current image.
RUN apt-get update && apt-get install -y curlThe above command updates the package lists and installs curl.
Provides defaults for the executing container. Only one CMD is allowed.
CMD ["echo", "Hello, World!"]If the container is run without specifying a command, it will execute the above echo command.
ENTRYPOINT
Section titled “ENTRYPOINT”Allows you to configure the container to run as an executable.
ENTRYPOINT ["echo"]CMD ["Hello, World!"]With this configuration, if you run the container without arguments, it will echo “Hello, World!”.
WORKDIR
Section titled “WORKDIR”This key sets the working directory inside the container.
WORKDIR /appAll the following instructions in the Dockerfile will be run in the /app directory.
This instruction sets the user or UID and optionally the group or GID to use when running the image.
USER developerThe image will be run using the “developer” user.
EXPOSE
Section titled “EXPOSE”Informs Docker that the container will listen on the specified network ports at runtime.
EXPOSE 80This tells Docker that our container will listen on port 80.
Sets an environment variable.
ENV MY_NAME JohnThis sets an environment variable called MY_NAME with the value “John”.
Copying and adding files
Section titled “Copying and adding files”This instruction copies new files, directories, or remote file URLs and adds them to the filesystem of the image.
ADD source /destinationWhile powerful, it’s often recommended to use COPY unless you need the tar and remote URL handling of ADD.
This key is similar to ADD, but without the tar and remote URL capabilities.
COPY local-file-path /destination-in-containerCOPY is more transparent because it only supports the basic copying of local files into the container.
Volumes
Section titled “Volumes”VOLUME
Section titled “VOLUME”This key creates a mount point for externally mounted volumes or other containers.
VOLUME /dataThis will create a mount point at /data which can be mounted by the host or other containers.
Arguments and environment variables
Section titled “Arguments and environment variables”Defines a variable that users can pass at build-time to the builder.
ARG MY_VAR=default_valueYou can pass a value to this during build with the —build-arg flag.
We already covered ENV under essential keys. It’s important to note that while both ARG and ENV can set environment variables, ARG is only available during the build of a Docker image and not in the container when it runs.
Layer optimization
Section titled “Layer optimization”Minimize the number of layers
Section titled “Minimize the number of layers”Docker images are composed of layers. To make images smaller, you can minimize the number of layers.
RUN apt-get update && apt-get install -y curl && apt-get cleanBy combining commands with &&, you create a single layer instead of three.
Grouping commands
Section titled “Grouping commands”By grouping related commands, you can reduce the number of layers and make your Dockerfile more readable.
RUN apt-get update && \ apt-get install -y curl vim && \ apt-get cleanThe backslashes allow us to break one long command into readable segments.
Cleaning up in the same layer
Section titled “Cleaning up in the same layer”After installing packages, it’s good to clean up cache to reduce image size.
RUN apt-get update && \ apt-get install -y curl && \ apt-get clean && \ rm -rf /var/lib/apt/lists/*The cleanup commands ensure that the intermediate cache and package data are not stored in the final image layer.