How to create Docker images like a PRO?
Tags: devops, docker, programming, opensource
Published: 2024-01-17 23:47:54


![Docker Container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhp5pu3cyq4pfqq5ofak.png)

---


So for a long time working with containers, I learned many tips and tricks about good practices to build a good image. I know today many DevOps professionals talking about Kubernetes, Clusters, Rancher, etc…
And forget the base for any cluster, build a good image with minimum best practices, in this article I try to explain and show some things to do this.

...

**Best Practice**

Let's discuss the concept of best practices, which is common in the technology industry. We have encountered this term many times in various services and tools used by us. Adhering to best practices can help you follow a set of steps that many developers have already gone through (and even suffered 😂), and they know what constitutes a good practice. The beauty of open source is that you can find well-written documentation, which [serves as an excellent example](https://docs.docker.com/develop/dev-best-practices/).

Using best practices for building containers can be confusing at first. But let’s break it down into just 3 parts:

**1. build small containers with only what’s needed;**
**2. focus on security;**
**3. consider using a linter for extra vigilance and security scan;**

My goal is to present a practical approach to container best practices, including demonstrations of specific tools and instructions on how to use them.

## 1 — Build a Small Container

Building a small container is the first step you can start a best practice, working with a small image can help you in many situations, like performance improvement, enhanced security, build time, and of course the performance size, but how I can do this?

This is very simple, start using the official base image from the [Docker Hub repository](https://hub.docker.com/search?q=):

![Docker Hub Example 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5x5zmn9tl95qasjk792.png)

Here you can find the official images of any language, service, or system that you need, for example, if I build an image for my Python code, is just get the image, an example of how to search for a good image for you:


![Docker Hub Example 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdmb07bbn38dqh4ux0cj.png)

Working with Alpine images is amazing, your images work like you need and are very small and performative, just try.


## 2 — Focus on Security

Talking about security using containers may be a challenge today, many DevOps, Developers create images without using official images, and using official images in your application helps you prevent many security issues. But using just official images can’t prevent issues problem, most companies start using an image but don’t upgrade, just use it. For example, I have worked for a company that used an official image of Ubuntu, but used version 12.04 (this version doesn’t have more support), and the security risk is very high.

_But how I can keep my images secure?_

- Always use official images (Python, Ubuntu, NodeJS, etc…);
- Keep your images updated;
- Always use a security scan on your images (I use [Trivy](https://aquasecurity.github.io/trivy/v0.48/), it’s open source and works amazing, basically this tool generates a report about security issues found in your image):

_An example using the docker command:_

```sh
docker run -u 0 — rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy image — exit-code 1 — severity HIGH imagename:latest
```

- [Non-Root](https://code.visualstudio.com/remote/advancedcontainers/add-nonroot-user) Acces, one of the most important practices that ends up being forgotten nowadays;
- NEVER EVER USE CREDENTIALS OR TOKENS IN YOUR IMAGE FIXED (OR IN YOUR REPO)!
- Using if necessary [multi-stage builds](https://docs.docker.com/build/building/multi-stage/);
- Always apply the minimum privilege of who needs access to the image (always work with a private repo);
- If your company and you are crazy about security, consider using the Cosing to keep your images assigned [example here](https://docs.digicert.com/zf/software-trust-manager/signing-tools/container-signing-with-cosign-and-sigstore.html).


## 3 — Use a linter and security scan

In the topic above I have explained the importance of using a security scan in your images using a Trivy, but if want to keep a good image I recommend also using a linter scan in my actual scenario I use [Hadolint](https://github.com/hadolint/hadolint), the Hadolint is an amazing tool to get improvements in your image.
  like: _don’t use many RUN commands on your Dockerfile._

A simple example how to use a Hadonlint:

```sh
docker run — rm -i hadolint/hadolint < Dockerfile
```


## Conclusion

Of course, every I’m writing here isn't mandatory, but can help many DevOps, Developers, and container lovers build a good image with the minimum best practices. If you want more about containers and best practices always use documentation

---
_I hope this helps everyone and keeps your image updated 😁🐳!_