How to Install Jenkins on Ubuntu with a Docker Container

Photo of author

By Victor Ashiedu

Published

Do you want to set up a Jenkins server but avoid the pain of manual installation? You can achieve this by installing Jenkins on Ubuntu using the Jenkins Docker container!

This is a hands-on walkthrough of the steps to install Docker, start a Jenkins container, open port 8080, and complete the initial Jenkins configuration.

Let’s do this!

Step 1: Install Docker on the Ubuntu Server

The beauty of running apps as containers is that you do not need to install too much software. If you’re containerizing with Docker, all you need to install is Docker!

Follow these steps to complete the first task of setting up Jenkins on an Ubuntu server with the official Jenkins Docker Container:

  1. As a standard practice, always update the app manager by running the “apt update”
sudo apt update
  1. After updating the Ubuntu app manage, execute “docker” to get the command you need to install the app.
When you run “docker,” since the app is not installed, Ubuntu returns a list of commands you can execute to install it.
How to Install Jenkins on Ubuntu with a Docker Container - Install Docker with the 'sudo apt install docker.io' command

I will run the command below (highlighted in the above screenshot) – to install Docker on my Ubuntu server;

sudo apt install docker.io

When you execute the command, you will be prompted to enter the root password. If you’re new to Linux and wondering why; it is because of the use of “sudo” in the command.

Moving on, after entering the root password, the installer will prompt you to confirm the installation. Type “Y” and press enter.

Wait for Docker to install, then proceed to step 2 below. Before you proceed, confirm that Docker is now installed by executing the “docker” command.

confirm that Docker is now installed by executing the "docker" command.

Since Docker is now installed, it displays information containing some details about the command.

Step 2: Start the Jenkins Container with a Docker Volume

Step 2: Start the Jenkins Container with a Docker Volume

After installing Docker on your Ubuntu Server, the next step to configuring Jenkins is to start the Jenkins container with a Docker Volume.

Wondering why we need to start the Jenkins container with a Docker Volume? Data changes in a Docker container are NOT persistent – meaning that if an app running as a container makes some changes, when the container restarts, it loses the data. To stop this from happening, you use a Docker Volume to sync the data on the container to a specified directory on the host (in this example, the Ubuntu server) – more on Docker Volumes later.

To start a Jenkins container on your Ubuntu Server using its official Docker image, execute the command below:

sudo docker run -p 8080:8080 -p 50000:50000 -d \
-v jenkins_home:/var/jenkins_home \ 
-v /var/run/docker.sock:/var/run/docker.sock \
jenkins/jenkins:lts
If the command fails to run in one go, copy and paste line by line. After pasting the first line, type “\” and press enter, then copy and paste the next line. Finally, hit the enter key to run the command.

Here are the explanations of the command and its parameters:

docker run – this is the Docker command you use to start a Docker container from an image

-p 8080:8080 – the “-p” switch is used to map the port the app’s container is running on to the host’s port. In this example, the first port 8080 represents the port on the host (my Ubuntu server).

Likewise, the second port 8080 is the container’s app. Notably, to access Jenkins via a browser, you use port 8080.

So, -p 8080:8080 means “map the container’s port 8080 to the host’s port 8080.” Note that the two ports do not have to be the same.

For example, if port 8080 was not available on my host, I could have used -p 8081:8080 – this would map the Jenkins port 8080 to the host’s port 8081.

-p 50000:50000 – this switch is optional but I included it for completeness. However, in production environments, this may be required, and here is why:

If you need a Jenkins server for a large workload, you may need to configure Jenkins clusters.

Port 50000 is the port where the master and worker Jenkins nodes communicate. So, it is essential to map the port to the host as well.

-d – this switch makes Docker run the Jenkins container in “detached” mode. This means that the Jenkins container process runs in the background.

Setting up the Jenkins Docker container this way in an Ubuntu server – or any OS – allows you to close the terminal without stopping the container’s process.

-v jenkins_home:/var/jenkins_home – you guessed it, the “-v” stands for volume! Earlier, I explained that to persist data on the Jenkins container, you must add a Docker volume.

