How to run Jenkins with docker?

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:

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

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

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.

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

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:

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

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

Jenkins_home owner
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:

Short description:

  • docker run -d – 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
  • -p 8080:8080 – it exposes container’s port 8080 to the local system port 8080
  • -p 50000:50000 – same as above, but with the port 50000
  • –name 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:

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

Jenkins with Docker - login screen

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.

Plugins installation

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.

First user creation

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

Leave a Reply

Your email address will not be published. Required fields are marked *