This post solves the common problem of running Dockerized projects inside Jenkins when Jenkins itself is running as a Docker container. The default Jenkins image leads to permission errors when building Docker containers in pipelines. The author provides a straightforward solution by creating a custom Jenkins image with the Docker CLI installed and configuring the Jenkins user to access Docker commands. A sample Dockerfile and usage instructions are included, making it easy to build and run a Jenkins container capable of running Docker builds inside. This helps developers integrate Docker builds within Jenkins pipelines smoothly.
There was the exact word that I used in a Google search recently, and it was challenging to find the correct answer. In the end, I didn't find it. So, I'm writing my solution to help others with that familiar doubt. First, we must install Jenkins Docker; you can discover how in the official documentation here.
But, if you only follow the documentation, you can run Jenkins to build standard pipelines and almost all kinds of projects. However, if you try to run a "Dockerized" project, you'll receive a permission access error.
To resolve this problem, I had to create a custom Jenkins image and install a Docker client together in this image, as you can see in the Dockerfile below.
FROM jenkins/jenkins:lts
# Switch the user to root to install the docker CLI
USER root
# Install docker CLI inside the container
RUN curl -sSL https://get.docker.com/ | sh
# Add Jenkins user to docker group (run without sudo)
RUN usermod -aG docker jenkins
# Switch back user to Jenkins
USER jenkins
Now, just run:
docker build -t my-custom-jenkins .
docker run -it -t -p 8080:8080 \
--volume jenkins-data:/var/jenkins_home \
--volume /var/run/docker.sock:/var/run/docker.sock my-custom-jenkins
After that, you can build and run a Docker container with Jenkins running inside the Docker container. This simple blog post helps you as it helps me. Thank you. See you.
Comments (0)
No comments yet. Be the first to comment!