This post introduces you top 8 dockerfile best practices developers should know. Docker is used the day more in modern engineering. However, its best practices are often forgotten and overlooked. Docker best practices will help you take advantage of them and you will improve your container stability, speed up deploy processes, cut down on image sizes and tighten security. If you want to build a docker image first, please refer to the post How to run Spring Boot with Docker.
Top 6 Dockerfile Best Practices Developers
1. FROM command
From command shouldn’t be latest and should have a tag
You should find the version of the image you want to use and specify the exact version you in Dockerfile. Don’t use the latest tag. Although, it has the convenience of always being available for images on Docker Hub. But you can get unexpected results or produce breaking changes.
2. USER property
USER property hould be specified, and it should not be root.
The USER command allows you to set user’s ID within the Dockerfile. If you don’t specify one user’s ID the default user is used it will be user root. Apparently, you will know the security vulnerabilities by running service as the root user.
3. HEALTHCHECK property
HEALTHCHECK property should use
In a microservice architecture, It would be great if you have visibility of every service. You can access the api via the HEALTHCHECK command. You can refer to the official document here.
4. LABEL property
LABEL property should is used to set the metadata information.
The LABEL property is a feature of Docker allows us to specify custom metadata in the form of key/value pairs. It helps us to produce readable and easily maintainable Dockerfile. We also extract the property of labels by using command
docker inspect at the build time.
5. .dockerignore file
Keep your image lightweight with a .dockerignore file. Let’s have a look an example of .dockerignore file.
Ignoring unnecessary files helps to speed up the build time. You can read up on exactly how to create a .dockerignore file in the official docs here.
6. Use official images
You should use official images when possible. Official images have clear documentation, best practices are applied and are designed for the most common use cases. Official images are available on the Docker Hub.
7. Order matters
Let’s have a look at Dockerfile below
I noticed that the way Docker operates is to try to reuse as much as possible. So placing the instructions in the Dockerfile from the least likely to change to the most likely to change is the best practice. This best practice has been applied for the above Dockerfile.
8. Minimize the layers
Here is the optimized Dockerfile from the above one.
You know every command in Dockerfile you enter creates a new image layer. So you ensure the number of commands to a minimum you will speed up the build time. Here, we have grouped three
LABELinstructions into one.
That’s all about Top 8 Dockerfile Best Practices Developers Should Know. You might be interested in some docker tutorials, click here.