<p>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?</p>
<p>Although the docker build process prompts make to add the <code>-j</code> 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.</p>
<p><a href="https://github.com/kubernetes/ingress-nginx/blob/main/images/nginx/rootfs/Dockerfile" rel="nofollow noreferrer">Source</a> of the dockerfile.</p>

<p>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.</p>
<hr />
<p>There are a few tricks you can use to speed up building Docker images.
First I will present you solution from <a href="https://cloud.google.com/build/docs/speeding-up-builds#using_a_cached_docker_image" rel="nofollow noreferrer">Google cloud</a>:</p>
<blockquote>
<p>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 <code>--cache-from</code> argument in your build config file, which will instruct Docker to build using that image as a cache source.</p>
</blockquote>
<p>You can read more here about <a href="https://docs.semaphoreci.com/ci-cd-environment/docker-layer-caching/" rel="nofollow noreferrer">Docker Layer Caching</a>.</p>
<p>Another way is <a href="https://vsupalov.com/5-tips-to-speed-up-docker-build/#tip-2-structure-your-dockerfile-instructions-like-an-inverted-pyramid" rel="nofollow noreferrer">Structure your Dockerfile instructions like an inverted pyramid</a>:</p>
<blockquote>
<p>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:</p>
<ul>
<li>All previous layers are unchanged.</li>
<li>In case of COPY instructions: the files/folders are unchanged.</li>
<li>In case of all other instructions: the command text is unchanged.</li>
</ul>
<p>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.</p>
</blockquote>
<p>You can also <a href="https://vsupalov.com/5-tips-to-speed-up-docker-build/#tip-2-structure-your-dockerfile-instructions-like-an-inverted-pyramid" rel="nofollow noreferrer">Only copy files which are needed for the next step</a>.</p>
<p>Look at these great tutorials about speeding building your Docker images:</p>
<p>-<a href="https://vsupalov.com/5-tips-to-speed-up-docker-build" rel="nofollow noreferrer">5 Tips to Speed up Your Docker Image Build</a>
-<a href="https://www.docker.com/blog/speed-up-your-development-flow-with-these-dockerfile-best-practices/" rel="nofollow noreferrer">Speed Up Your Development Flow With These Dockerfile Best Practices</a>
-[Six Ways to Build Docker Images Faster (Even in Seconds)](# Six Ways to Build Docker Images Faster (Even in Seconds)</p>
<p>At the end I will present you one more method described here - <a href="https://tsh.io/blog/speed-up-your-docker-image-build/" rel="nofollow noreferrer">How to speed up your Docker image build?</a> You can you a tool Buildkit.</p>
<blockquote>
<p>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!</p>
</blockquote>
<blockquote>
<p>The difference is about 18 seconds on an image that builds in the 70s. That’s a lot, almost 33%!</p>
</blockquote>
<p>Hope it helps ;)</p>
