How to install Jenkins with Docker and Docker Compose

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Jenkins is a powerful open-source automation server that helps developers around the world to automate the building, testing, and deployment phases of their development process. With the advent of containerization, Jenkins can be deployed in a Docker container to create isolated and reproducible environments, enhancing the consistency of the setup and reducing conflicts between different project dependencies. This tutorial walks you through the process of installing Jenkins using Docker and Docker Compose, providing an efficient and scalable way to manage Jenkins instances.

Prerequisites

To get the most out of this article, you should have:

  • Docker installed on your system. Instructions can be found on the official Docker website.
  • Docker Compose installed. It’s usually included in the Docker installation on Windows and macOS, but may need to be installed separately on Linux.
  • Basic understanding of Docker and containerization principles.

Installing Jenkins with Docker

First, we’ll begin with a basic setup to run Jenkins in a Docker container without Docker Compose. This is an excellent starting point to understand how Jenkins can be containerized.

docker run -d --name jenkins -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts

Running this command will pull the Jenkins Long-Term Support (LTS) version image from Docker Hub and start a container named ‘jenkins’. The -d flag runs the container in detached mode, allowing you to continue using the terminal. Ports 8080 and 50000 are exposed for web UI access and inter-agent communication, respectively.

Setting up Jenkins with Docker Compose

We’ll move to a more scalable solution using Docker Compose, a tool for defining and running multi-container Docker applications. With Docker Compose, you can manage the entire lifecycle of your Jenkins instance through a single command.

Create a file named docker-compose.yml in your desired directory with the following content:

version: '3' services: jenkins: image: jenkins/jenkins:lts container_name: jenkins ports: - "8080:8080" - "50000:50000" volumes: - jenkins-data:/var/jenkins_home volumes: jenkins-data: 

This configuration file tells Docker Compose to pull the Jenkins LTS image and run it as a service named ‘jenkins’. It exposes the same ports as before and mounts a volume named ‘jenkins-data’ for persistent storage of Jenkins configurations and builds. This ensures data persists even if the container stops or is removed.

To run Jenkins with the composed configuration, use the following command in the directory containing your docker-compose.yml file:

docker-compose up -d

This command starts Jenkins in detach mode, similar to our initial Docker command but managed through Docker Compose. You can manage your Jenkins service lifecycle by using Docker Compose commands such as stop, start, and down.

Accessing Jenkins

After the Jenkins container starts, you can access the Jenkins web UI by navigating to http://localhost:8080. The first time you access Jenkins, it will ask for an initial admin password. Retrieve this password by running:

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

Copy the password and paste it into the Jenkins setup wizard to proceed with the installation.

Customizing Jenkins with Docker and Docker Compose

Now that you have Jenkins running, you might want to customize it. You can use Docker and Docker Compose to set environment variables, change the Jenkins version, customize configurations, or install plugins at startup.

Setting Environment Variables

version: '3' services: jenkins: image: jenkins/jenkins:lts environment: JAVA_OPTS: "-Djenkins.install.runSetupWizard=false" 

This will disable the setup wizard, allowing for an automated setup with pre-defined configurations or scripts.

Installing Plugins at Startup

To automate the installation of Jenkins plugins, you can list them in a file and have Jenkins install them during the container startup.

volumes: jenkins-data: - ./myplugins.txt:/usr/share/jenkins/ref/plugins.txt

Replace ./myplugins.txt with the path to your plugins list file, and Jenkins will handle the installation.

Conclusion

By following this tutorial, you’ve learned how to install Jenkins using Docker and Docker Compose, creating a reproducible and scalable Jenkins environment. Whether you’re running a single instance of Jenkins or scaling out to multiple containers, Docker and Docker Compose make the management straightforward and efficient.