loading...

Install Jenkins using Docker Compose

andresfmoya profile image Andres ・2 min read

What is Jenkins?

Jenkins is an open source automation tool written in Java with plugins built for Continuous Integration purpose. It is used to build and test your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. It also allows you to continuously deliver your software by integrating with a large number of testing and deployment technologies.

And Docker Compose?

Docker is a platform for running applications in an isolated environment called a "container" (or Docker container). Docker Compose is a tool for defining and running 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. It lets you take advantage of the benefits of Docker while abstracting the complexity of your stack.

Installing Jenkins directly in your OS can be tricky and expensive in terms of time and resources. You need to have Java installed in your local machine and at least 10 GB of drive space. On the other hand, using docker compose is really straightforward and offers a lot of advantages.

We're going through the steps to install Jenkins using Docker-Compose.


Install Docker Compose

Docker Desktop for Mac and Docker Toolbox already include Compose along with other Docker apps, so Mac users do not need to install Compose separately. Docker install instructions for these are here:

Install Docker Compose

You can verify the version of docker compose using:

> docker-compose --version
docker-compose version 1.25.4, build 8d51620a

Create docker-compose configuration

Inside your working directory create the docker-compose.yml file:

/jenkins-config

touch docker-compose.yml

Then copy the following configuration:

/jenkins-config/docker-compose.yml

version: '3.7'
services:
  jenkins:
    image: jenkins/jenkins:lts
    privileged: true
    user: root
    ports:
      - 8081:8080
      - 50000:50000
    container_name: jenkins
    volumes:
      - ~/jenkins:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/local/bin/docker:/usr/local/bin/docker

You need to ensure the directory ~/jenkins exists:

mkdir ~/jenkins

This volume will be used to persist all your data: configurations, plugins, pipelines, passwords, etc.

The remaining two volumes allow you to use docker inside the Jenkins server (Yes, you can create docker containers inside a docker container).

Run Docker Compose

/jenkins-config

> docker-compose up -d

Jenkins is running in localhost:8081.

Firs Log in

View the generated administrator password to log in the first time.

❯ docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword

Now, you're ready to install plugins and start creating pipelines. If you want to stop the Jenkins container you can do it with docker-compose down. When you restart it all your configuration, users, plugins previously installed will persist there.

Upgrading Versions

To upgrade to latest versions just modify the tag of the image in your docker-compose yml file, for example:

/jenkins-config/docker-compose.yml

version: '3.7'
services:
  jenkins:
    image: jenkins/jenkins:2.223.1
    ...

That's it! You can start using Jenkins to implement your CI/CD.

Pipeline

Posted on by:

andresfmoya profile

Andres

@andresfmoya

DevOps | Engineer | Infrastructure

Discussion

pic
Editor guide
 

hello I have add this

- /usr/bin/docker-compose:/usr/bin/docker-compose

to my docker-compose file but when I run a docker-compose -v get this error to me

+ docker-compose -v
Traceback (most recent call last):
  File "/usr/bin/docker-compose", line 15, in <module>
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
Build step 'Execute shell' marked build as failure
Finished: FAILURE
 

I have reinstalled docker-compose and now work

 

Hello there, I hope you are doing well. In my case when I try to run a docker image(docker run hello-world) I get this message:
/var/jenkins_home/workspace/myproject_test@tmp/durable-81e309a3/script.sh: 1: /var/jenkins_home/workspace/myproject_test@tmp/durable-81e309a3/script.sh: docker: Permission denied


 

8081 is already in use by Docker. I changed to 8080:8080 and it seems to work.

 

Great article, thank you for sharing.

I tried to set-up Jenkins using docker-compose you provided, and it works but I am unable to use docker commands inside my pipeline, instead receiving error:

docker build -t 547caf1aa35d1cb405cd7c5f91ce564aca862ad8 -f Dockerfile .

/var/jenkins_home/workspace/name@tmp/durable-7666a900/script.sh: 1: /var/jenkins_home/workspace/name@tmp/durable-7666a900/script.sh: docker: Permission denied

I checked again and I have all 3 volumes set correctly.
Any idea what might be the issue?

 

I managed to solve this by changing one of the volumes to:

 - /usr/bin/docker:/usr/bin/docker
 

Is the ~/jenkins:/var/jenkins_home really necessary ? Or can this all exist in Docker ?

 

Big thanks man! its really good tutorial