Running Jenkins as a Docker Container

Jenkins as a Docker container is a powerful way to leverage the benefits of containerization in your CI/CD workflows. Docker allows you to create, manage, and deploy Jenkins environments efficiently, enabling consistent builds across various environments.

Running Jenkins as a Docker Container

Running Jenkins as a Docker container is a powerful way to leverage the benefits of containerization in your Continuous Integration/Continuous Deployment (CI/CD) workflows. Docker allows you to create, manage, and deploy Jenkins environments efficiently, enabling consistent builds across various environments. This post will guide you through the process of running Jenkins in a Docker container, including prerequisites, configurations, and best practices.

If you want to learn how to create a custom Docker image for Jenkins, check out my detailed guide from last week: How to Create a Custom Dockerfile for Jenkins.

1. Understanding the Benefits of Running Jenkins in Docker

Running Jenkins as a Docker container offers several benefits:

  • Portability: Docker containers can run consistently across different environments, ensuring that Jenkins behaves the same on your local machine as it does in production.
  • Isolation: Each Jenkins instance runs in its own container, reducing conflicts with other applications and simplifying dependency management.
  • Scalability: Docker allows you to scale Jenkins horizontally by running multiple instances, which can be particularly useful for large teams or projects.
  • Easy Upgrades: Upgrading Jenkins and its plugins can be accomplished by simply pulling a new Docker image and redeploying the container.

2. Prerequisites for Running Jenkins in Docker

Before you can run Jenkins in a Docker container, ensure that you have the following prerequisites in place:

3. Pulling the Jenkins Docker Image

Jenkins provides an official Docker image on Docker Hub. You can pull the latest Long-Term Support (LTS) version using the following command:

docker pull jenkins/jenkins:lts

This command downloads the Jenkins image, which you can use to create your Jenkins container.

4. Running Jenkins in a Docker Container

4.1 Using Docker Run Command

To run Jenkins in a Docker container, use the docker run command with appropriate options. Here’s a basic command to get you started:

docker run -d \
  -p 8080:8080 \
  -p 50000:50000 \
  --name jenkins \
  jenkins/jenkins:lts
  • -d: Runs the container in detached mode.
  • -p 8080:8080: Maps port 8080 on your host to port 8080 in the container, allowing access to the Jenkins web interface.
  • -p 50000:50000: Maps port 50000, which is used for Jenkins agents to connect to the master.
  • --name jenkins: Assigns a name to the container for easier management.

4.2 Configuring Persistent Storage

To ensure that Jenkins data persists even after the container is stopped or removed, configure a volume for Jenkins home directory. This is critical for preserving build artifacts, job configurations, and plugin installations. You can do this by modifying the docker run command as follows:

docker run -d \
  -p 8080:8080 \
  -p 50000:50000 \
  --name jenkins \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts

In this command, -v jenkins_home:/var/jenkins_home creates a Docker volume named jenkins_home that will store all Jenkins data in the specified directory.

4.3 Setting Up Networking

By default, Docker containers run in bridge mode. If you want Jenkins to communicate with other containers, you can create a custom Docker network. This step is optional but can simplify container communication.

To create a custom network, use the following command:

docker network create jenkins-network

Then, you can run your Jenkins container in this network:

docker run -d \
  --network jenkins-network \
  -p 8080:8080 \
  -p 50000:50000 \
  --name jenkins \
  -v jenkins_home:/var/jenkins_home \
  jenkins/jenkins:lts

5. Accessing the Jenkins Web Interface

Once your Jenkins container is running, you can access the Jenkins web interface. Open your web browser and navigate to:

http://localhost:8080

The first time you access Jenkins, you will be prompted to unlock it using an initial admin password. To find this password, run the following command:

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

Copy the password and paste it into the Jenkins unlock page. After unlocking, you can proceed with the initial setup, including installing suggested plugins and creating an admin user.

Conclusion

Running Jenkins as a Docker container simplifies the setup and management of your CI/CD processes. With the flexibility of Docker, you can easily configure persistent storage, manage networking, and scale Jenkins to meet your team's needs. By following the steps outlined in this post, you can set up a robust Jenkins environment that enhances your software development workflow.

Whether you're just starting with Jenkins or looking to optimize your existing setup, using Docker can provide significant advantages for your continuous integration and deployment strategies.

Read next

Using Jenkins to Trigger AWS Lambda Functions

AWS Lambda is a serverless computing service that allows you to run code in response to events without provisioning or managing servers. Integrating Jenkins with AWS Lambda can help automate workflows, such as deploying code, triggering serverless applications, or running scheduled tasks.

Automating S3 File Uploads with Jenkins

Amazon S3 is a highly scalable, secure, and durable object storage service provided by AWS. It stores files such as application artifacts, backups, media, etc. Automating file uploads to S3 can be integrated with AWS S3 to automate file uploads.

Configuring Jenkins to Deploy to AWS EC2 or ECS

Integrating Jenkins with AWS provides a powerful combination for automating the deployment of cloud-native applications. Jenkins, a popular CI and CD tool, can be used to build, test, and deploy applications to AWS. Automating these deployments can accelerate software delivery.