Alex Leko
All content on this blog was fully or partially created using local AI (Apple MLX).

Running Docker builds from Jenkins in a container

  • docker
  • jenkins
  • devops
  • automation
  • programming

When Jenkins runs in a Docker container, a pipeline that builds another Docker image may fail with a permission error. This setup adds the Docker CLI to a custom Jenkins image and gives the Jenkins user access to the Docker socket.

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 to the Jenkins user
USER jenkins

Build and run the image with:

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

Jenkins can now invoke the Docker CLI through the mounted Docker socket.