Open In App

How To Use Dockerfile Best Practices for Efficient Image Building?

Docker is an open-source platform that enables developers to build, distribute, operate, update, and manage containers.

What is Dockerfile?

A simple example of a Dockerfile could be this:



FROM registry.aws.site.com:443

RUN microdnf install wget



RUN wget -c project-with-dependencies.jar

COPY ./package.json

RUN npm install

RUN npm run build

This dockerfile will execute all the steps mentioned in the file and will eventually run the npm application. To better understand the file, we should know the Dockerfile commands i.e., FROM, RUN, CMD, COPY, etc.

Explaining them with respect to this example:

Best Practices For Efficient Image Building

Some of the best practices for creating a Dockerfile which optimizes image building and increases efficiency are as follows:

 Examples of Best Practices In Action

A multi-stage build that uses a lightweight base image: to build a production-ready image. To utilize multi-stage builds, we can use numerous FROM statements in our Dockerfile. Each FROM command can start a new stage of the build and therefore will use a different base. This also helps to handle multiple environments like dev, QA, UAT, and PROD. An example of the same could be this:

FROM golang:alpine AS dev
COPY --from=dev /binary/bin

FROM golang:alpine AS uat
COPY --from=uat /binary/bin

FROM golang:alpine AS prod
COPY --from=prod /binary/bin
RUN go test

A lightweight base image that is optimized for production use: Using a lightweight base image can help reduce the size of the image and container to a lot of extents (even 90% sometimes). This could be achieved by using specific images and not the original ones like using an alpine-based image instead of the original.

Using this:
FROM golang:alpine
Instead of this:
FROM golang

A caching strategy: that uses a Dockerfile to cache dependencies and speed up the build process. To understand the importance of caching, we can look at it this way – all the Dockerfile commands create a new layer in the image. Suppose we change something in any command, that in turn affects that layer and it needs to rebuild. And whichever layer rebuilds, all the layers below it also need to rebuild whether or not something changed for them. To optimize this, we use a caching strategy.

1. Using smaller layers that do not include any unnecessary files and trying to combine the RUN command in whatever ways possible.

Instead of this:
RUN microdnf install wget
RUN wget -c project-with-dependencies.jar
Use this:
RUN microdnf install wget && wget -c project-with-dependencies.jar

2. Ordering the layers properly so as to not include anything later which is not affected by the initial commands.

3. Using multistage builds like those mentioned above also helps here.

Conclusion

In conclusion, we have seen what a Dockerfile is, how we use it, and what advantages we get by using it. We then saw some of the best practices that should be followed while using the Dockerfile to have efficient image building.

By following these best practices, you can build efficient Docker images that are smaller, faster, and more secure.

We should be able to use its functionality in an optimum way by following good practices. This could also be achieved by going through some official Dockerfiles and understanding them.


Article Tags :