Dockerfiles – best practices

Dockerfiles – best practices

Let’s take a look at a few tips that are considered best practices for writing our own Dockerfiles. Following these will ensure that your images are lean, consistent, and easy for others to follow:

  • You should try to get into the habit of using a .dockerignore file. We will cover the .dockerignore file in the Building Docker images section of this chapter; it will seem very familiar if you are used to using a .gitignore file. It will essentially ignore the items you specified in the file during the build process.
  • Remember to only have one Dockerfile per folder to help you organize your containers.
  • Use a version control system, such as Git, for your Dockerfile; just like any other text-based document, version control will help you move not only forward, but also backward, as necessary.
  • Minimize the number of packages you need to install per image. One of the biggest goals you want to achieve while building your images is to keep them as small and secure as possible. Not installing unnecessary packages will greatly help in achieving this goal.
  • Make sure there is only one application process per container. Every time you need a new application process, it is good practice to use a new container to run that application in.
  • Keep things simple; over-complicating your Dockerfile will add bloat and potentially cause you issues down the line.
  • Learn by example! Docker themselves have quite a detailed style guide for publishing the official images they host on Docker Hub. You can find a link to this in the Further reading section at the end of this chapter.

Leave a Reply

Your email address will not be published. Required fields are marked *