How to setup Jenkins with docker-compose ?

Ertuğrul Şen
5 min readDec 30, 2020

DevOps is a process. We can manage this process with applications without interruption. Docker become one of the most popular management tools available today. We can manage any applications on the docker container. The main purpose of this article is to install jenkins which is one of the first step of the CI/CD process. We will install jenkins wtih docker-compose and make the necessary setup, configurations step by step and create a new docker agent.

In the next article, we will write how to automate this setup with configuration as a code?

Why do we prefer docker-compose?

We will install the CI/CD steps and applications step by step and associate cicd applications with each other and aim to run all applications with a single command.

Firstly, we create first image in compose file and write image, container name and we write the port information that jenkins will access in internal and external network. We open a network because that containers can communicate with each other. The important point is that we want to explain about volume briefly. Containers run on the docker host without any status information and when we container is deleted, everything in it is deleted. Configuration, log and all necessary files of applications may be managed independently Therefore volume is important to us. We set a specific folder in the volume step of our compose file because we will do this for every application and we want the setup files to be where we specified also we can access secret files in locale machine.

We declare volume at the bottom of the compose file. If we don’t declare it, the compose file will give an (no declaration was found in the volume section)error.

Secondly, Another image in our compose file is socat. Our aim in this article is to use the agents as dockerize, It’s needs to make Jenkins container talk to the daemon hosting it therefore we use socat.

We are running our compose file with the ‘docker-compose up’ command then we expect images to be downloaded and jenkins running on port 8080.

‘Docker exec -it jenkins bash’

You can connect to the image with the above command. We provide access to admin password via “var/jenkins_home/secrets” we paste the password into the admin password section. In the next step, we select “Install suggested plugin” and expect plugins to be uploaded to jenkins and then after completing the username and password determination steps, we access the jenkins home page.

We need to configure docker cloud settings so install the docker plugin on jenkins.

Let’s go to Jenkins configure clouds settings and click add new cloud button then create a new docker host.

You can fill the host information as above. You must make sure that the access is done by clicking test connection button. Ip of alpine-socat is important to us, you can write “docker network inspect golden_gate” to learn the socat ip in console. These two containers are under the same network and they can be accessed with each other via hostname so you can configure it as “alpine-socat: 2375”. We used the name information and you can use ip information.

Our next step will be to add a docker template. This template image is important. We used “benhall/dind-jenkins-agent:v2”.

Why didn’t we use “docker/agent” or “jnlp/slave”?

Because docker is not installed in these images. When we run any docker command in agent, we can get an error so we are using “benhall/dind-jenkins-agent:v2” image.

Let’s complete the template setup process as follows.

You must fill in the network and volume sections in the container settings area otherwise the slave will not be able to access the jenkins container.

labels: docker-agent
name: docker
docker image: benhall/dind-jenkins-agent:v2
Network: golden_gate
Volume: /var/run/docker.sock:/var/run/docker.sock
Connection method : Attach docker container

Let’s test our configurations and create a new job.

You create a new pipeline project by clicking on new item on the home page and write the following declarative script to the pipeline script area.

After you getting a new build, you can see that a new docker-agent is running and your process has been completed successfully.

Lastly, We ran Jenkins on docker and we were able to build a new job through docker agent and we have completed these setup settings manually. In the next article we will write about how to automate with configuration as a code. When we run the docker compose file, we will see how all settings are ready. Thank you

--

--