How to Deploy Jenkins in a Docker Container

If you’re looking to streamline your CI/CD pipeline, deploying Jenkins in a Docker container offers flexibility, scalability, and ease of configuration. In this guide, we’ll walk you through setting up Jenkins in Docker using a custom Dockerfile, allowing you to execute Docker commands inside Jenkins – a must for building Docker images as part of your CI/CD workflow.

Why Deploy Jenkins in Docker?

Running Jenkins in a Docker container has several advantages:

  1. Isolation: Docker containers keep your Jenkins setup separate from the host environment.
  2. Portability: Containers make it easy to move Jenkins across different environments without reconfiguring.
  3. Simplicity: Docker images are straightforward to update or recreate, which is ideal for Jenkins setups that require custom configurations.

In this tutorial, we’ll create a custom Dockerfile to ensure Jenkins can interact with Docker, allowing you to run Docker commands within your Jenkins container.

Prerequisites

To follow this guide, you’ll need:

  • Docker installed on your local machine.
  • Basic knowledge of Docker commands.
  • Root or sudo privileges.

Step 1: Create a Custom Dockerfile

To begin, create a Dockerfile that will define the custom Jenkins image. This Dockerfile extends the official Jenkins image and installs Docker within the container so Jenkins can execute Docker commands. Here’s the complete Dockerfile:

FROM jenkins/jenkins   # Use the official Jenkins image
USER root # Switch to root user to install dependencies

# Install required packages and Docker
RUN apt-get update && \
apt install -y apt-transport-https ca-certificates curl gnupg2 software-properties-common

# Add Docker's official GPG key and repository
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - && \
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

# Update and install Docker within the container
RUN apt-get update && \
apt-cache policy docker-ce && \
apt install -y docker-ce

# Add Jenkins user to Docker group
RUN usermod -aG docker jenkins

# Additional tools for remote management and permission handling
RUN apt install -y sudo sshpass

This Dockerfile performs the following tasks:

  1. Starts with the Jenkins base image.
  2. Installs necessary packages such as curl, ca-certificates, and other utilities.
  3. Adds Docker’s GPG key and repository.
  4. Installs Docker inside the container.
  5. Adds the Jenkins user to the Docker group, enabling it to execute Docker commands.

Saving the Dockerfile

Save this content into a file named Dockerfile in your working directory.

Step 2: Build the Custom Docker Image

Now, let’s build the custom Jenkins Docker image with Docker installed inside. Run the following command in your terminal:

sudo docker build -t jenkins-docker-custom .

This command creates a new Docker image named jenkins-docker-custom. Docker will read the Dockerfile instructions to build the image, installing Docker within Jenkins to enable CI/CD pipelines that require Docker commands.

Step 3: Run the Jenkins Container

Once the image is ready, you can run a container from it. This command mounts necessary directories and assigns ports:

sudo docker run -d \
-v [root-storage-data]:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-p 8080:8080 \
--restart always \
--name jenkins-docker \
jenkins-docker-custom
  • -v [root-storage-data]:/var/jenkins_home mounts your Jenkins data directory to persist data.
  • -v /var/run/docker.sock:/var/run/docker.sock mounts the Docker socket to allow Jenkins to communicate with Docker.
  • -p 8080:8080 maps the container’s port 8080 to your localhost for web access.
  • --restart always restarts the container automatically if it crashes or the system reboots.
  • --name jenkins-docker assigns a name to the container for easy management.

Accessing Jenkins

After starting the container, you can access Jenkins by navigating to http://localhost:8080 in your web browser. You’ll see the Jenkins setup screen, where you can configure the initial setup by following the prompts.

Step 4: Running Docker Compose (Optional)

To use Docker Compose within the Jenkins container, you’ll need to add execute permissions to the docker-compose binary. First, enter the container with the following command:

sudo docker exec -it jenkins-docker /bin/bash

This command will open a shell session inside your running Jenkins container (jenkins-docker is the container name given in Step 3).

Once inside the container, run the following command to make Docker Compose executable:

chmod +x /usr/local/bin/docker-compose

This command ensures that Docker Compose is executable, which is essential if your CI/CD pipeline involves multiple Docker containers managed by Docker Compose.

Step 5: Testing Your Setup

Once Jenkins is running, test your setup by creating a simple Jenkins pipeline that builds a Docker image. You can confirm everything is working by navigating to http://localhost:8080 and setting up a basic job in Jenkins that uses Docker commands to build and push an image. This ensures Jenkins can interact with Docker seamlessly.

Conclusion

Deploying Jenkins in a Docker container allows you to manage your CI/CD pipeline with flexibility and portability. With the custom Dockerfile and Docker Compose configuration in place, Jenkins is now capable of managing Dockerized applications directly from within the container. This setup is ideal for development teams looking to streamline their CI/CD workflows while maintaining an isolated, scalable environment.

By following this guide, you’ve set up a powerful foundation for deploying applications more efficiently. Now, you’re ready to harness the full potential of Jenkins and Docker for your CI/CD pipelines!

Explore more fascinating blog posts on our site!

1 thought on “How to Deploy Jenkins in a Docker Container”

Leave a Comment