Running Jenkins on Docker — for a newbie

Tharaka Devinda
GDG SRILANKA
Published in
3 min readSep 10, 2018

I never learnt software the “right” way. Most of the stuff I have learnt is thru jumping into the river without knowing how to swim. So these big deal CI/CD things never amused me. It was compile — sftp — run — repeat for me.

But when I wanted to run Jenkins and even more, do it on top of Docker, I never looked elsewhere than the docker hub and downloaded the image. After trying out several searches which said “This image is deprecated”, I found the right one and ran it. This story is for you who might be doing the same. (This might be obsolete, or I may be doing it wrong. But I got it to work, and that might help someone, so here it is…)

docker run -it -p8080:8080 jenkins/jenkins:lts

Whats more to ask?

Well, that’s where all the trouble began. When I ran a simple multi-branch pipeline as it was said in the tutorial page, I ran into the problem. Jenkins says: docker not found. I have no idea why everything does not work out of the box on a Docker image. :/

Yeah..pretty much WTF stuff. So I tried to fiddle with the installation until I realized docker not found means, the docker container which I’m running Jenkins, wants docker, to spawn a maven:3.3.3 image. Well, that’s unfortunate, because I can’t run docker inside docker. (Inception anyone?)

I spent almost a day trying to fiddle with things and get docker to work. Started with mounting the docker socket of the host inside the container.

docker run -it -v /var/run/docker.sock:/var/run/docker.sock -p8080:8080 jenkins/jenkins:lts

Nope. Doesn’t work.

The trick I did next was to try and install docker on the container itself. Which was again a blocker. The user in the image is ‘jenkins’. That user is pretty handicapped. Cannot issue sudo commands and cannot su. So no apt-get installs for him.

Then I got the tip from a friend that the Docker container can be issued a USER root command to switch to root. So I built a custom docker image just for this purpose. That is when at least it started working. So the Dockerfile goes as;

FROM jenkins/jenkins:lts
USER root
RUN apt-get update
RUN curl -sSL https://get.docker.com/ | sh

So what this does is to switch the user to root an then update and install docker (client) on the image. Remember this is only the client. There is no inception limbo stuff here. Just build this image and you are ready to use Jenkins. To build (cd to where the Dockerfile is);

sudo docker build -t yourusername/jenkins .

Then run the container. Remember you still need to mount the docker socket into the jenkins container. So;

sudo docker run -d -p8080:8080 -v /var/run/docker.sock:/var/run/docker.sock yourusername/jenkins

That’s it. It will spawn Jenkins and it will be able to access docker. All the images it pulls and runs, will actually run on the host machine. Ah and one last note: Do NOT try this on Windows. I have no idea how to do that socket mount on Windows. Maybe if someone does, they can leave that in the comments.

--

--