First time setup with Jenkins on Docker
Like many people in tech, I’m a fundamentally lazy person. When I solve a problem once, I don’t want to have to solve it (or slight variations of it) again. This is especially true for things like automated testing and deployment, which often fall under the dreaded ‘tedious, but critical’ category and can break your project in all sorts of exciting ways if steps are skipped or done incorrectly.
Fortunately, there’s a better way: use an automation server and get friendly robots to do the boring stuff for us!
Less fortunately, it can be a pain to set up a server for the first time, leaving us with yet another thing to struggle with on top of all the work we’re trying to automate away.
Things aren’t as bad as they appear, however: by using Docker — an open source platform for building and managing containers (more on containers in a second) — we can quickly and easily set up an automation server with the popular (and free!) Jenkins service.
Why use Docker with Jenkins?
While you can absolutely use Jenkins without Docker, using Docker provides two big advantages: ease of setup, and replicability. Both of these are due to how Docker uses images and containers.
Docker containers are like running programs — they generally fulfill a specific purpose — like running a Jenkins instance — and can be terminated or restarted similar to a regular executable program. Docker images are like the blueprints for containers, allowing Docker to build and run a container on any system that supports Docker.
Because of this, the same container can potentially run on a Mac, Linux, or modern Windows operating system without modification beyond certain command line options or file system considerations.
What this means for our purposes is that if we have a Docker image for Jenkins we can run a container for Jenkins on any machine that supports Docker with little modification and easily repeat the process across multiple machines.
So what do I need to get started?
Before we get started, be sure to install Docker on the device you plan to use for Jenkins. The official Docker guide is pretty helpful for first-time users and I would recommend walking through at least the first few parts if you’ve never used Docker before.
Aside from having Docker installed and a working connection to the internet, open a terminal and you should be good to go!
NOTE: in this article we assume you are running macOS or Linux. If you’re working with Windows, however, have no fear! The command to start the Jenkins container for Windows is slightly different, but the core concepts translate over.
The magic words
To start Jenkins in Docker, simply run the following command in your terminal:
So what does all that mean?
The Jenkins documentation walks through most of these options, but here’s a quick breakdown:
-u root makes the user the root user for the container, which is necessary for running Jenkins.
--rm is an optional argument that removes the Jenkins container when it is shut down, and helps keep old containers from piling up.
-d is an optional argument to run the container in detached mode, meaning it doesn’t hold up your terminal and doesn’t terminate if you close your terminal.
-p 8080:8080 maps port 8080 on the host machine (the first number) to port 8080 in the Docker container. You can set the first number to a different port if you want, but should leave the second one alone.
-v options assign volumes to the Jenkins container. Volumes provide persistent storage that exists independently from containers but can be attached to one or more containers as necessary.
The first volume argument assigns a volume called
jenkins-data to your container, with the volume itself referencing
/var/jenkins_home on your hard drive. The second volume argument makes the socket for the host Docker process available to Jenkins, which allows Jenkins itself to use Docker images and containers.
--name jenkinsis an optional argument that assigns the name
jenkins to the container. This makes it easier to reference the container via Jenkins since otherwise you’d have to refer to it by its hash id or auto-generated name to do things like
docker container stop <id or name> .
jenkinsci/blueocean is the name of the image we want to use. This image has Jenkins and the Blue Ocean pipeline visualization tool and is Jenkin’s suggested Docker image.
And there you have it: a running Jenkins instance using Docker!
One more thing
Although you now have a running Jenkins instance, if this is your first time using Jenkins on a machine you will likely need to do the first time setup process. While most of this is very straightforward, one part may be confusing: how to get the auto-generated password it asks for before it allows you to create an admin account.
If you ran the command above without the
-d argument, you can find the password in your terminal’s standard output. If you did run it in detached mode, you’ll need to run
docker logs jenkins to view the output from the container (if you didn’t name your container
jenkins you should replace
jenkins in that command with the container id or the container’s name).
Now that you have Jenkins running you can use it to automatically build your projects, run tests, or any number of other things you might want to automate!
For some ideas on how to quickly get a project up and running, I recommend looking at the tutorials in the Jenkins documentation for suggestions on how to start.
Hopefully this helped — let me know in the comments if you had any trouble, or have suggestions for others doing this for the first time.