2

Sometimes we face some nginx vulnerabilities, so we need to fix the nginx vulnerabilities inside ingress-nginx, but the docker build -t image is too slow. The reason is that the dockerfile internal will make compile and make install process. How to add some parameters can make the docker build process faster?

Although the docker build process prompts make to add the -j parameter to increase threads to speed up the process, there is no make related parameter inside the dockerfile. It is not a good idea to modify the dockerfile directly.

Source of the dockerfile.

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
auggie321
  • 41
  • 5

2 Answers2

1

There is no one good solution on how to speed up the building of a Docker image. This may depend on a number of things. That is why I am posting the answer of the community wiki to present as many solution proposals as possible, referring to various tutorials.


There are a few tricks you can use to speed up building Docker images. First I will present you solution from Google cloud:

The easiest way to increase the speed of your Docker image build is by specifying a cached image that can be used for subsequent builds. You can specify the cached image by adding the --cache-from argument in your build config file, which will instruct Docker to build using that image as a cache source.

You can read more here about Docker Layer Caching.

Another way is Structure your Dockerfile instructions like an inverted pyramid:

Each instruction in your Dockerfile results in an image layer being created. Docker uses layers to reuse work, and save bandwidth. Layers are cached and don’t need to be recomputed if:

  • All previous layers are unchanged.
  • In case of COPY instructions: the files/folders are unchanged.
  • In case of all other instructions: the command text is unchanged.

To make good use of the Docker cache, it’s a good idea to try and put layers where lots of slow work needs to happen, but which change infrequently early in your Dockerfile, and put quickly-changing and fast layers last. The result is like an inverted pyramid.

You can also Only copy files which are needed for the next step.

Look at these great tutorials about speeding building your Docker images:

-5 Tips to Speed up Your Docker Image Build -Speed Up Your Development Flow With These Dockerfile Best Practices -[Six Ways to Build Docker Images Faster (Even in Seconds)](# Six Ways to Build Docker Images Faster (Even in Seconds)

At the end I will present you one more method described here - How to speed up your Docker image build? You can you a tool Buildkit.

With Docker 18.09 ,a new builder was released. It’s called Buildkit. It is not used by default, so most of us are still using the old one. The thing is, Buildkit is much faster, even for such simple images!

The difference is about 18 seconds on an image that builds in the 70s. That’s a lot, almost 33%!

Hope it helps ;)

Mikołaj Głodziak
  • 4,775
  • 7
  • 28
  • Thank you very much for your patience and explanation, I appreciate it. I'm using a multi-stage image locally, but in order to fix the vulnerability, the build nginx image itself takes more time to compile inside the image, this part can't be bypassed using multi-stage as far as I know because of the official patch, the other parts are very fast. I will try buildkit. – auggie321 Oct 26 '21 at 10:06
  • @auggie321, please [accept or upvote](https://stackoverflow.com/help/someone-answers) the answer if it was helpful. – Mikołaj Głodziak Oct 27 '21 at 08:54
0

enter image description here

docker version: docker-ce.20.10

Actual testing of images made by buildkit
may actually do little to improve the efficiency of making images if they involve a lot of compilation processes internally.
Maybe based on the official alpine base image,
consider add the -j nproc parameter in dockerfile to increase the threads, would be a good direction.

auggie321
  • 41
  • 5
  • 3
    [Please don't post screenshots of text](https://meta.stackoverflow.com/a/285557/354577). They can't be searched or copied, or even consumed by users of adaptive technologies like screen readers. Instead, paste the code as text directly into your question. If you select it and click the `{}` button or Ctrl+K the code block will be indented by four spaces, which will cause it to be rendered as code. – ChrisGPT was on strike Oct 28 '21 at 01:40
  • Thanks for the reminder, will pay attention to this stuff later. – auggie321 Nov 05 '21 at 05:53