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.
Prerequisites:
- Freshly installed linux server. (example: Debian 9.8.0)
- Gitlab account
- Gitlab access token
Step 1: Install docker & docker-compose:
sudo apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io sudo groupadd docker sudo usermod -aG docker $USER sudo systemctl enable docker sudo curl -L --fail https://github.com/docker/compose/releases/download/1.23.2/run.sh -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' services: jenkinswithdockerbuild: build: . image: jenkinswithdockerbuild:0.1 jenkinswithdocker: container_name: jenkinswithdocker image: jenkinswithdockerbuild:0.1 ports: - "8080:8080" - "50000:50000" network_mode: "bridge" restart: always volumes: - "jenkins_home:/var/jenkins_home" - /var/run/docker.sock:/var/run/docker.sock - /usr/bin/docker:/usr/bin/docker volumes: jenkins_home:
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
- http://192.168.1.228:8080
- Create a new item -> Multibranche pipeline
Create a demo project in Gitlab
Jenkinsfile
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' } } } }