Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

Docker Best Practices – Slim Images

4 min read

Docker images are the building blocks of Docker containers. They are lightweight, executable packages of software that include everything needed to run an application: code, runtime, system tools, system libraries and settings.

Docker images are created using Dockerfiles. Dockerfiles are text files that contain a set of instructions that Docker uses to build images. Each instruction in a Dockerfile creates a new layer in the image. The final image is made up of all the layers, stacked on top of each other.

What factor decides the size of Docker Image?

The size of a Docker image is determined by the number of layers it has and the size of each layer. Larger images can be slower to build, deploy, and start. They can also be more expensive to store and transfer.

It is important to create slim Docker images to improve performance, reduce costs, and enhance security. Slim images are images that contain only the essentials needed to run an application.

Explore Docker Image Layers using Dive Tool

Dive is an open-source tool for exploring a Docker image and its layer contents, then discovering ways to shrink the size of your Docker/OCI image. Dive takes this information and does the following:

  • Breaks down the image contents in the Docker image layer by layer.
  • Shows the contents of each layer in detail.
  • Shows the total size of the image.
  • Shows how much space was potentially wasted.
  • Shows the efficiency score of the image.

Want to try Dive Tool? Enter Dive-In, a new Docker Extension that integrates Dive into Docker Desktop!

Best Practices for creating Slim Docker Images

Here are some best practices for creating slim Docker images:

1. Use a small base image

The base image is the first layer in a Docker image. It contains the operating system and other essential system files. There are many different base images available, including official images from Docker and images from third-party vendors.

When choosing a base image, it is important to select one that is as small as possible. For example, instead of using the general-purpose node:16.17 image(333 MB), consider using the slimmer node:lts-slim(62 MB) image.

2. Remove unnecessary packages

Docker images often include many packages that are not needed to run the application. These packages can add unnecessary size and complexity to the image.

To remove unnecessary packages, use the RUN instruction in your Dockerfile to uninstall them. For example, the following instruction uninstalls the apache2 package:

RUN apt-get remove -y apache2

You can also use the Alpine Linux distribution as a base image for your Docker images. Alpine Linux is a lightweight distribution that contains only the essential packages needed to run a system.

3. Use multi-stage builds

Multi-stage builds allow you to build multiple Docker images in a single Dockerfile. Each stage can have its own base image and set of instructions.

This can be useful for creating slim Docker images, as you can use a different base image for each stage. For example, you could use a large base image in the first stage to build your application, and then use a small base image in the second stage to create the final image.

To create a multi-stage build, use the FROM instruction to specify the base image for each stage. For example, the following Dockerfile creates a multi-stage build for a Python application:

FROM python:latest AS builder

WORKDIR /app

COPY . /app

RUN pip install -r requirements.txt

FROM python:alpine

WORKDIR /app

COPY --from=builder /app /app

CMD ["python", "app.py"]

In this example, the first stage uses the python:latest base image to build the application. The second stage uses the python:alpine base image to create the final image. The final image contains only the Python runtime and the application code.

4. Use caching

Docker caching can be used to reduce the time it takes to build Docker images. When you build a Docker image, Docker caches the layers that are created. On subsequent builds, Docker can reuse the cached layers, which can significantly improve the build time.

To use caching, Docker creates a unique hash for each layer. The hash is based on the contents of the layer and the instructions that were used to create it. When Docker builds an image, it compares the hash of each layer to the hash of the corresponding layer in the cache. If the hashes match, Docker reuses the cached layer.

You can use caching to reduce the size of your Docker images by only rebuilding the layers that have changed. To do this, use the –cache-from flag when you run the docker build command. For example, the following command builds an image using the cache from the latest image:

docker build --cache-from latest .

5. Use a Docker image registry

Docker image registries are used to store and distribute Docker images. There are many different Docker image registries available, including Docker Hub, Amazon Elastic Container Registry (ECR), and Google Container Registry (GCR).

Using a Docker image registry can help you to keep your Docker images slim.

When you push a Docker image to a registry, the registry stores a copy of the image. When you pull an image from a registry, the registry only sends you the layers that you don’t already have. This can save you bandwidth and storage space, especially if you are using a multi-stage build.

6. Secure Your Container Image using Docker Scout

According to Gartner, 45% of companies will be subject to a supply chain attack by 2025. Adopting a comprehensive approach to securing your software supply chain ensures you’re not included in that stat – but how?

Docker Scout comes to rescue. Docker Scout is a collection of software supply chain features that appear throughout Docker user interfaces and the command line interface (CLI). These features provide detailed insights into the composition and security of container images. Docker Scout is designed with developers in mind and integrated into Docker. With Docker Scout, spend less time searching for and fixing vulnerabilities, and more time developing your code. Docker is building Docker Scout to sit as a layer on top of the Docker ecosystem to help developers build and maintain a secure software supply chain.

Why Docker Scout?

First, Docker Scout offers developers analysis and context into components, libraries, tools, and processes, resulting in increased transparency of the software supply chain. Second, Docker Scout guides users toward smarter development decisions through context-aware recommendations, enabling developers to efficiently build applications with reliability and security in place from the start. Third, Docker Scout detects, highlights, and suggests corrections based on relevant changes in state or deviation of policies. Application security is ensured by providing suggestions to tackle security concerns before they hit production

Here are a few specific examples of how Docker Scout can be used to secure container images:

  • A developer can use Docker Scout to scan their local image for vulnerabilities before pushing it to a registry. This helps to ensure that the image is secure before it is deployed to production.
  • A security team can use Docker Scout to scan all of the images in a registry for vulnerabilities. This helps to identify any images that may be vulnerable to attack.
  • A system administrator can use Docker Scout to enforce security policies for container images. For example, they can use Docker Scout to ensure that all images are built from a trusted base image and that they do not contain any known vulnerabilities.

Docker Scout is a powerful tool that can help to improve the security of container images. By using Docker Scout, developers and system administrators can reduce the risk of their applications being compromised by vulnerabilities.

Conclusion

By following the best practices in this blog post, you can create slim Docker images that are smaller, faster, and more secure.

References

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Distinguished Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 700+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 9800+ members and discord server close to 2600+ members. You can follow him on Twitter(@ajeetsraina).

How to Generate Your SBOM from Docker Images

In today’s software landscape, understanding the components of your applications is crucial for security and compliance. A Software Bill of Materials (SBOM) provides a...
Ajeet Raina
2 min read
Join our Discord Server
Index