Jenkins & Docker

Many companies use Jenkins as their automation server of choice. and use Docker as their preferred way to ship their applications.
CI/CD becoming a more popular thing every day due to the worldwide promotion of DEVOPS I wanted to create a simple tutorial where we put these two technologies together and see if we can arrange a beautiful marriage.


Step 1: Install docker & docker-compose:

sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common
curl -fsSL | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli
sudo groupadd docker
sudo usermod -aG docker $USER
sudo systemctl enable docker
sudo curl -L --fail -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo reboot now

Step 2: Install Jenkins.

Create Dockerfile

mkdir jenkins
cd jenkins
nano Dockerfile
FROM jenkins/jenkins:lts

USER root
RUN apt-get update \
      && apt-get install -y sudo \
      && rm -rf /var/lib/apt/lists/*
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers

USER jenkins

Create docker-compose.yml

nano docker-compose.yml 
version: '2'
    build: .
    image: jenkinswithdockerbuild:0.1
    container_name: jenkinswithdocker
    image: jenkinswithdockerbuild:0.1
     - "8080:8080"
     - "50000:50000"
    network_mode: "bridge"
    restart: always
      - "jenkins_home:/var/jenkins_home"
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker

Run container

docker-compose up -d 

Get the admin password for Jenkins

docker exec jenkinswithdocker cat /var/jenkins_home/secrets/initialAdminPassword

Go to the Jenkins WebUI and finish the installation

Create a demo project in Gitlab


pipeline {
    agent none
    stages {
        stage("Fix the permission issue") {
            agent any
            steps {
                sh "sudo chown root:jenkins /run/docker.sock"
        stage('Step 1') {
            agent {
                docker {
                    image 'node:7-alpine'
            steps {
                sh 'node --version'

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s