Skip to content

How to install Jenkins using Docker Compose?

Hi. Today I will show you how you can install Jenkins server and run your first task in it within an hour.

What is Jenkins?

Jenkins is an open source tool for automating work related to the Continuous Integration and Continuous Delivery process. It is a tool that supports the work of programmers in the continuous building, testing and even automatic installation of projects on test and production environments.

What is Docker Compose?

Docker is software used to run applications in a virtualized environment. Because of that, we can run the application in a separate container that is independent of the operating system. In this way, we can prepare different environments using different tools without fear of conflicts between versions of libraries and frameworks.

Docker Compose is a tool that allows you to define and run Docker-based applications. This tool allows you can define entire environments consisting of multiple containers using YAML files. As an example of such an environment, I can mention a www application, which consists of a container containing a web server and a second container containing a database for this application.

Required tools

To start installing Jenkins, make sure you have the required tools. As the installation will be based on Docker Compose, please make sure you have it installed.

To verify that Docker Compose is available you can use this command:

> docker-compose -v
Docker Compose version v2.0.0

If you do not have this tool yet, I suggest you install it according to the instructions on this page: https://docs.docker.com/compose/install/

For Windows and Mac OS, Docker Compose is included in Docker Desktop. For Linux, this package must be installed separately.

Note: All the commands in this guide are run from the Linux console, which means that they won’t work in the standard Windows console. Here I propose to use Windows Subsystem for Linux (WSL) which can be installed on Windows 10.

Create a configuration for Docker Compose

At the very beginning, I suggest creating a separate directory where we will work on the project and an additional directory where we will store Jenkins data (~ / jenkins_home).

> mkdir jenkins
> mkdir ~/jenkins_home       
> cd jenkins 

Now create the docker-compose.yml file and copy the following content to it:

# docker-compose.yml
version: '3.7'
services:
  jenkins:
    image: jenkins/jenkins:lts
    privileged: true
    user: root
    ports:
      - 8080:8080
      - 50000:50000
    container_name: jenkins-lts
    volumes:
      - ~/jenkins_home:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/local/bin/docker:/usr/local/bin/docker

The above configuration will allow us to create a container with the latest version of Jenkins (LTS). The most important elements of this configuration are:

  • image – determines the image we want to use.
  • ports – defines what ports we want to make available from the container to the outside. In our case, it will be ports 8080 (www) and 50000 (api)
  • container_name – the name of our container
  • volumes – these are the virtual volumes used by the container. In our case, we have a jenkins_home directory, which is on our system, and an additional two volumes that allow us to use Docker from Jenkins.

Start the container

The container is launched with one simple command:

> docker-compose up -d

After running the Docker command, it will download the Jenkins image from the servers and run the container. In my case it looked like this:

Before we go to the browser, we need to copy the administrator password that was created automatically when the server was first started. We do it with the command:

docker exec jenkins-lts cat /var/jenkins_home/secrets/initialAdminPassword

Jenkins’s first run

Once we have the password, we can go to the browser. Enter http://localhost:8080/ in the URL address and the following page should appear:

Here, in the Administrator password field, enter the password that we just copied. After entering the password, we go to next page: Continue.

In the next step, we have the option to choose the add-ons that we want to install on our server. Here, I suggest using the default list of plugins by selecting the Install suggested plugins option.

After a few moments, Jenkins should install the required add-ons.

In the next step, we need to create an administrator account. After providing the required information, click Save and Continue.

The server address information will appear on the next screen. We leave the default address here and move on (Save and Finish).

Basic setup is complete!

After going to the next page, we should see this screen:

Congratulations, you just installed Jenkins!

We create the first job

The first tasks will be based on a simple Hello World project in Java: https://github.com/Coveros/helloworld. To be able to build a project, you must configure Jenkins to use the Maven tool.

To do this, go to the Jenkins configuration panel (Manage Jenkins):

In the next screen we go to Global Tool Configuration:

On the next screen, go to the Maven section and click Add Maven. Here we give the name of the Maven configuration (just maven) and leave the rest at their defaults. Save the settings.

After configuration, we go back to the main screen (click on Dashboard) and create our first task:

On the next screen we define our job. We give its name and choose the type of Freestyle project.

On the new screen, go to the Source Code Management section. There, select Git and enter the address https://github.com/Coveros/helloworld.git in the Repository URl field. This is the address of our Git repository on GitHub.

In this way, we defined the project source for our task.

Then we go to the Build section, where we will define our jobs related to building the project. Click here Add build step and select Invoke top-level Maven targets. Here we select the previously defined maven configuration. Additionally, we define the goals of our Goals: clean package.

Now let’s add the second step to our build process: Execute shell and pass the command java -cp target / helloworld-1.1.jar com.coveros.demo.helloworld.HelloWorld. This command will start our application and display the result in the console.

We now save our configuration and run our job: Build Now

After a few moments, the task should start and complete successfully (green color).

Here it is worth taking a look at what the whole building process looked like. We can do this by clicking on the link # 1. And then click on Console Output:

On the new page, we should see the log of the entire job.

In the log, we should see information about the correct building of the application and the result of the test application call.

Stop the container

A previously run docker-compose command made the container run in the background. If you want to stop the container, just run the following command in the jenkins directory:

docker-compose down

Summary

In this tutorial, I showed you how to easily start a Jenkins server. Importantly, the server runs in a container but saves its data to a directory available on your computer / server. This means that you are able to easily archive this data or even transfer it to another computer or server. This way, the data will not be lost even if the container is deleted.

Published inCI/CDDockerTutorial

Be First to Comment

Leave a Reply

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