Setting up the Jenkins + Artifactory on Docker

Dinesh Velhal
8 min readApr 20, 2019

--

Image by Wolfgang Schröpfer from Pixabay

In the previous article, we covered how to easily set up the Jenkins instance on the Docker. In this article, we will cover how to configure more than one containers, each running its own service, in a coordinated way.

Our objective

  1. Setup one container running Jenkins and another one running Artifactory
  2. Set up the network between them so they can communicate with each other
  3. Set the containers to use dedicated persistent storage using the named volumes (we covered this in the previous article)

In addition, we will configure the Artifactory service to act as a Maven repository and then set up a simple Jenkins pipeline to upload the Maven package to Artifactory for every new build.

Quick intro of the Artifactory

Artifactory is a Binary Artifact Manager by JFrog. It simplifies the storage and management of binary files generated as part of the build process during software development. We will use the Open Source version of Artifactory for this article.

OK, let’s start setting it all up.

Step 1 — Set up the containers

Multi-container setup using Docker Compose

We will be using Docker Compose to configure multiple containers (called as Services) in one go. From the official Docker Compose Documentation:

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

In other words, Compose allows you to declare all your services in a YAML format file, that can be used to easily start or stop services with a single command.

We will create a docker compose file to declare

  • 2 services (containers) namely Jenkins & Artifactory. Both these services will be started using the images available.
  • A bridge network that will be used by the containers to talk to each other.
  • 2 named volumes — for use by Jenkins & Artifactory containers respectively.
  • Mapping specific ports from the containers to the ports on the host OS

See the file content along with the description

docker-compose.yml file

Docker Compose file description

  • Line 1 is always the compose file format version (in this case it’s 3.3. (Current version is 3.7 compatible with Docker version 18.06.0+)
  • Line 3 & 13 declare the Jenkins and Artifactory services respectively (Under the services section declared on line 2)
  • 2 named volumes are declared on lines 23 & 24 respectively. They are mapped to the services on lines 11 and 20.
  • A network ci_net is declared on line 26 and mapped to the services on lines 6 & 16 You must have noted that the parameters defined for the services are also the same when you start the containers from the command line using docker container run command.
  • Jenkins service is based on the image jenkins/jenkins:lts (see line 4)
  • Artifactory service is based on te image http://docker.bintray.io (see line 14)

Let’s now start the services!

Let’s save the compose file as docker-compose.yml in a specific folder. This is the default compose file name. Now open the command prompt in this folder and run the below command:

PS E:\DOCKER\compose> docker-compose up

That’s how simple it is! This command will start the network, create the volumes and then run the services (containers) specified in the docker-compose.yml file.

Please note -

  1. You don’t need to specify the compose file name because we saved it with the default name.
  2. Stopping the services (containers) is equally easy. Just use the command docker-compose down

You will see the real-time logs getting written on STDOUT that shows the progress of starting the services. Compose elegantly shows the logs generated by different services in different colors.

docker-compose command logs

Verify that the services started successfully

You can see from the compose file that Jenkins service is mapped to the port 8085 on the host while the Artifactory service is mapped to the 8086. If services are running fine, then you should be able to access them using the URLs http://localhost:8085 and http://localhost:8086 respectively.

Check whether services can contact each other over the bridge network

First list down the running containers:

Docker allows us to start a shell in the running container. We will start a shell in the Jenkins container. See the command below.

Start a shell

The exec subcommand of the docker container command allows us to run any arbitrary Linux command. -ti option starts the command (in this case bash) in interactive mode. You can see that, we get a bash terminal attached to the Jenkins container. Now we can run Linux commands in interactive mode. Let's try to check if the Artifactory container can be accessed.

Connect to Artifactory from Jenkins

You can repeat the same steps with starting a shell in the Artifactory container and then pinging the Jenkins container from there.

Step 2 — Artifactory Configuration

Start the Artifactory UI by browsing to the URL http://localhost:8086. You will be presented with the 1st time setup wizard. complete the steps, especially

  1. Set a new password for the admin user
  2. When it asks you to Create Repositories, Pls choose Maven and click the Create button. This will configure the Artifactory for the Maven type artifacts.
Create Maven Repository

By default following repositories will be created.

Maven Repositories

Initial setup is now complete. Both Jenkins and Artifactory are running and they are on the same network.

Step 3 — Jenkins Configuration

Now that the containers are running, you can access the Jenkins UI using the URL http://localhost:8085. Since you are opening this Jenkins instance for the first time, you will be presented with the 1st time plugin installation screen. Install suggested plugins.

Configuring the Artifactory instance in the Jenkins

After the initial setup is complete, go to the Manage Jenkins → Manage Plugins screen. Search for Artifactory plugin and install.

Artifactory Plugin installation

Now create a new Global credential in Jenkins by following the navigation Credentials → System → Global credentials → Add credentials. Here, add the details of the Artifactory Admin user and save.

Add Artifactory credentials

Configure the tools (Maven and JDK)

Navigate to Manage Jenkins → Global Tool Configuration and configure JDK 8 & Maven 3.6 as shown in the screenshots below:

Add Maven tool details
Add JDK details

Step 4 — Set up a simple pipeline

Now is the time to configure a Pipeline and test the setup done so far!

We will use a git repo available on Github (at https://github.com/dineshvelhal/calculate.git) to configure the pipeline. This repo contains a tiny maven project and also a Jenkinsfile as shown in the screenshot below.

Github Repo

See the inline comments inside the Jenkinsfile to understand its various sections.

Now create a new Pipeline in Jenkins. Configure the pipeline with below details.

Pipeline SCM details
  • Specify the repo URL https://github.com/dineshvelhal/calculate.git
  • Specify your GitHub credentials (Credentials need to be created in advance)
  • Specify the branch as */master
  • Specify the path of Jenkinsfile as maths/Jenkinsfile
  • Save the pipeline

Now let’s trigger a new build. If all configurations are correct, then you should be able to see a successful build in Jenkins!

Successful Build

Bravo! we have successfully configured a Jenkins pipeline.

Let’s now verify in the Artifactory if the artifact is uploaded in the Maven repo or not. For this, first, inspect the pom.xml to check the project version. As you can see below, it’s version 0.0.2.

pom.xml — version details

Since it’s not a snapshot version, the compiled artifact will be treated as release version and will get stored in the lib-release-local repository in the Artifactory. We can verify that by navigating to the Repository Browser in the Artifactory UI as shown below.

New Artifact

You can see that the artifact is added in the lib-release-local repository.

If you navigate to Builds as shown below, you can see the latest build number is 38 which matches the latest build number in the Jenkins as seen in one of the screenshots above.

Build Info

Ok, this confirms that the pipeline is successfully configured. Congratulations!!

In summary

We saw how easy it’s to install Jenkins & Artifactory as container-based services. We used docker-compose to easily start/stop the services. We also verified that Jenkins & Artifactory could connect to each other over a bridge network specified in the compose file. Finally, we configured Jenkins & Artifactory to set up a simple pipeline that executes the following steps as part of the build.

  • clone the project git repo
  • execute unit tests
  • setup Artifactory server details
  • execute Maven build and if it’s successful, upload the artifact in the Artifactory
  • publish the build info in the Artifactory

Also, see:

--

--