A Step-by-step Guide to Setting up Ci/cd with Docker and Jenkins

Continuous Integration and Continuous Deployment (CI/CD) are essential practices in modern software development. They help teams deliver code faster and more reliably. This guide walks you through setting up a CI/CD pipeline using Docker and Jenkins, two popular tools in the DevOps ecosystem.

Prerequisites

  • Basic knowledge of Docker and Jenkins
  • Docker installed on your machine
  • Jenkins installed and running
  • A sample application to deploy

Step 1: Set Up Your Docker Environment

First, ensure Docker is installed and running on your system. Create a Dockerfile for your application if you haven’t already. This file defines how your application will be built and run inside a container.

Example Dockerfile:

FROM python:3.8-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Step 2: Configure Jenkins

Open Jenkins and install the Docker plugin if you haven’t already. Create a new pipeline job and configure it to pull your code from your repository.

In the pipeline script, define stages for building the Docker image, testing, and deploying.

Sample Jenkins Pipeline Script

pipeline {
agent any
stages {
stage('Build') {
steps {
script {
docker.build('myapp:latest')
}
}
}
stage('Test') {
steps {
// Add testing commands here
}
}
stage('Deploy') {
steps {
script {
docker.withRegistry('https://registry.hub.docker.com', 'docker-credentials') {
docker.image('myapp:latest').push()
}
}
}
}
}
}

Step 3: Automate the Pipeline

Configure your repository to trigger Jenkins builds automatically on code commits. This can be done using webhooks or polling options in Jenkins.

Ensure your Docker registry credentials are securely stored in Jenkins to enable image pushing during deployment.

Conclusion

Setting up a CI/CD pipeline with Docker and Jenkins streamlines your development process, enabling faster delivery and more reliable releases. By following these steps, you can create an automated workflow tailored to your project’s needs.