Effortless CI/CD for Node.js and Angular Apps: A Jenkins Pipeline with Docker
Continuous Integration and Continuous Deployment (CI/CD) is a game-changer for modern web development. When you combine Docker with Jenkins, you supercharge your CI/CD pipeline. In this article, we will explore how to set up a Jenkins pipeline for a Node.js and Angular project, incorporating Docker for seamless containerized builds and deployments.

Introduction
Node.js and Angular are at the forefront of web application development. With Docker and Jenkins, we can create an efficient and consistent CI/CD pipeline. Let’s dive into the process with practical code examples.
Preparing Your Project
Before we dive into Jenkins, make sure your Node.js and Angular project is ready for CI/CD with Docker. Create a Dockerfile
in your project root directory to build your application's image.
# Use an official Node.js runtime as a parent image
FROM node:14
# Set the working directory to /app
WORKDIR /app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install app dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME my-angular-app
# Run app when the container launches
CMD ["npm", "start"]
Setting Up Jenkins Pipeline
- Install Jenkins: If you haven’t already, install Jenkins and the necessary plugins, including the Docker plugin.
- Create a Jenkinsfile: In your project repository, create a file named
Jenkinsfile
. This file defines your Jenkins pipeline. Here's a simplified example:
pipeline {
agent {
docker {
image 'node:14' // Use a Node.js image
args '-p 3000:80' // Map the app's port to host
}
}
stages {
stage('Build') {
steps {
sh 'npm install' // Install project dependencies
}
}
stage('Test') {
steps {
sh 'npm test' // Run tests
}
}
stage('Build Docker Image') {
steps {
script {
docker.build("my-angular-app:${env.BUILD_NUMBER}")
}
}
}
stage('Deploy') {
steps {
script {
docker.withRegistry('https://your-docker-registry', 'docker-hub-credentials') {
docker.image("my-angular-app:${env.BUILD_NUMBER}").push()
}
}
}
}
}
}
This Jenkinsfile specifies a multi-stage pipeline that includes building the Node.js project, running tests, building a Docker image, and deploying it to a Docker registry.
- Jenkins Configuration: Configure Jenkins to connect to your version control system (e.g., GitHub) and set up a webhook to trigger the pipeline on code changes.
- Docker Registry Credentials: You’ll need Docker registry credentials to push your Docker image. Create a Docker Hub account and generate API credentials, which you should add to Jenkins.
With this Jenkins pipeline in place, every code commit or pull request will trigger a build, test, and deployment process.
Conclusion
Combining Docker and Jenkins for CI/CD in your Node.js and Angular project enhances your development workflow, making it consistent and efficient. In this article, we have shown how to set up a Jenkins pipeline with Docker for building, testing, and deploying your application. By following these steps, you can ensure that your applications are always ready for prime time.
In this article, we have:
- Prepared your Node.js and Angular project with a Dockerfile.
- Created a Jenkins pipeline using a Jenkinsfile with practical code examples.
- Demonstrated how to configure Jenkins and Docker Hub credentials for a smooth CI/CD workflow.
