Install Jenkins in Linux Using Docker

Jenkins is a powerful open source automation server used for building, deploying and automating projects, in this tutorial we’ll see how to setup its docker image in a Linux box.

Why using Docker ?

My main goal was to experiment with Jenkins , so I needed a quick way to run and stop the server occasionally without worrying about auto-starting services , Docker seemed like a hassle-free solution .

Notes:

  1. I also installed Jenkins using apt and didn’t notice any difference in terms of performance compared to Docker
  2. This is a simplified installation , if your Jenkins projects need to run Docker containers you have to use Docker in Docker instead , refer to docs .

Prepare working directory

To keep things organized , we’ll create a directory for our configuration files and mounted volumes .

mkdir jenkins
cd jenkins

Get Jenkins docker image

docker pull jenkins/jenkins:alpine

Notice that we’re using the :alpine version , your disk storage will thank you .

Set the docker-compose file

Inside the project’s directory , create a new file docker-compose.yml with the following content :

docker-compose.yml

version: "3.9"
services:
        jenkins:
                image: jenkins/jenkins:alpine
                container_name: jenkins
                ports:
                        - 8080:8080
                        - 50000:50000
                volumes:
                        - ./volumes:/var/jenkins_home

This configuration file has instructions on how to start the Jenkins docker container , mainly there are four settings:

  • image : which image to use , we’re using the image we pulled in the previous step
  • container_name : naming the resulting container so we can easily interact with it later (stop, re-run, .. etc)
  • ports : mapping ports from the container to our host machine ports , the 8080 port it the most important as we’ll use it later to access Jenkins server from the browser. (50000 is used for communication between multiple Jenkins instances , nice to set , but we don’t need it now )
  • volumes : any configuration data created by Jenkins is lost after restarting the container (due to Docker’s containers being stateless) , to achieve Persistence we instruct Docker to map the content of Jenkins configuration folder (inside the container) /var/jenkins_home to a local folder in our machine ./volumes that persist between container restarts.

You’ll need docker-compose to use the configuration file:

Check if docker-compose exists in your system using docker-compose --version if command not found , install docker compose:

# get binary and save it in /usr/local/bin
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# make binary executable
sudo chmod +x /usr/local/bin/docker-compose

Let’s Run it

make sure you’re in the project directory where we have the docker-compose.yml file , then start the container : docker-compose up

The first time I encountered a volume permission problem The volume folder was created but with wrong permissions

fixing permissions and restarting container :

sudo chown 1000 volumes/

docker-compose up

Check the logs and take note of the generated password , you’ll need it during setup.

Access Jenkins

Since we mapped ports form container to our host machine , we can access Jenkins from the browser http://127.0.0.1:8080/ the first time you’ll be prompted to enter the generated password from previous step

After installing recommended plugins you can set an admin user.

Congratulation !

Note that docker-compose created a container with our settings , next time all you need is to run the container to get the Jenkins server ready , let’s try that:

Switch to the terminal and stop the container using Ctrl+C

You don’t need to be in the docker-compose.yml directory for this to work.

# go home directory
cd ~

# (optional) list containers , you'll see jenkins
docker container ls -a

# start jenkins container
docker start jenkins

Try to access http://127.0.0.1:8080/ from the browser Welcome to Jenkins !