Jenkins Environment using Docker
Creating Jenkins Environment for Developing Pipelines
Updated: 2020年2月21日
Overview
Jenkins is a popular open source scheduling and build automation environment that can help you with the discipline of continuous integration.
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)
You need to have Docker installed on the host system, so for macOS or Windows, you will need something like Docker Desktop to managed virtual machines and provide the Docker service.
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.
Linux
On Linux, you can follow these install guides to gain access to Docker.
macOS
On macOS, the Docker Desktop environment (uses HyperKit) offers the smoothest experience:
- Docker Desktop for Mac now bundles Docker Compose
Windows 10 Pro
Only Windows 10 Pro is supported because Hyper-V, required by Docker Desktop, is only supported on Professional version of Windows:
- 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:
{
"debug": true,
"experimental": false,
"hosts":["tcp://0.0.0.0:2375"]
}
The Configuration
Compose Configuration (Linux or macOS)
On macOS or Linux, we first create a Docker Compose configuration called docker-compose.yaml
:
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
docker
if it does not exist, and addjenkins
user to the docker group, e.g.groupadd docker && usermode -aG docker 'jenkins'
. - Adjust permissions on the
docker.sock
file so thatjenkins
user 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.
Instructions
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.
Once the service is ready, you can access it by pointing your web browser to localhost:8080
on either Linux or macOS running Docker for Mac.
If you are using Docker Machine, you will navigate localhost, but run on a different IP address, which you can find out with docker-machine ip
.
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.
Prerequisites
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
git
client tool
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 git@github.com:$ACCOUNT/simple-node-js-react-npm-app.git
cd simple-node-js-react-npm-app
Add a Jenkinsfile with the following content:
After adding this, push the changes:
git add .
git commit -m "Adding CI Pipeline (Jenkinsfile)"
git push
On the local Jenkins server, http://localhost:8080, you can add a new item, chose Pipeline
:
Then hit 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)
For simplicity, I purposefully covered only Docker Engine for Linux, and for systems that need a virtual machine, which is macOS or Windows, I covered Docker Desktop.
Alternatively you can use Docker Machine (not tested with this guide). This solution can support the open source Virtualbox, or other virtual machines, including cloud solutions:
macOS
- Docker Toolbox (optional if above installed) comes with Docker Machine and Docker Compose tools.
Windows
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]
Links
Some great tutorials from the BlueOcean project. You can skip all the docker container run
steps.
- https://jenkins.io/doc/book/pipeline/getting-started/
- https://jenkins.io/doc/tutorials/build-a-node-js-and-react-app-with-npm/
Conclusion
That is all there is to it, a very quick way to get up and running with Jenkins and experiment creating pipelines or other Jenkins configurations.