Install Jenkins In Docker Container

Running Jenkins Container. Next, we'll run a docker container using this image and map data directory from the container to the host. Here we map /var/lib/jenkins from the container to jenkins/ directory from the current path on the host. Jenkins port 8080 is also exposed to the host as 49001. Aug 30, 2020 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. Create a freestyle job, select “Execute Shell” option under Build section and use the below commands.

How to Deploy Jenkins in Docker? How to use use Docker inside Jenkins container?

This will be a quick tutorial on how to deploy Jenkins using Docker. Also, we will customize our Jenkins Docker image to allow us to use Docker in Jenkins Pipeline.

Assuming you already have a Linux host server up and running. While doing this tutorial I am running the commands on a CentOS Operating System.

The first step is to install Docker on your host server and start the docker daemon. Then change the ownership of a file docker.sock to make sure that all users in your system that belongs to docker group will not get a permission error while running Docker commands.

Install Jenkins In Docker Container

Install Docker On Host Server

Install Jenkins In Docker Container Linux

Install jenkins in docker container designInstall

Next is to install docker-compose. Later we will use a compose file to easily manage our Jenkins service. If you are still getting an error after installing docker-compose, try creating a symbolic link to the applicable directory in your system’s path. In here, I created a link to /usr/local/bin. And again, to avoid getting permission issue we changed the ownership of the docker-compose file to allow docker group to access this.

Install Docker-compose In Jenkins Container

Right now we are running all the commands as a sudoer (or as root) in our system. But for best practices, we will create a new jenkins user that will belong to the docker group. Also, notice the last command here in this step. We will need to take note of the User ID of jenkins. This ID will also be assigned inside the Docker image later when we customize this.


Install Jenkins In Docker Container Design

Install Jenkins In Docker Container

Login to your host server as the newly created jenkins user. Make sure you are in the home directory. Then create a folder where we will store all our files related to our Jenkins service.

Use docker-compose to deploy Jenkins

The first file we are going to create inside our folder /home/jenkins/jenkins is the docker-compose.yml file. In here, we can define the context of our build and where to find the Dockerfile. Also, we need to define our network port and volumes to keep our data persistent. This allows us to stop and start again our Jenkins service without having to start with a blank installation. All these files will be saved when we mount the volume of our Docker container into the folder /home/jenkins/jenkins/jenkins_home.

Use Docker inside Jenkins container

Install Jenkins In Docker Container In Linux

The next important file we need to create is our Dockerfile. Basically what this file does is define our customization inside the Jenkins image. In here we used jenkins/jenkins as base image. Then, as root user, install docker-ce so we could use Docker commands later in our Pipeline. In the early steps we got the User ID of jenkins in our host server, now we will use this to match the User ID inside the Docker image. And finally, switch back the user to Jenkins.

Now that we have our docker-compose and Dockerfile all set up, we can now deploy Jenkins using Docker! 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.

Once your Jenkins container is up and running, follow the instructions in the Jenkins installation wizard. Access the portal using your host server IP Address and port number. For example

After you finished the Jenkins installation wizard. Go to your Dashboard, then click on Manage Jenkins, then Manage Plugins. Search for “Docker Pipeline” and install this plugin.

Install Jenkins In Docker Container

Once the Docker Pipeline plugin has been installed and Jenkins has restarted, you should now be able to use Docker with Pipeline.

Install Jenkins In Docker Container Model

If you enjoyed this tutorial, please read also my Docker command cheat sheet.

Most Viewed Posts