Running Multiple Node.js Versions in CI with a Single Dockerfile and Jenkins
This guide demonstrates how to use a single Dockerfile and Jenkins to test a Node.js project against multiple Node versions in a CI pipeline, leveraging Docker build arguments, custom scripts, and volume caching to simplify environment management without installing numerous tools.
Using a single Dockerfile to run multiple Node.js versions in CI eliminates the need to install dozens of tools or launch full virtual machines on the CI host; installing Docker alone is sufficient for most programs.
The article explains how to test a project against different Node.js versions and achieve this in Jenkins.
Assuming you have a Jenkins instance that can run Docker, you can start it with a command like:
docker run --name jenkins -itd \
-p 8081:8080 \
-p 50000:50000 \
-v ~/jenkins:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/local/bin/docker:/usr/bin/docker \
jenkins/jenkins:ltsThe project structure is simple:
├── package-lock.json
├── package.json
└── src
├── file.js
└── file.test.jsThe project uses npm install to install exact dependency versions from the lock file, and the source code and tests reside in the src folder, with the test task defined in package.json.
To test the project for a specific Node version, you can build an image using the following Dockerfile:
FROM node:12.14
ENV NODE_VERSION=12.14
RUN mkdir -p /app/src
WORKDIR /app
ENV HOME=/app
COPY package.json package-lock.json ./
RUN npm install --ci
COPY src src
CMD npm testThen configure a Jenkins job to run these commands:
docker build -t node-ci:12.14 .
docker run -it --rm node-ci:12.14The Dockerfile can be adjusted as needed; you may mount a volume for node_modules or avoid copying code. The official Node image already sets the NODE_VERSION environment variable, so the explicit ENV NODE_VERSION line is optional.
Multiple Node Versions
Each project should define the Node versions to test. Modify the Dockerfile to accept a build argument that specifies the base image:
ARG NODE_VERSION
FROM node:$NODE_VERSION
ARG NODE_VERSION
ENV NODE_VERSION=$NODE_VERSION
RUN mkdir -p /app/src
WORKDIR /app
ENV HOME=/app
COPY package.json package-lock.json ./
RUN npm install --ci
COPY src src
CMD npm testA script can iterate over the defined versions, passing the NODE_VERSION build argument to build and run each image:
#!/bin/bash -e
versions="10.16 12.14"
for version in $versions; do
echo "========== node $version ==========="
docker build -t node-ci:$version --build-arg NODE_VERSION=$version .
docker run -it --rm node-ci:$version
doneThis approach enables a single Dockerfile to run CI jobs for multiple Node versions. Using Docker containers in Jenkins makes configuring and running CI environments highly flexible, and the same ARG + FROM pattern can be applied to other languages or technologies.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
