How to Run Jenkins on Docker: A Complete Tutorial

Running Jenkins on Docker is a great way to simplify the setup and management of your continuous integration and deployment processes. Docker allows you to run Jenkins in an isolated environment, making it easy to scale and maintain. This tutorial will guide you through the steps to get Jenkins up and running on Docker, from setting up your environment to troubleshooting common issues.

Key Takeaways

  • Learn how to install Docker and verify its installation on your system.
  • Understand the steps to pull and run the official Jenkins Docker image.
  • Get insights into customizing your Jenkins Docker image with a custom Dockerfile.
  • Discover how to manage Jenkins plugins effectively within a Docker container.
  • Understand advanced configurations like setting up persistent storage and networking for Jenkins.

Running Jenkins in a Docker Container

Running Jenkins in a Docker container is a straightforward process that allows you to leverage the benefits of containerization. This section will guide you through starting the Jenkins container, accessing the Jenkins dashboard, and performing initial configuration steps.

Customizing Your Jenkins Docker Image

Creating a Custom Dockerfile

To create a custom image and bundle your favorite tools, start by creating a Dockerfile. This file will define the base image and any additional software you want to include. Here’s a simple example:

FROM jenkins/jenkins:lts-jdk11
USER root
RUN apt-get update && apt-get install -y lsb-release
RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
    https://download.docker.com/linux/debian/gpg
RUN echo "deb [arch=$(dpkg --print-architecture) \
    signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
    https://download.docker.com/linux/debian \
    $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"

This Dockerfile starts with the official Jenkins image and adds Docker CLI tools and some essential plugins.

Building Your Custom Jenkins Image

Once your Dockerfile is ready, you can build your custom Jenkins image. Use the following command to build the image and give it a meaningful name:

docker build -t myjenkins-blueocean:latest .

This command will create a new Docker image based on your Dockerfile. Make sure to replace myjenkins-blueocean:latest with a name that makes sense for your project.

Running Your Custom Jenkins Container

After building your custom image, you can run it as a container. Use the following command to start the container:

docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home myjenkins-blueocean:latest

This command will start Jenkins on port 8080 and map the Jenkins home directory to a Docker volume for persistent storage. You can now access Jenkins at http://localhost:8080 and proceed with the initial setup.

Customizing your Jenkins Docker image allows you to tailor Jenkins to your specific needs, making it easier to manage and deploy your CI/CD pipelines.

Managing Jenkins Plugins

Accessing the Plugin Manager

To get started with managing plugins in Jenkins, you need to access the Plugin Manager. From the Jenkins dashboard, click on Manage Jenkins. Then, select Manage Plugins. This is your central hub for all things related to Jenkins plugins.

Installing Essential Plugins

Once you’re in the Plugin Manager, you can install new plugins. Go to the Available tab to see a list of plugins you can add. If you’re unsure which plugins to install, you can start with the recommended ones. Simply check the boxes next to the plugins you want and click Install without restart. This will add the plugins to your Jenkins instance without needing to restart it.

Updating and Removing Plugins

Keeping your plugins up-to-date is crucial for security and performance. In the Plugin Manager, go to the Updates tab to see if any of your installed plugins have updates available. Select the plugins you want to update and click Download now and install after restart. If you need to remove a plugin, go to the Installed tab, find the plugin you want to remove, and click Uninstall. This will remove the plugin from your Jenkins instance after a restart.

Pro Tip: Regularly check for plugin updates to ensure your Jenkins instance runs smoothly and securely.

Advanced Jenkins Configuration

Setting Up Persistent Storage

To keep your Jenkins data safe, you need to set up persistent storage. This ensures that your data isn’t lost when the container stops or restarts. Use Docker volumes to store Jenkins data outside the container. You can create a volume with the command:

 docker volume create jenkins_home

Then, run Jenkins with the volume attached:

 docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk11

Configuring Jenkins as a Systemd Service

Running Jenkins as a systemd service allows it to start automatically on boot. Create a service file at /etc/systemd/system/jenkins.service with the following content:

[Unit]
Description=Jenkins
After=docker.service
Requires=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk11
ExecStop=/usr/bin/docker stop -t 2 jenkins
ExecStopPost=/usr/bin/docker rm -f jenkins

[Install]
WantedBy=multi-user.target

Enable and start the service with:

 sudo systemctl enable jenkins
 sudo systemctl start jenkins

Networking and Port Configuration

By default, Jenkins runs on port 8080. You can change this by passing the --httpPort argument. For example, to run Jenkins on port 8081, use:

 docker run -p 8081:8081 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts-jdk11 --httpPort=8081

You can also set up a reverse proxy to manage traffic. Nginx is a popular choice for this. Here’s a basic Nginx configuration:

server {
    listen 80;
    server_name jenkins.example.com;

    location / {
        proxy_pass http://localhost:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

This setup helps in managing traffic and adds a layer of security.

Remember, advanced Jenkins job configuration can be done using Groovy scripts for automation. This makes your setup more flexible and powerful.

Troubleshooting Common Issues

Jenkins on Docker

Running Jenkins on Docker can sometimes lead to unexpected problems. Here, we’ll cover some common issues and how to fix them.

Scaling Jenkins with Docker

Scaling Jenkins with Docker can help you manage larger workloads and improve performance. This section will guide you through running multiple Jenkins containers, using Docker Compose, and connecting Jenkins agents.

Frequently Asked Questions

What is Jenkins?

Jenkins is an open-source automation server widely used for continuous integration (CI) and continuous delivery (CD) in software development. It helps automate the building, testing, and deployment of code changes.

What is Docker?

Docker is a platform that uses OS-level virtualization to deliver software in packages called containers. It allows applications to run in isolated environments.

How do I install Docker on my system?

To install Docker, visit the official Docker website and follow the installation instructions for your operating system. Make sure to follow any post-installation steps provided.

How do I pull the Jenkins Docker image?

Open your terminal and run the command `docker pull jenkins/jenkins`. This will download the latest Jenkins image from Docker Hub.

How do I start a Jenkins container?

Run the command `docker run -p 8080:8080 -p 50000:50000 -v /your/home:/var/jenkins_home jenkins/jenkins` in your terminal. This starts a Jenkins container with the necessary port mappings and volume settings.

Where can I find the initial admin password for Jenkins?

You can find the initial admin password in the Jenkins container logs. Run the command `docker logs [container_id]` and look for the line that contains the password.

You may also like...