jenkins_home” is the name of the Docker volume that the data on the container is synched – more on this later. However, the “/var/jenkins_home” is the path a Jenkins Docker container saves data.

By specifying “-v jenkins_home:/var/jenkins_home,” I’m asking Docker to create a Volume on my Ubuntu server and sync the container’s data on the path /var/jenkins_home to the same path on the host.

-v /var/run/docker.sock:/var/run/docker.sock – this line of code mounts the path to the Docker binaries on the host server (where we already installed Docker) to the Jenkins container.

This is a very easy way to make Docker available on the container without needing to install it.

jenkins/jenkins:lts – this is the name of the Jenkins Docker image. The first part (before the “:”) is the image name.

Meanwhile, the second part (“lts”) is the “tag” or version of the image you want to use. You can learn more about available tags on the Jenkins official Docker hub page.

Now that you understand the parameters of the command that installs Jenkins on Ubuntu by starting its Docker container, let’s run the command.

The command pulls the image in “Layers” from the Docker Hub – then, it starts the Jenkins container.

How to set up Jenkins on Ubuntu with a Docker Container - Step 2: Start the Jenkins Container with a Docker Volume

Before proceeding to the next step, let’s run some commands that give us information about the Jenkins container, starting with “docker ps”:

sudo docker ps

The ‘docker ps’ command confirms that the Jenkins container started successfully.

There are multiple of information about the container, but I like to highlight the ports. The ports “0.0.0.0:8080->8080/tcp, :::8080->8080/tcp” – the first is IPv4, while the second is IPv6 – indicates that the host’s port 8080 is mapped to the same port on the container.

Similarly, the values “0.0.0.0:50000->50000/tcp and :::50000->50000/tcp” are port mappings for Jenkins master and worker nodes.

Having reviewed the results of the “docker ps” command, let’s look at another important command that provides information about the Docker volume:

sudo docker volume inspect jenkins_home

The command displays useful information about the Docker volume, jenkins_home created when we started the Jenkins container. By the way, this type of Docker Volume is called a “Named” volume.

Back to the result of the last command, here is the screenshot.

I have highlighted the “Mountpoint” because this is where the Jenkins data is stored on the Ubuntu server (the container host). To display the information in the path, let’s run the “ls” command.

sudo ls /var/lib/docker/volumes/jenkins_home/_data

The command displays multiple files and directories but for now, I will like you to the “secrets” directory. This is because this folder has the initial password we need to sign in to Jenkins – more on this later.

Step 3: Confirm that Port 8080 is Open on the Ubuntu Server

Before you proceed with the demo in this section, you must install telnet on your PC and netstat on your Ubuntu server.

So far, we’ve covered two steps in deploying Jenkins on Ubuntu with the Jenkins Docker container.

In the process, we already established that Jenkins is available on port 8080. So, since Jenkins is on this port, we should be able to access it by entering “server IP address:8080” on the browser.

Before we do that though, let’s confirm that port 8080 is open on the server. First, from your computer, run the telnet command:

192.168.0.96 is the IP address of my Ubuntu server while (as we already established), 8080 is the Jenkins port. If port 8080 is open, the connection should be established and you will see a blank screen.
telnet 192.168.0.96 8080

Another method to check that port 8080 is open is to view the app using the port on the Ubuntu server. To display this information, run the command below on your Ubuntu server:

sudo netstat -tulpn | grep :8080

The command confirms that our Jenkins Docker container is listening on port 8080 for IPV4 and IPV6 for all incoming connections.

Step 4: Complete the Initial Jenkins Configuration

Now that we have established that port 8080 is open on the Ubuntu server, it is time for the final step – completing the initial Jenkins configuration.

Follow the steps below to conclude the final stage of installing Jenkins on Ubuntu:

Enter the IP address of the Ubuntu server, followed by “:8080” on the browser. In my example, the IP address of my Ubuntu server is 192.168.0.96, I’ll enter:

192.168.0.96:8080

And, boom, Jenkins is up! Well, not quiet.

How to Install Jenkins on Ubuntu with a Docker Container - initial Jenkins configuration page

