Jenkins Environment using Docker
The Jenkins server itself is limited and relies on numerous community developed plugins for its function, which is a mixed blessing:
The best thing about Jenkins are the plugins. The worst thing about Jenkins are the plugins.
This is a small guide that shows how you can create mini Jenkins environment locally using Docker Compose. For those unfamiliar, Docker Compose is a declarative configuration that describes how to run docker containers. It is easier to use and develop environments than using Docker CLI.
Required Tools (Prerequisites)
IMPORTANT: You need to have access to Unix socket on the host system, which is fine for Linux or macOS. On Windows, which doesn’t have Unix sockets, you can use direct access TCP. This has to be explicitly enabled in Docker Desktop on Windows 10 Pro.
On Linux, you can follow these install guides to gain access to Docker.
Windows 10 Pro
- Docker Desktop for Windows [Windows 10 Pro, will NOT work on Windows 10 Home]
IMPORTANT: You may need to enable the
Expose daemon on tcp://localhost:2375 without TLS in the option in the General section of Docker-Desktop settings on Windows. In the Daemon setting, add the key/value of
"hosts":["tcp://0.0.0.0:2375"]. Thus the Deamon setting would look something like this for example:
Compose Configuration (Linux or macOS)
On macOS or Linux, we first create a Docker Compose configuration called
The easiest way to run this container is in privileged mode, as it will have all the permissions and access needed to build docker containers.
SIDE NOTE: Should you need to run in the default unprivileged mode, you will need to do the following from within the container:
- Add a group
dockerif it does not exist, and add
jenkinsuser to the docker group, e.g.
groupadd docker && usermode -aG docker 'jenkins'.
- Adjust permissions on the
docker.sockfile so that
jenkinsuser can access it, e.g.
chmod 777 /var/run/docker.sock.
IMPORTANT (SECURITY): Don’t do this for a production implementations, as a docker container should always be run in unprivileged, and should NEVER have access to a service running in privilege mode. This guide is intended for local desktop development.
Compose Configuration (Windows)
As it turns out you can get this to work on with Docker Desktop for Windows if you have to enable
Expose daemon on tcp://localhost:2375 without TLS enabled. See the Docker settings (accessed from systray icon on the taskbar).
On Windows, create a
docker-compose.yml file with the following content:
We have to run this as root, as the
jenkins user cannot access the
host.docker.internal address, only root can. If there is a way to work around this and run it as the unprivileged
jenkins user, then drop a comment, I can make an update.
Bring up the environment by simply typing:
docker-compose up. This will print out the initial password you can use to bring up an environment.
If you are using Docker Machine, you will navigate localhost, but run on a different IP address, which you can find out with
Once logged in, you can now create the initial Jenkins account. I typically use
jenkins user for my local environments. Afterward, you can create pipelines.
Testing the Solution
We now have a Jenkins environment, and you can experiment with creating jobs or pipelines. If you already have jobs or pipelines that you know how to create, you can skip this.
This part is for those new to Jenkins pipelines.
You need to have the following for these steps.
- GitHub account or other git service accessible from your desktop
- SSH public key added GitHub account (or alternative git service) so that you can access the git repository through ssh
Creating a Pipeline Steps
For a quick test, you can fork this repository:
Then clone the repository locally.
ACCOUNT="<your github account name goes here>"
git clone firstname.lastname@example.org:$ACCOUNT/simple-node-js-react-npm-app.git
Add a Jenkinsfile with the following content:
After adding this, push the changes:
git add .
git commit -m "Adding CI Pipeline (Jenkinsfile)"
On the local Jenkins server, http://localhost:8080, you can add a new item, chose
OK and select the Pipeline tab, paste the URL of the forked repository:
His save and run this. Click on the
Open Blue Ocean link of the left panel to view the pipeline using the BlueOcean interface, which looks something like this below:
Addendum: Using Docker Machine (optional)
On Windows, you can only use one virtual machine solution at a time.
- Docker Toolbox comes with Docker Machine and Docker Compose tools. [Windows 7, Windows 8.1, Windows 10 Home, Windows 10 Pro]
Some great tutorials from the BlueOcean project. You can skip all the
docker container run steps.