How to build a custom Docker in Docker image for Jenkins master

Key takeaways:

  • Docker is a containerization tool that packages applications and their dependencies into isolated environments.

  • Jenkins is an automation server that facilitates continuous integration and continuous delivery (CI/CD).

  • A custom Docker image with Docker installed is needed to run Docker containers within a Jenkins container.

  • The setup involves creating a Dockerfile that installs Docker on a Jenkins base image.

  • Building the custom image is done through Docker commands, allowing Jenkins to execute Docker commands internally.

  • The command docker run -d ... is used to start the Jenkins container with configurations for port mapping and volume mounting.

  • Security risks associated with Docker-in-Docker require proper access control and permissions to prevent misuse.

Docker is a containerization tool that creates containers with all the code files, system files, and libraries required to run an application in an isolated environment. The Docker Hub is a collaboration tool for community developers and open-source contributors to create and share Docker images for Docker containers, which makes it relatively easy to build on the already existing Docker environment setups done by others. These images usually contain different kinds of applications and even OS kernels pre-installed on them, so you do not have to install the dependencies yourself.

Jenkins in Docker

Jenkins is an automation tool that helps users build, test, and deploy their code by building automation pipelines that help a developer continuously integrate and deliver. The Docker Hub contains the base image for Jenkins that helps the user simply pull it and run a container for Jenkins and run it without any complicated setup. Sometimes, we might need to run a container inside the already-running Jenkins container. This could be for various reasons, such as isolation, consistency, reproducibility, etc.

Running a docker container inside jenkins
Running a docker container inside jenkins

To run a Docker container within a Jenkins master container, we will create a Dockerfile for a custom Jenkins image with Docker installed. We will use this to define services for Jenkins and Docker-in-Docker (dind), ensuring privileges and dependencies. Build the custom Jenkins image, then run the Docker image to start the container. This setup enables Jenkins to execute Docker commands internally, facilitating integration for CI/CD tasks such as building and deploying Docker images.

Steps to build a custom dind for Jenkins master

To build a custom Docker-in-Docker (DinD) image for a Jenkins master, follow these steps:

  1. Create a directory: Begin by creating a directory on your local machine where you will keep the Dockerfile and related files. You can do this using the following command:

mkdir custom-jenkins
cd custom-jenkins
Creating and navigating to a directory
  1. Create a Dockerfile: Next, create a Dockerfile in the directory. This file will contain the instructions for building your custom Jenkins image.

# Use the official Jenkins base image
FROM jenkins/jenkins:lts
Creating a dockerfile containg custom Jenkins image
  1. Install Docker in the Jenkins base image: Within the Dockerfile, add instructions to update package repositories and install Docker components. Here’s an example of what your Dockerfile might include:

# Update package repositories
USER root
RUN apt-get update && \
apt-get install -y apt-transport-https ca-certificates curl software-properties-common && \
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" && \
apt-get update && \
apt-get install -y docker-ce docker-ce-cli containerd.io
Installing docker in the Jenkins base image
  • Lines 3 to 8: In these lines, we update the system's package repositories and fetch all the required repositories that will be needed to download and install Docker.

  1. Build the custom Jenkins image: Once your Dockerfile is ready, build the custom image using the following command:

docker build -t custom-jenkins .
Building the custom Jenkins image
  1. Run the Docker container: Once the Docker image is created, you can run the Docker container by using the command below, and you will have a Docker container running with Jenkins and Docker-in-Docker enabled. Enter the following command in the terminal above to run your containers:

docker run -d -p 8080:8080 -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock --name jenkins-master custom-jenkins
Docker command to run container

To build the image, run the playground below:

# Using the official jenkins image as the base image
FROM jenkins/jenkins:lts

# Switching to root user
USER root

# Installing Docker dependencies
RUN apt-get update && \
    apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
RUN apt-get update && \
    apt-get install -y docker-ce docker-ce-cli containerd.io

# Add jenkins user to the docker group
RUN usermod -aG docker jenkins

# Switching back to jenkins user
USER jenkins
Creating docker image and run docker container

Explanation

  • -d: Run the container in detached mode.

  • -p 8080:8080: Map port 8080 on the host to port 8080 in the container (Jenkins UI).

  • -v jenkins_home:/var/jenkins_home: Mounts the Jenkins data directory as a volume to persist data.

  • -v /var/run/docker.sock:/var/run/docker.sock: Mounts the Docker socket to communicate with the Docker daemon from within the container.

  • --name jenkins-master: Names the container as “jenkins-master”.

  • custom-jenkins: The name of the Docker image.

Conclusion

Now, you have a Jenkins server running with all the Docker commands, which you can use in Jenkins pipelines using the DinD container. To access the Jenkins server, click the URL provided below the playground. However, be aware that this could lead to some security risks, so before running anything, ensure that you have proper access control and permissions in place so that no misuse of the Docker environment takes place.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How can we install Docker in Jenkins Docker image?

We can install Docker in a Jenkins Docker image by creating a Dockerfile that uses the Jenkins base image, updates the package manager, and installs Docker.


How can we use Jenkins agent Docker?

To use a Jenkins agent in Docker, we can create a Jenkins pipeline that defines a docker agent block.


Can we install Docker inside Docker?

Yes, we can install Docker inside Docker, commonly referred to as Docker-in-Docker (DinD). This allows a Docker container to run Docker commands. However, it’s important to configure it correctly and consider potential security implications. To enable this, we typically mount the Docker socket of the host into the container.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved