Install Jenkins On Docker Container

/ Comments off

In this video, we will discuss How To Install Jenkins Using Docker ContainerLinkshttps://hub.docker.com//jenkinshttps://hub.docker.com/r/jenkins/jenkinsDocu. In here we use docker-compose command to up our Jenkins container. Then check the status of Jenkins container. Finally, list our customized Jenkins image. Docker-compose up -d -build docker container ls docker images. Once your Jenkins container is up and running, follow the instructions in the Jenkins installation wizard. Jenkins – an open source automation server which enables developers around the world to reliably build, test, and deploy their software. Now run below command to start the docker. Docker run -p 8080: 8080-p 50000: 50000 jenkins / jenkins: lts. The -p flag corresponds to the port number, with the former 8080 referring to the port exposed to the host system and the latter 8080 referring to the port used by the docker container.

In this blog, we will integrate Jenkins with Docker so that we can have an automation to run the docker container directly from the Jenkins. We will create a freestyle and the pipeline job to run a docker image inside a container through Jenkins

If you want to see the video for this article, click here

If you do not know about Docker and the installation process, refer my docker article for the basic understanding and how to do the docker installation.

Prerequisite:

  1. Jenkins should be installed
  2. SSH setup between Jenkins and the remote server
  3. Docker should be installed on the server where you need to run a container and the user should be added to the docker group
  4. Setup SSH between Github and Jenkins if you want to use the SSH url instead of https url of your git repo

Freestyle Job in Jenkins

Let’s create a freestyle job in Jenkins to run a docker commands directly on the Jenkins server and then on the remote server. We don’t need to install the Docker plugin on the Jenkins.

Run Docker on Jenkins Server/Agent

We will create a freestyle job and the docker container will run inside a Jenkins server or the agent where job is actually running

  1. Create a freestyle job , select “Execute Shell” option under Build section and use the below commands.

2. This container will run on the Jenkins Server or if you are using agents then on the Jenkins agents

3. Run the job and see the output, you should be able to see the below output

Run Docker on Remote Host

We will create a freestyle job and the docker container will run on the remote hosts where your application should be deployed

  1. Create a freestyle job , select “Execute Shell” option under Build section and use the below commands
  2. Here, we will be using private/public IP of the remote server and the user for which we have established the SSH connection

3. Make sure to add jenkins user to the docker group on the remote hosts as well.

4. Output of the job

Pipeline Job in Jenkins

  1. Use this git repo
  2. Create a Jenkinsfile as shown below

3. Create a pipeline job and use this git repo and trigger the build

You should be able to success output and the pipeline job like this

Install docker cli on jenkins containerInstall jenkins slave in docker container

Install Docker On Jenkins Container

Docker Troubleshooting

If you see the below error it means docker service is not running on the machine. Start the docker service and try the build again

Install Jenkins In Docker Container

5. If you see the below error “ Permission Denied”

Setup Jenkins Docker Container

then add jenkins user to the docker group and restart the jenkins service as shown below :

Install Docker Cli On Jenkins Container

Congratulations, you have successfully learnt how to Integrate Jenkins with Docker and run the docker container in an automated fashion using CI/CD.