DEV Community

Krisha Arya
Krisha Arya

Posted on

๐Ÿš€ DevOps Project Journey: Docker + Jenkins Setup

๐Ÿ› ๏ธ Project Overview

This blog is a walkthrough of my hands-on experience in setting up a Jenkins automation pipeline using Docker. The aim was to containerize a project and integrate Jenkins for continuous integration and delivery (CI/CD). I will explain the entire process from Docker setup to Jenkins configuration, highlight key commands, and document the challenges and confusions I faced.


๐Ÿณ Docker Setup

๐Ÿ“ Step 1: Project Directory Structure

your-project/
โ”‚
โ”œโ”€โ”€ frontend/ (React App)
โ”‚   โ””โ”€โ”€ Dockerfile
โ”‚
โ”œโ”€โ”€ backend/ (Node.js App)
โ”‚   โ””โ”€โ”€ Dockerfile
โ”‚
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ Jenkinsfile

Enter fullscreen mode Exit fullscreen mode

โœ… Prerequisites Check
Ensure the following are installed:
-> Docker ๐Ÿณ
-> Docker Compose (docker compose version)
-> Jenkins (running as a container or installed locally)
-> Ports not already in use (check the ones in your docker-compose.yml)

๐Ÿงพ Step 2: Dockerfile

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 5002
CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode
FROM node:18-alpine as build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80 3002
CMD ["nginx", "-g", "daemon off;"]
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฐ Step 3: docker-compose.yml

version: "3.8"

services:
  backend:
    image: <your-image-name>
    ports:
      - "5002:5002" //any port u want

  frontend:
    image: <your-image-name>
    ports:
      - "3002:80"//any port you want
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Step 4: Run Docker

docker-compose up --build
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ Challenges Faced with Docker

  • Docker not recognizing updated files โ†’ resolved by rebuilding the image
  • Volume permissions โ†’ checked and fixed using chmod

๐Ÿ” Commands I Referenced

docker ps
docker stop <container_id>
docker rm <container_id>
docker volume ls
docker volume rm <volume_name>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ง Jenkins Setup with Docker

๐Ÿ› ๏ธ Step 1: Pull Jenkins Image and Run

docker run -d -p 8081:8080 -p 50000:50000 \
  -v jenkins_home:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  --name jenkins \
  jenkins/jenkins:lts
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ Step 2: Get Initial Admin Password

docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPassword
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ Restart Jenkins if Needed

docker restart jenkins
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›‘ Stop and Remove Jenkins

docker stop jenkins
docker rm jenkins
docker volume rm jenkins_home
Enter fullscreen mode Exit fullscreen mode

โš™๏ธ Jenkinsfile Example

pipeline {
  agent any

  stages {
    stage('Clone from GitHub') {
      steps {
        git branch: 'main', url: 'https://github.com/Aryagithubk/educart_docker.git'
      }
    }

    stage('Build Backend') {
      steps {
        sh 'docker build -t <your-image-name> ./backend'
      }
    }

    stage('Build Frontend') {
      steps {
        sh 'docker build -t <your-image-name> ./educart'
      }
    }

    stage('Push Images') {
  steps {
    sh 'echo "<password>" | docker login -u <username> --password-stdin'
    sh 'docker push <your-backend-image-name>'
    sh 'docker push <your-frontend-image-name>'
  }
}


    stage('Deploy to Swarm') {
      steps {
        sh 'docker-compose up --build -d'
      }
    }
  }
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿ˜ต Confusions & Debugging

  • Couldnโ€™t install nano inside Jenkins container โ†’ switched to vim
  • Permission denied while updating apt โ†’ switched to root user to install packages
  • Jenkins login loop โ†’ fixed by deleting and recreating Jenkins container/volume

๐Ÿ” Helpful Commands I Referred

docker exec -it jenkins bash
apt-get update
apt-get install -y vim
vim /var/jenkins_home/users/admin/config.xml
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  What I Learned

  • How to isolate and run services using Docker
  • CI/CD integration with Jenkins
  • Handling Jenkins configuration manually in files
  • Importance of clean volumes and environment consistency

๐Ÿ“ธ **

**
Your final jenkins dashboard will look like this. Now we need to configure this dashboard as well. I will tell about this next time.


๐Ÿงพ Conclusion

This was a complete experience from setting up a Dockerized environment to implementing a Jenkins CI/CD pipeline. While it came with its fair share of hurdles, it turned out to be a valuable learning process.


Thanks for reading! Bye Bye! ๐Ÿ™Œ

Top comments (0)