The first page of the Jenkins configuration requires us to enter an Administrator password. Before you panic, Jenkins provided the path this password is saved – see the highlighted portion of my screenshot above.

If you recollect, I touched on this in step 2 when I discussed this command.

sudo ls /var/lib/docker/volumes/jenkins_home/_data

Wait a minute, but the path in the above command is different from the path in the initial Jenkins configuration page shown earlier!

Yes, it is.

The path – /var/lib/docker/volumes/jenkins_home/_data – is the path the Docker volume on the Ubuntu server is mounted. However, /var/jenkins_home/ is the Jenkins directory on the container.

We can get our initial Jenkins administrator password from both paths, but for this guide, I’ll get the password from the volume mount point – /var/lib/docker/volumes/jenkins_home/_data.

So, let’s review the output of this command, once again:

sudo ls /var/lib/docker/volumes/jenkins_home/_data

As I mentioned earlier, the _data directory has a directory called “secrets.”

This is the same “secrets” directory in the Jenkins configuration page.

Anyway, to retrieve the administrator password, use the cat command below:

sudo cat /var/lib/docker/volumes/jenkins_home/_data/secrets/initialAdminPassword

And there is your administrator password. Copy the password and paste it on the “Administrator password” field of the Jenkins “Getting Started.”

After you enter the password, click “Continue” to proceed with the initial Jenkins server setup.

On the next page, select the default, “Install suggested plugins.” Then, wait for Jenkins to install the plugins.

start Jenkins on Ubuntu with a Docker Container

After that, create the first Jenkins administrator.

Finally, confirm the Jenkins server URL and port and finish the initial setup.

deploy Jenkins on Ubuntu with a Docker Container
setup Jenkins on Ubuntu with a Docker Container

I hope that by following this guide you set up Jenkins on Ubuntu with a Docker Container successfully! If you did or have any questions, let us know with the comments form towards the end of this page.

Step 5: Make Docker Command Available on the Jenkins Container

When I mounted the Jenkins container in step 2, I explained that this line – “-v /var/run/docker.sock:/var/run/docker.sock” – of the code mounted the path to the Docker binaries on the host to the same path on the Jenkins container.

However, to make Docker commands available on the container, there are some final tasks we need to perform.

If you’re wondering why we need Docker commands on the Jenkins container, it is to allow us to run Docker commands when we create Jenkins jobs.

To confirm that Docker commands are not available on the container, let’s sign in to the container as the normal jenkins user. This is done using the “docker exec” command.

However, since you require the container’s ID, run the “docker ps” command first to get it.

sudo docker ps

Now, log in to the container and run the “docker” command

Now that we have confirmed that docker is not available on the Jenkins container, let’s fix that problem with these steps:

  1. Exit the container as the jenkins user and sign in as root with this command
sudo docker exec -u 0 -it bd3c208d8633 bash
The difference between the above command and the previous “docker exec” command is that this last one included the “-u 0” switch – “u” allows specifying a user and “0” is a root user in a Linux-based docker container.
  1. After signing in as root, execute this command:
curl https://get.docker.com/ > dockerinstall && chmod 777 dockerinstall && ./dockerinstall

The curl command fetches the latest version of Docker from the official site, https://get.docker.com, and saves it in the dockerinstall folder. Then, the chmod command sets the correct permission on the folder.

Finally, “./dockerinstall” command installs Docker on the container. Execute the command, wait for it to complete, and proceed to step 3.

  1. Grant everyone “rw” permission to the /var/run/docker.sock file. Before you run the command below, execute “ls -l /var/run/docker.sock” – to see the current permission.
chmod 666 /var/run/docker.sock

Execute “ls -l /var/run/docker.sock” again to confirm that everyone now has “rw” permission. Here are my results.

  1. Finally, exit the container as root and log in as jenkins user – docker is now available on the container!

Other Useful Resources

  1. Docker
  2. Docker Hub Container Image Library | App Containerization
  3. Jenkins – Build great things at any scale
  4. Jenkins Docker container
  5. Docker Volume

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment

Send this to a friend