Sometimes no one wants to deploy and configure the whole server and applications. Especially when the time is the scarce resource. Developers know about it – they face this problem every day. One of the solutions is docker. It helps with deploying, provisioning and automating development and production environments without the big workload. Docker can help also with learning. You do not need the create separated virtual machines for your labs. The only one you need is docker and your own notebook/PC. Forget about long configuration processes and subsequent work expenditures (removing, cleaning etc.). If you want to learn some Jenkins, don’t waste your time. Just run Jenkins with docker!

Hereinafter you can find some instructions based on CentOS 7. Steps for Debian are similar – the main difference is the docker installation, which is performed by another package manager. Also, it is possible you will have to install another repository.

Prerequisites – docker

To run docker, you need… docker! And more specifically Docker Community Edition. If you do not know, Docker Engine has been ended at 1.13.1 version (last release: 2017-02-08). Since then packages named “docker” and “docker-engine” have been not maintained anymore. If you want to install the supported version, you should choose Docker Community Edition, which is available in the additional repository. Moreover, CentOS-extras repository should be enabled.

First, if you have installed old Docker Engine, you should uninstall packages:

# yum remove docker docker-common docker-selinux docker-engine 1 # yum remove docker docker-common docker-selinux docker-engine

Next, check if you have enabled the CentOS extras repository:

# yum repolist enabled | grep extras 1 # yum repolist enabled | grep extras

If the output is empty, enable this repository with the following commands:

# yum install yum-utils -y # yum-config-manager --enable extras 1 2 # yum install yum-utils -y # yum-config-manager --enable extras

Now you have to make sure you have installed packages needed by the storage driver. After that, you can add the required repository and install Docker Community Edition.

# yum install yum-utils device-mapper-persistent-data lvm2 -y # yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # yum-config-manager --enable docker-ce-edge # yum install docker-ce -y 1 2 3 4 # yum install yum-utils device-mapper-persistent-data lvm2 -y # yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo # yum-config-manager --enable docker-ce-edge # yum install docker-ce -y

And that’s almost all. All that remains is to start docker service, ensure it starts with the OS, and run the test.

# systemctl start docker # systemctl enable docker # docker run hello-world 1 2 3 # systemctl start docker # systemctl enable docker # docker run hello-world

How to run Jenkins with Docker

Now we can finally get to the point – running Jenkins with Docker. Since now you have installed and working docker. The only one thing you must do is to pull and run the Jenkins container. But first, you should think whether you want to just run the docker, or maybe you want to attach the $JENKINS_HOME path to your local system. With this option, you will be able to easily access all files (including workspaces, plugin directory and more) and you never lose these files. If your container dies, all data will be safe.

Creating /var/jenkins_home

By default, Jenkins docker image uses a user with the UID 1000. So if you create a directory with another owner, docker will return the following error during the container creation:

# docker logs c32d946b81b9 Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions? touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied 1 2 3 # docker logs c32d946b81b9 Can not write to / var / jenkins_home / copy_reference_file .log . Wrong volume permissions ? touch : cannot touch '/var/jenkins_home/copy_reference_file.log' : Permission denied

The solution is very easy – directory you choose for the $JENKINS_HOME should have the owner with the UID 1000. If you don’t have such user in your system, just create it. Most commonly cloud providers give VPSes with the previously created user (exactly with the UID 1000). This situation is possible also when you try to run Jenkins with Docker on your own notebook.

In that case, the easy solution is to change owner of the chosen directory to 1000:1000

# chown 1000:1000 /var/jenkins_home 1 # chown 1000:1000 /var/jenkins_home

Please notice, that this is not the recommended configuration for the production environments!

Running Jenkins with Docker

Now you are prepared to run Jenkins with Docker. You have everything that is required to do that. Just simply type this command in your shell:

# docker run -d -v /var/jenkins_home:/var/jenkins_home:z -p 8080:8080 -p 50000:50000 --name myjenkins jenkins/jenkins:lts 1 # docker run -d -v /var/jenkins_home:/var/jenkins_home:z -p 8080:8080 -p 50000:50000 --name myjenkins jenkins/jenkins:lts

Short description:

docker run -d – it runs container in the background

– it runs container in the background -v /var/jenkins_home:/var/jenkins_home:z – it attaches the container’s directory at the local system directory and it sets up appropriate SELinux fcontext

– it attaches the container’s directory at the local system directory and it sets up appropriate SELinux fcontext -p 8080:8080 – it exposes container’s port 8080 to the local system port 8080

– it exposes container’s port 8080 to the local system port 8080 -p 50000:50000 – same as above, but with the port 50000

– same as above, but with the port 50000 –name myjenkins – display name of our container

myjenkins – display name of our container jenkins/jenkins:lts – it tells “use the LTS version of Jenkins from the jenkins docker repository”

It’s quite important to use “-d” flag because if you don’t type it, you will get the whole output in your shell. You can check whether the container is up and running with the following command:

# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3bb8c6cb1fcd jenkins/jenkins:lts "/bin/tini -- /usr..." 4 days ago Up 4 seconds 0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp myjenkins 1 2 3 # docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3bb8c6cb1fcd jenkins / jenkins : lts "/bin/tini -- /usr..." 4 days ago Up 4 seconds 0.0.0.0 : 8080 -> 8080 / tcp , 0.0.0.0 : 50000 -> 50000 / tcp myjenkins

First login

And now you can enjoy the Jenkins with Docker – all you need to do is changing the default password and start working! For this purpose, type in your browser http://localhost:8080 and check the temporary password in the /var/jenkins_home/secrets/initialAdminPassword

# cat /var/jenkins_home/secrets/initialAdminPassword a8de26c5e7ac421cb337aeb0fd4a9b7f 1 2 # cat /var/jenkins_home/secrets/initialAdminPassword a8de26c5e7ac421cb337aeb0fd4a9b7f

You will be asked about the plugins to install. Here you can choose between installing the suggested plugins (recommended) and chosen by you. It’s strongly recommended to use the suggested plugin, especially when you just starting your adventure with Jenkins.

You have to do only just one more thing – create your first user. This user will be the one available in the Jenkins, until you create your own set of users. Moreover, it will be the first user with the administrative privileges. In the theory, you can skip this step, but it’s not recommended.

As a result of your work, now you have your own, fully operational Jenkins installation running on Docker!