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!

6 Replies to “How to run Jenkins with docker?”

  1. It might be worthwhile to update this to show using a Docker volume docker volume create my_jenkins_home instead of mapping in from the host and then docker run -d my_jenkins_home:/var/jenkins_home ...etc. This should avoid the host/guest permissions issues while still allowing for persistence.

    You also didn’t show the command you used to get into the container to get the initialAdminPassword. I believe it is something like docker exec containerID cat /var/jenkins_home/secrets/initialAdminPassword.

    1. Hi, Dragon!

      Thanks for your feedback! Yes, you’re right, it’s a very good idea to add docker volume to this article. And yes, it’s exactly that command. 🙂 I will try to update my article this week. Thank you very much! 🙂

  2. Thanks for sharing,
    I somehow followed your howto to implement my testing jenkins to not to compromise our corporate.
    adding my 50cents : add –rm flag to the run command to not to get this error after stoping and attempting to start again :
    docker: Error response from daemon: Conflict. The container name “/myjenkins” is already in use by container 3d4b6c645b5029087893ea4b7846573e727432dc4577851960efc39d3cca493c. You have to remove (or rename) that container to be able to reuse that name..
    so final command will be :
    # docker run -d -v /var/jenkins_home:/var/jenkins_home:z -p 8080:8080 -p 50000:50000 –rm –name myjenkins jenkins/jenkins:lts

    All the best

    1. Hi, Dali!

      Thanks for feedback! Yes, you’re right, –rm it’s very useful when you use a standard docker command. I’d prefer to use docker-compose on a daily basis work, so I forgot to add this option to my post. But I’ll fix that asap! 🙂

  3. Hi,

    I have to run the Jenkins in docker with different user .So that when i add any command in the Build Step in the New item it should use that user ?
    Can you help me how can i achieve this ?

    1. Hi!

      What do you mean when you say that you “have to run the Jenkins in docker with different user”? With a different user than the default one in the host system (e.g. not as user “ubuntu” with the UID 1000)? Or you have to change user “jenkins” in the image to another one?

      Anyway, independently from that, when you set up your Jenkins in docker, you should be able to run any command in the build step (I assume that you mean some shell scripts as a build step) without any problem and without necessity of adding some “su USER” or something like that. All build steps should “inherit” the user from the container. Like for normall installation.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.