<p>I am running my monolith application in a docker container and k8s on GKE.</p>

<p>The application contains python &amp; node dependencies also webpack for front end bundle.  </p>

<p>We have implemented CI/CD which is taking around 5-6 min to build &amp; deploy new version to k8s cluster.</p>

<p>Main goal is to reduce the build time as much possible. Written Dockerfile is multi stage.</p>

<p>Webpack is taking more time to generate the bundle.To buid docker image i am using already high config worker.</p>

<p>To reduce time i tried using the <a href="https://cloud.google.com/blog/products/gcp/introducing-kaniko-build-container-images-in-kubernetes-and-google-container-builder-even-without-root-access" rel="nofollow noreferrer">Kaniko builder</a>.</p>

<p>Issue :</p>

<p>As docker cache layers for python code it's working perfectly. But when there is any changes in <code>JS</code> or <code>CSS</code> file we have to generate bundle.</p>

<p>When there is any changes in <code>JS</code> &amp; <code>CSS</code> file instead if generate new bundle its use caching layer.</p>

<p>Is there any way to separate out build new bundle or use cache by passing some value to docker file.</p>

<p>Here is my docker file : </p>

<pre><code>FROM python:3.5 AS python-build
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt  &amp;&amp;\
    pip3 install Flask-JWT-Extended==3.20.0
ADD . /app

FROM node:10-alpine AS node-build
WORKDIR /app
COPY --from=python-build ./app/app/static/package.json app/static/
COPY --from=python-build ./app ./
WORKDIR /app/app/static
RUN npm cache verify &amp;&amp; npm install &amp;&amp; npm install -g --unsafe-perm node-sass &amp;&amp; npm run sass  &amp;&amp; npm run build


FROM python:3.5-slim
COPY --from=python-build /root/.cache /root/.cache
WORKDIR /app
COPY --from=node-build ./app ./
RUN apt-get update -yq \
    &amp;&amp; apt-get install curl -yq \
    &amp;&amp; pip install -r requirements.txt
EXPOSE 9595
CMD python3 run.py
</code></pre>

<p>I would suggest to create separate build pipelines for your docker images, where you know that the requirements for npm and pip aren't so frequent.
This will incredibly improve the speed, reducing the time of access to npm and pip registries.</p>

<p>Use a private docker registry (the <a href="https://docs.docker.com/registry/deploying/" rel="nofollow noreferrer">official one</a> or something like <a href="https://goharbor.io/" rel="nofollow noreferrer">VMWare harbor</a> or <a href="https://www.sonatype.com/download-oss-sonatype" rel="nofollow noreferrer">SonaType Nexus OSS</a>).</p>

<p>You store those build images on your registry and use them whenever something on the project changes.</p>

<p>Something like this:</p>

<p><strong>First Docker Builder</strong> // <strong>python-builder:YOUR_TAG</strong> [gitrev, date, etc.)</p>

<pre><code>docker build --no-cache -t python-builder:YOUR_TAG -f Dockerfile.python.build .

FROM python:3.5 
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt  &amp;&amp;\
    pip3 install Flask-JWT-Extended==3.20.0
</code></pre>

<p><strong>Second Docker Builder</strong> // <strong>js-builder:YOUR_TAG</strong> [gitrev, date, etc.)</p>

<pre><code>docker build --no-cache -t js-builder:YOUR_TAG -f Dockerfile.js.build .  

FROM node:10-alpine
WORKDIR /app
COPY app/static/package.json /app/app/static
WORKDIR /app/app/static
RUN npm cache verify &amp;&amp; npm install &amp;&amp; npm install -g --unsafe-perm node-sass 
</code></pre>

<p>Your <strong>Application Multi-stage build</strong>:</p>

<pre><code>docker build --no-cache -t app_delivery:YOUR_TAG -f Dockerfile.app .  

FROM python-builder:YOUR_TAG as python-build
# Nothing, already "stoned" in another build process

FROM js-builder:YOUR_TAG AS node-build
ADD ##### YOUR JS/CSS files only here, required from npm! ###
RUN npm run sass &amp;&amp; npm run build

FROM python:3.5-slim
COPY . /app # your original clean app
COPY --from=python-build #### only the files installed with the pip command
WORKDIR /app
COPY --from=node-build ##### Only the generated files from npm here! ###
RUN apt-get update -yq \
    &amp;&amp; apt-get install curl -yq \
    &amp;&amp; pip install -r requirements.txt
EXPOSE 9595
CMD python3 run.py
</code></pre>

<p>A question is: why do you install <code>curl</code> and execute again the <code>pip install -r requirements.txt</code> command in the final docker image? 
Triggering every time an <code>apt-get update</code> and install <em>without</em> cleaning the apt cache <code>/var/cache/apt</code> folder produces a bigger image.</p>

<p>As suggestion, use the docker build command with the option <strong>--no-cache</strong> to avoid caching result:</p>

<pre><code>docker build --no-cache -t your_image:your_tag -f your_dockerfile .
</code></pre>

<p><strong>Remarks</strong>:</p>

<p>You'll have 3 separate Dockerfiles, as I listed above.
Build the Docker images 1 and 2 <strong>only</strong> if you change your <code>python-pip</code> and <code>node-npm</code> requirements, otherwise keep them fixed for your project.
If any dependency requirement changes, then update the docker image involved and then the multistage one to point to the latest built image.</p>

<p>You should always build only the source code of your project (CSS, JS, python). In this way, you have also guaranteed reproducible builds.</p>

<p>To optimize your environment and copy files across the multi-stage builders, try to use <code>virtualenv</code> for python build.</p>
