How to optimise the Docker build process in JenkinsPipeline How to optimise the Docker build process in JenkinsPipeline jenkins jenkins

How to optimise the Docker build process in JenkinsPipeline


You can use combination of base images and multi stage builds to speed up your builds.

Base image with pre-installed packages/dependencies

Stuff like installing python3, pip, google-chrome, awscli etc need not be done every build. These layers might get cached if you are building on single machine but if you have multiple build machines or clean the cache, you will be re-building these layers unnecessarily. You can build a base image which already has these stuff and use this new image as the base for your app.

Multi stage builds

You are copying your source code and then doing npm install. Even if package.json has not changed, the layer will be re-built if any other file in source code might have changed.

You can create a multi stage dockerfile where you just copy the package.json in the first stage and run npm install and other such commands. This layer will be re-built only if package.json is changed.

In your second stage, you can just copy the npm cache from first stage.

FROM node:lts as dev-builderWORKDIR /cache/COPY package.json .RUN npm installRUN npm run build-devFROM NEW_BASE_IMAGE_WITH_CHROME_ETC_DEPENDENCIESCOPY --from=node_cache /cache/ .COPY . . <snip>

Identify any other such optimisations that you can make.