How to Use Docker in Jenkins Declarative Pipeline | #Day 27 | #90 Days of Dev Ops
What is Docker?
Docker is a platform and tool designed to make it easier to create, deploy, and run applications in isolated environments called containers. Containers are lightweight, portable, and self-sufficient units that include everything needed to run an application, including the code, runtime, libraries, and system tools. Docker uses containerization technology to achieve this.
How to Use Docker Commands in Jenkins Declarative Pipeline?
To use Docker commands in the Jenkins declarative pipeline, you need to have:
Docker is installed on your machine or server where Jenkins runs.
A Dockerfile that defines how to build your image for your application.
A Jenkinsfile that defines your pipeline using the declarative syntax.
In this tutorial, I will use the same Node.js app that I used in the previous blog post. The app is a simple to-do app. The source code for the app is available on GitHub: [https://github.com/avanishnit08/node-todo-cicd.git]
The Dockerfile for the app looks like this:
# Use an official Node.js runtime as a parent image
FROM node:14
# Set the working directory in the container
WORKDIR /app
# Copy package.json and package-lock.json to the container
COPY package*.json ./
# Install application dependencies
RUN npm install
# Copy the rest of the application code to the container
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Define the command to run your Node.js application
CMD ["node", "app.js"]
This Dockerfile will create a Docker image that contains the Node.js runtime environment and your Node.js application. The FROM
instruction specifies the base image that will be used to build your image. In this case, we are using the official Node.js Docker image.
The WORKDIR
instruction sets the working directory for the image. The COPY
instruction copies files from your local machine to the image. The EXPOSE
instruction exposes port 3000, which is the port that your Node.js application will be listening on. The CMD
instruction specifies the command that will be executed when the image is run.
Docker commands used in this project
docker build
: Builds a Docker image from the Dockerfile.docker run
: Runs a Docker image.docker push
: Pushes a Docker image to a registry.
Docker-integrated Jenkins declarative pipeline looks like this:
Groovy
pipeline {
agent any
stages {
stage('Checkout') {
steps {
// Check out your source code from version control (e.g., Git)
// Use the appropriate SCM plugin for your version control system
// Example for Git:
checkout scm
}
}
stage('Build and Package') {
steps {
// Build and package your Node.js application
sh 'npm install'
}
}
stage('Build Docker Image') {
steps {
// Build a Docker image for your Node.js application
script {
def appImage = docker.build("your-node-app:latest", "-f Dockerfile .")
}
}
}
stage('Push Docker Image') {
steps {
// Push the Docker image to a container registry
script {
docker.withRegistry('https://registry.example.com', 'your-registry-credentials') {
def appImage = docker.image("your-node-app:latest")
appImage.push()
}
}
}
}
stage('Deploy') {
steps {
// Deploy your Dockerized Node.js application (e.g., using Kubernetes, Docker Compose, etc.)
// Add your deployment scripts/commands here
}
}
}
post {
always {
// Clean up by removing the Docker image locally (optional)
cleanWs()
}
}
}
This Jenkins declarative pipeline has the following stages:
Checkout: Check out your source code from the version control system.
Build and Package: Installs Node.js application dependencies.
Build Docker Image: Build a Docker image using the Dockerfile for your Node.js app.
Push Docker Image: Pushes the Docker image to a container registry. Replace
'
https://registry.example.com
'
with your actual container registry URL and'your-registry-credentials'
with your registry credentials.Deploy: This is where you would deploy your Dockerized Node.js application to your target environment. You'll need to add your deployment scripts or commands here.
Post: Performs optional cleanup, removing the Docker image locally.
Make sure to configure Jenkins with the necessary Docker and pipeline plugins to enable this pipeline to work. Additionally, customize the pipeline to fit your specific needs and deployment environment.
To create and run this pipeline, follow these steps:
Go to your Jenkins dashboard and click on New Item.
Enter a name for your pipeline (e.g., Node Todo App with Docker) and select Pipeline. Then click OK.
On the configuration page, scroll down to the Pipeline section and select Pipeline script from SCM from the Definition drop-down menu.
Select Git from the SCM drop-down menu and enter the repository URL: [https://github.com/avanishnit08/node-todo-cicd.git]. You can leave the other fields as default.
Under Build Triggers, select the GitHub hook trigger for GITScm polling. This will enable the pipeline to be triggered by GitHub webhooks.
Click Save to save your pipeline.
Now, whenever you push any changes to your GitHub repository, your pipeline will be triggered automatically, and build and run your app using Docker.
You can also manually trigger your pipeline by clicking on Build Now on your Jenkins dashboard.
You can see the status and details of your pipeline under Build History. You can also click on Console Output to see the logs of your pipeline.
You can verify that your app is running by going to your browser and typing http://localhost:3000
. You should see a to-do app where you can add, edit, and delete tasks.
Here is an example of a docker-integrated Jenkins declarative pipeline using the Docker
groovy syntax inside the stage block:
Groovy
pipeline {
agent any {
docker {
image 'node:14'
}
}
stages {
stage('Build') {
docker.image 'node:14'
steps {
sh 'npm install'
sh 'npm run build'
}
}
stage('Test') {
docker.image 'node:14'
steps {
sh 'npm run test'
}
}
stage('Deploy') {
docker.image 'node:14'
steps {
sh 'docker build -t my-node-app:latest . '
sh 'docker push my-node-app:latest'
}
}
}
post {
always {
archiveArtifacts artifacts: '**/*.zip'
}
}
}
This pipeline will build, test, and deploy your Node.js application to a Docker registry. The docker
syntax is used in each stage to specify the Docker image that will be used to execute the steps in that stage.
This approach has several benefits, including:
It allows you to isolate the build, test, and deploy processes in different Docker containers. This can help to improve the security and reliability of your pipeline.
It allows you to use the same Docker images across different stages of your pipeline. This can help to simplify your pipeline and make it easier to maintain.
It allows you to use the latest Docker images for each stage of your pipeline. This can help to ensure that your pipeline is always using the most up-to-date tools and technologies.
Overall, using the docker
groovy syntax inside the stage block is a good practice for creating docker-integrated Jenkins declarative pipelines. It can help to improve the security, reliability, simplicity, and maintainability of your pipelines.
I hope this blog post helps you understand how to use Docker in the Jenkins declarative pipeline. In the next blog post, I will show you how to use different types of stages, steps, directives, and options in the Jenkins declarative pipeline.
If you have any questions or feedback, please feel free to connect with me on:
LinkedIn: https://www.linkedin.com/in/avanishsinghnitap/
GitHub: https://github.com/avanishnit08
Thank You!!!