Running jenkins with Docker Compose on Ubuntu 20.04.6 LTS (Focal Fossa)
- 5 minsBefore Installation of Jenkins with Docker compose?
- I assume that You have already installed docker and docker compose for this tutorial.
- I will run the container as root user, this is not a best practise for any docker image.
Prepare Docker Compose File for Jenkins
- Let’s create a new directory for your Jenkins docker compose yaml file and Create a yaml file.
mkdir docker-compose
cd docker-compose
touch docker-compose.yaml- Edit your yaml file and You can use below compose file.
version: '3.8'
services:
jenkins:
image: jenkins/jenkins:lts
restart: always
privileged: true
user: root
ports:
- 8080:8080
- 50000:50000
container_name: jenkins
volumes:
- /home/root/jenkins_compose/jenkins_configuration:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock- Save the yaml file.
Run Docker Compose and Setup Jenkins
- We are ready for run compose file. Run docker-compose in the directory where you placed docker-compose.yaml.
docker-compose up -d
- You can access Jenkis via 8080 port now and You will see unlock page.
- You can find the initial password from Docker container logs or from secrets file.
- Go bask your terminal and check container logs.
docker logs jenkins
-
Copy and paste the password.

-
Select Install Suggested Plugins on the next page. When Jenkins finishes, it will prompt you for a new admin user and password. Enter a user name and password and click Save and Continue.
-
You may create your user.

-
The next page gives you a chance to change the host name of your controller. For this tutorial, you can accept the default and click Save and Finish.

- Click start Jenkins.
- And We are done for setup, We can set up an agent now!
Adding Jenkins Agent With Docker Compose
- To make nodes status ready, we must install POD network addons like Calico or flannel.
- An agent is typically a machine, or container, which connects to a Jenkins controller and executes tasks when directed by the controller.
- We will add a new container as an agent of Jenkins.
- Before adding or running agent We need to create SSH key This key will be used controller to access the agent via SSH.
- Use ssh-keygen to create a key.
ssh-keygen -t rsa -f jenkins_agent-
This command creates two files: jenkins_agent, which holds the private key, and jenkins-agent.pub, the public key.

- We will give the key to controller access to the agent.
-
Start with the Manage Jenkins menu.

-
Then go to Manage Credentials.

- Click Jenkins under Stores scoped to Jenkins.
-
Then click Global credentials.

-
Finally, click Add Credentials in the menu on the right.

- Set these options on this screen.
- Select SSH Username with private key.
- Limit the scope to System. This means the key can’t be used for jobs.
- Give the credential an ID.
- Provide a description.
- Enter jenkins for a username. Don’t use the username used to create the key.
- Under Private Key, check Enter directly.
- And, paste the contents of jenkins_agent in the text box.
-
Click Create.

- We are ready for setting up the agent.
- We will use jenkins_agent.pub in docker-compose.yaml.
agent:
image: jenkins/ssh-agent:jdk11
privileged: true
user: root
container_name: agent
expose:
- 22
environment:
- JENKINS_AGENT_SSH_PUBKEY=<YOUR-SSH-KEY>- Add a new service to docker compose file.

- New service-container with jenkins/ssh-agent:jdk11 image and options similar to the controller, except you’re exposing the SSH port, 22
-
We addedenvironment variable with the public key. Add the exact contents of the text file, including the ssh-rsa prefix at the beginning. Don’t enclose it in quotes.
- And now We are ready to run again our compose file but before that We need to top Docker Compose.
docker-compose down
docker-compose up -d
-
We need to back Jenkins, click Manage Jenkins, and select Nodes

-
Click New Node.

-
Now define your Jenkins agent. Give a node name and select permanent agent.
- Top of the form, give the agent name and set the Remote root directory to /home/jenkins/agent.
- Then, in the next part of the form, select Use this node as much as possible under Usage.
- Under Launch method, select Launch agents via SSH.
- You can create label as agent
- For Host, enter agent. Each container can reach the others by using their container names as hostnames.
- Next, click the dropdown under Credentials and select the one you just defined.
- Now, under Host Key Verification Strategy, select Non verifying Verification Strategy.

- Click Advanced on the right.
- This opens the advanced configuration page. You need to change one setting here. Set the JavaPath to /usr/local/openjdk-11/bin/java.
-
And Also You can specify agent timeout values.

- Click Save at the bottom, and now it’s time to watch the agent start.
-
Jenkins will take you back to the node list. Click on your new node name.

-
Then click on Log in the dropdown menu.

- The most important entry you’ll see is Agent successfully connected and online.
- But if you look at the other entries, you’ll see plenty of information about your agent, including the SSH key.
Guneycan Sanli.