In this article, let’s discuss how to run a Docker build faster with a simple tip.

The docker build command took approximately 53 seconds to run.

On a side note, you can calculate the time taken to run any command in Linux/Mac by appending the word time before your command

> time docker build -t my-img .

The Nginx image we used to build our Docker image is just 20.6MB in size. Why is it taking 53 seconds to build such a small Docker image?

The culprit here is the build context.

Understanding the Build Context

In the docker build -t my-img . command, you might have noticed the . (dot) at the end, which implies:

  • The Dockerfile resides in the current directory
  • The build context is set in the current directory

The Docker build command will first compress and create a TAR file of everything inside Build Context’s directory. Then that file will be sent to the Docker daemon to build the image.

Since we kept the Dockerfile in the project root directory, the build command is creating a TAR file of the entire root directory of the project (including the node_modules folder).

That’s why we are seeing “Sending build context to Docker daemon 378.9MB” in the output, meaning 378.9MB are transferred to the Docker daemon to build the Docker image.

Note: The Docker daemon could be running either in our local system or on a remote server. If it’s remote, we will be sending 378 MB data to the Docker daemon on a remote server for every single build command.

How to Avoid This

Don’t create the Dockerfile in the project root directory! Always create the Dockerfile or docker-compose file inside the Docker folder.

What’s the Catch With This Solution?

Dockerfile can only refer to files/folders available in its current directory or its subdirectories.

In our Angular Application, we need content from the dist folder to build our Docker image. The dist folder usually gets generated in the root directory by the ng build command.

If we moved the Dockerfile to the Docker folder, we can’t refer to the dist folder from Dockerfile.

How to Fix This

In the angular.json file, change “outputPath”: “dist/my-app” to “outputPath”: “Docker/dist/my-app”

This will ensure the dist folder gets generated inside the Docker folder all the time.

#cloud #tutorial #docker #dockerfile #docker build

How to Make Docker Build Run Faster
1.15 GEEK