Docker Container as Jenkins Agent
This article gives a walk through on the steps to set docker container as a Jenkins agent.
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.
- Composing a docker image
- Spinning up the container using docker compose
- 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
- ubuntu as base
- install the required packages such as openJDK and openSSH
- create an non-root with home directory
- start ssh service
- expose port 22
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…
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”.
Add the details such as below and save.
- Remote root directory: /home/superman (A dedicated directory for this node)
- Launch method: Launch agents via SSH
- Host: Public IP address of the host
- Credentials: Create a credentials which we gave in the dockerfile when creating the non-root user.
- Host Key Verification Strategy: Non verifying Verification Strategy
- Port: 5000 (the port which we mapped to the container from the host)
- JavaPath: /usr/lib/jvm/java-11-openjdk-amd64/bin/java (path to the openJDK)
Now we have docker container running as Jenkins Agent.
Thanks for reading!!