Install Jenkins using Docker Compose
Tags: jenkins, devops, cicd, docker
Published: 2020-05-05 14:14:05

## 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](https://docs.docker.com/compose/install/)

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:

```yaml
/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:

```yaml
/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](https://my-blog-andmedev.s3.amazonaws.com/Screen+Shot+2020-05-04+at+10.59.05+PM.png)