Writing lean Dockerfiles to improve image builds

Writing lean Dockerfiles to improve image builds

 2 min read

 

Files in white cabinet

Imagine if you could consolidate parts of your process and improve the speed and quality of your image builds, thus improving the content of your Docker container. This is possible with Dockerfiles!

 

What is a Dockerfile? According the official Docker documentation, a Dockerfile is “a text document that contains all the commands a user could call on the command line to assemble an image.” A developer trying to assemble an image in Docker can save time and effort by using a Dockerfile, since it’s a bundled series of pre-assembled build instructions that are validated upon execution.

 

Dockerfiles are only useful, however, if they’re written well. When formed concisely, they speed up image builds and can be adjusted and reused. So how we write Dockerfiles well?

 

In a recent blog post outlining best practices for creating Dockerfiles, Docker covered a variety of suggestions about how to approach writing file content. We’ve pared these down to a few key themes:

 

1. Be specific. As you write commands, be as specific as possible. This will force you to examine the overall function of each line, section, and total file and decrease the likelihood of execution errors.

 

2. Consolidate what you can, but keep unique items separate. If you can execute a command in one line instead of four, do it. If you can reference an external source instead of writing it directly into your Dockerfile, do it. The more concise your file, the smaller the file size and the less room there is for error. However, don’t combine unique processes into a single step; this complicates the repeatability of your instructions. Each unique command should have its own line.

 

3. Pay attention to command order. With every additional command line in a Dockerfile, image assembly gets more complicated. If your commands are out of order, they may cancel each other or interrupt the execution of the build.

 

 

Dockerfile correct order
An example of correct command line order. (Source: Tibor Vass, Official Docker blog.)

 

4. Don’t reinvent the wheel. If you have command lines already written, adapt them instead of starting over completely. While it may seem valuable to start with a fresh slate, you’ll lose valuable time and structure if you do. Look for similar Dockerfiles and resources on GitHub to get you started.

 

5. Think of your file as a template. If you focus on reproducibility, you’ll be more inclined to clean up your file. Using messy code costs time, money, and patience—every time! On the other hand, clean files are easy to use as a resource in multiple ways: as a template for a new project, as a resource linked in another file, and as an example of how to assemble an image correctly.

 

When written correctly, Dockerfiles are a fantastic tool for improving your image builds. For more information, check out the official Docker blog.

 

 

 

 

Docker logo