What are the best practices to write a Dockerfile?
best practices to write a Dockerfile

Introduction

In this tutorial, we recommended methods and best practices for building efficient images.

The Docker builds images automatically by following or reading the instructions from a Dockerfile (here, Dockerfile is a text file that store or contains all the commands, in order, that are needed/required to build a given image).

Dockerfile stick to a specific format and set of instructions. A Docker image consists of read-only layers each of which reflects a Dockerfile instruction. The layers are stacked and each layer is a delta of the changes from the previous layer.

Dockerfile Sample

The sample Dockerfile is shown as below:

Guidelines and Recommendations

  • Create ephemeral containers – The Dockerfile should define images and generate containers in such a way that it will be as ephemeral as possible. Here, “ephemeral” means that the containers can be destroyed and can be stopped, then rebuilt and replaced with a very minimum set up and configuration.
  • Understand build context – The build context means, the current working directory from where you execute the docker build command and the dockerfile is located in this directory by default (which you can change also using the file flag “-f”).
    Also, all the recursive contents of directories and files of the current directory are sent to the Docker daemon as the build context.
  • Pipe Dockerfile through stdin – Docker uses piping inside Dockerfile to build images through stdin with a remote or local build context. Piping a dockerfile is useful to perform the one-off builds without writing the file to disk.
  • Exclude with .dockerignore – Use a .dockerignore file to exclude files that are not relevant to the build without restructing your source repo, this file supports some patterns similar to the .gitignore files.
  • Don’t install unnecessary packages – To reduce file sizes, dependencies, build times and complexity avoid installing extra or unnecessary packages just because they might be nice to have.
  • Minimize the number of layers – In Docker’s old versions, it was important that you minimized the number of layers in your images to ensure they were performant.

Instructions for Docker

LABEL

To organize the images by project, you can add labels to your image.
Add a line beginning with LABEL for each label and with one more key-value pairs.

Strings with spaces are quoted or the spaces are escaped. Inner quote characters (“), must also be escaped.

EXPOSE

This instruction indicates all the ports on which the container listens for the connections. But you can use the traditional or common port for your application.

FROM

Always use current official images based on your images. We recommend using the Alpine image because it is tightly controlled and small in size (e.g. under 5 MB), while still being a full Linux distribution.

RUN

Split the complex and long RUN statements which are separated on multiple lines with the backslashes for making your Dockerfile more maintainable, understandable, and readable.

APT-GET

It is the most common use-case for RUN in an application of apt-get.
This installs packages. Always combine RUN apt-get update with apt-get install in the same RUN statement.

Example:

ADD or COPY

Both COPY and ADD similar functionalities, but COPY is preferred.
This is because it is much more transparent than ADD. Copy only copies some files in the container. You can use the ADD in the best way is local tar file auto-extraction into an image.

For example:

VOLUME

The VOLUME instruction is used to reveal any configuration storage, files, or database storage created by docker container. You should strongly be encouraged to use VOLUME for any mutable and/or user-serviceable and also parts of your image.

WORKDIR

For reliability and clarity, you should use absolute paths for your WORKDIR and use WORKDIR instead of proliferating instructions like RUN cd … && do-something, which are really very hard to read, troubleshoot, and maintain.

Examples for Official Images

Below are some Official Images which have exemplary Dockerfiles:

  • Ruby
  • Perl
  • Go
  • Hy

Related Posts