Docker Container as Jenkins Agent

Famidha Thurab
Make Android
Published in
3 min readOct 24, 2023

This article gives a walk through on the steps to set docker container as a Jenkins agent.

Photo by Luca Bravo on Unsplash

What if you are building a huge project which involves multiple scripts, multiple environment variables, multiple actions which has to be performed on a container as we are running a Jenkins job.

Yes, we can run Jenkins job with docker container as its agent.

Lets see on how can we do so in below steps.

  1. Composing a docker image
  2. Spinning up the container using docker compose
  3. Jenkins agent setup

Composing docker image

Assuming that docker has been installed on the host machine, will jump into the creation of docker image to bring up the container and run it as Jenkins agent.

Will create an image with

  1. ubuntu as base
  2. install the required packages such as openJDK and openSSH
  3. create an non-root with home directory
  4. start ssh service
  5. expose port 22
Dockerfile to bringup ubuntu container with JAVA and SSH
Dockerfile to bringup ubuntu container with JAVA and SSH

Now that the dockerfile is available, will build the image.

$ docker build -t jenkins-agent -f jenkins-agent-dockerfile .

where,

  • -t referring to the image name
  • -f referring to the dockerfile name available in the current directory

Spinning up the container using docker compose

Now that we have an image available, lets spin up a container using docker compose, assuming docker compose is preinstalled.

Why docker compose?
With docker compose, bringing up the container is achieved with simple command as we have configured the details of the container on the docker compose file.

Before we create a docker compose file, we need an open port from the host to map with the container.

To find the open port, run any of the below commands.

$ sudo netstat -tuln
$ sudo ss -tuln
$ sudo nmap -p 1-65535 localhost
$ sudo ufw status

Now we will write a compose file with details of the container such as image name, container name, ports, volumes…

Docker compose file, docker_compose.yml
Docker compose file, docker_compose.yml

Lets run docker compose to spin up the container.

$ docker-compose up -d
Creating network "docker_default" with the default driver
Creating ubuntu18-node ... done

Now we have our container running with host port 5000 mapped with the container port 22.

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
37e85a2f09ad jenkins-agent "/usr/sbin/sshd -D" 3 minutes ago Up 3 minutes 0.0.0.0:5000->22/tcp, :::5000->22/tcp ubuntu18-node

Jenkins agent setup

Given that the Jenkins environment is available, lets configure above running container as its node.

On Jenkins console, go to “Manage Jenkins” -> “Manage Nodes and Clouds” -> “New Node”

Give the “Node name”, select “Permanent Agent” and click on “Create”.

Creating a new Jenkins node
Creating a new Jenkins node

Add the details such as below and save.

  1. Remote root directory: /home/superman (A dedicated directory for this node)
  2. Launch method: Launch agents via SSH
  3. Host: Public IP address of the host
  4. Credentials: Create a credentials which we gave in the dockerfile when creating the non-root user.
  5. Host Key Verification Strategy: Non verifying Verification Strategy
  6. Port: 5000 (the port which we mapped to the container from the host)
  7. JavaPath: /usr/lib/jvm/java-11-openjdk-amd64/bin/java (path to the openJDK)
Node details
Node details

Now we have docker container running as Jenkins Agent.

Thanks for reading!!

--

--

Famidha Thurab
Make Android

DevOps Engineer| Editor Make Android | Writes on AIOps | Android IVI | AOSP | Docker | Kubernetes | AWS | Jenkins | Python | Shell | Git