Deploy Jenkins in Docker

By Rex Resurreccion Apr 04, 2021
Deploy Jenkins in Docker

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 Docker On Host Server

yum update -y
yum-config-manager --add-repo
yum install -y docker-ce docker-ce-cli
systemctl start docker
chown root:docker /var/run/docker.sock

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.

curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
chmod ug+x /usr/local/bin/docker-compose
chown root:docker /usr/local/bin/docker-compose

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.

useradd jenkins
usermod -aG docker jenkins
id jenkins

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.

su jenkins
mkdir -p jenkins/jenkins_home

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.

version: '3.7'
      context: .
      dockerfile: ./Dockerfile
    container_name: jenkins
      - "8080:8080"
      - "$PWD/jenkins_home:/var/jenkins_home"
      - "/var/run/docker.sock:/var/run/docker.sock"
      - jenkins-net


Use Docker inside Jenkins container

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.

FROM jenkins/jenkins

USER root

RUN apt-get update -y

RUN apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release && \
    curl -fsSL | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

RUN echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
  $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

RUN apt-get update -y && \
    apt-get install -y docker-ce docker-ce-cli && \
    curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \
    chmod +x /usr/local/bin/docker-compose

RUN usermod -u 1001 jenkins && usermod -aG docker jenkins

USER 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.

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. 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.

Jenkins Dashboard Menu
Jenkins Manage Plugins

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

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

© 2020