Node, NPM, and NPX in Docker
Overview
During the deployment of a Node.js project within a Jenkins container running inside Docker, we encountered several issues related to the installation and execution of Node.js, NPM, and NPX. This document provides an overview of the challenges faced and a structured approach to resolving them.
Issues Faced
1. Node.js, NPM, and NPX Not Found in Jenkins Container
When executing commands like node --version, npm --version, and npx --version inside the Jenkins container, they were not found. This happened because:
- The default Jenkins image does not include Node.js.
- The Jenkins container was built without including Node.js and its package manager.
2. Incorrect Node.js Version
After installing Node.js, the version was outdated (v16.x), while the project required at least v18.x. This led to build failures with messages like:
[ERROR] Minimum Node.js version not met :(
[INFO] You are using Node.js v16.20.2, Requirement: Node.js >=18.0.
3. Port 3004 Not Accessible
Even after starting the server, the application was not accessible via localhost:3004. Checking running processes showed multiple instances binding to the same port, potentially causing conflicts.
Step-by-Step Solution
Step 1: Modify the Dockerfile to Include Node.js
Since the default Jenkins image does not include Node.js, we modified the Dockerfile to install the required Node.js version.
FROM jenkins/jenkins:lts
USER root
# Install Node.js and NPM
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - \
&& apt-get install -y nodejs \
&& npm install -g npx
USER jenkins
Step 2: Update docker-compose.yml to Use the Custom Image
We ensured that our docker-compose.yml referenced the custom Dockerfile instead of pulling the default Jenkins image.
version: '3.8'
services:
jenkins:
build:
context: .
dockerfile: Dockerfile
privileged: true
user: root
ports:
- 8083:8080
- 50000:50000
- 3003:3003
- 3004:3004
container_name: jenkins
volumes:
- ./jenkins/:/var/jenkins_home
- ./shared/:/shared
Step 3: Rebuild and Restart the Jenkins Container
To apply the changes, we rebuilt and restarted the Jenkins container using:
docker compose up --build -d
Step 4: Verify Node.js Installation
Once the container was running, we accessed it and verified the installations:
docker exec -it jenkins /bin/bash
which node
which npm
which npx
Output:
/usr/bin/node
/usr/bin/npm
/usr/bin/npx
Step 5: Ensure the Correct Node.js Version
To verify the installed Node.js version:
node --version
Output:
v18.16.0
Step 6: Fix Port 3004 Issue
To check if the port was already in use, we ran:
lsof -i :3004
If multiple processes were found, we killed them using:
npx kill-port 3004
Then restarted the application:
npm run start -- --host 0.0.0.0 --port 3004 &
Note:
- The
&at the end ensures the process runs in the background. - If the application still hangs, check for logs or errors preventing it from properly binding to the port.
Conclusion
By modifying the Dockerfile, ensuring the correct Node.js version, and managing process conflicts on port 3004, we successfully resolved the deployment issues. These steps provide a structured approach for handling similar issues in Dockerized environments running Node.js applications.