Skip to content

Instantly share code, notes, and snippets.

@younisshah
Last active February 16, 2024 04:33
Show Gist options
  • Save younisshah/d492438d250c0a699c00777481d4da77 to your computer and use it in GitHub Desktop.
Save younisshah/d492438d250c0a699c00777481d4da77 to your computer and use it in GitHub Desktop.
Running Docker in Jenkins (in Docker)
  1. Create a Dockerfile
FROM jenkins/jenkins:lts
 
USER root
RUN apt-get update \
      && apt-get install -y sudo \
      && rm -rf /var/lib/apt/lists/*
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers
 
USER jenkins

We need to give the jenkins user sudo privileges in order to be able to run Docker commands inside the container.

  1. Build your jenkins container
$ docker build -t my_jenkins .
  1. Run your container
$ docker run -d -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker -p 8080:8080 -p 50000:50000 my_jenkins
  1. Once your container is running, navigate to http://localhost:8080, install the default plugins and setup your Jenkins admin.
  2. Go to Manage Jenkins -> Manage Plugins. Under the Available tab search for docker and install Docker and Docker Pipeline plugins.
  3. Create a pipeline (Freestyle or Multibranch pipeline) and test!
  4. If you get Docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock, try these:
  5. Add jenkins user to the docker group and restart the Jenkins:
$ sudo usermod -a -G docker jenkins
$ sudo service jenkins restart

You can check it was successful by doing grep docker /etc/group and see something like this:

docker:x:998:jenkins

Then change your users group ID to docker:

newgrp docker

Finally, log out and log in again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment