Setting up a local Jenkins instance is useful for testing Jenkins job changes without affecting the production instance. Using Docker makes the setup very straightforward.
As of now, this setup allows you to test Job DSL scripts. Imported Beam jobs will not succeed due to some tools (like gcloud) and credentials missing.
If you haven't yet, install Docker on your machine.
WARNING: Not for production use.
INFO: Changing admin credentials is advised.
You can utilize scripts in this folder to build Docker container with Jenkins, pre-installed plugins and some basic configuration.
docker build -t beamjenkins . docker run -p 127.0.0.1:8080:8080 beamjenkins:latest
-p 127.0.0.1:8080:8080
parameter to docker run
ensures that your Jenkins instance will only be available via localhost and not your machine hostname.Image built via this scripts will contain all required plugins, basic configuration listed in manual setup instructions below and also remap JENKINS_HOME, so it does not map to a volume, but is part of container instead.
This last action will allow you to persist changes you do to your container:
docker ps -a docker commit <container_id> <new_image_name> docker run -p 127.0.0.1:8080:8080 <new_image_name>
Such approach may be handy for debugging, but it is highly advised to keep all data in docker volumes.
When invoking Docker, you can map a local directory to persist state and keep your Jenkins configuration even after machine reboot.
JENKINS_HOME=$(readlink --canonicalize-missing ~/jenkins/homedir) mkdir -p ${JENKINS_HOME} chmod -R 777 ${JENKINS_HOME} docker run -p 127.0.0.1:8080:8080 -v ${JENKINS_HOME}:/var/jenkins_home jenkins/jenkins:lts
Running this command will bring up your Jenkins instance at http://127.0.0.1:8080. And map jenkins_home of running container to JENKINS_HOME on your OS file system.
You can setup your Jenkins instance to look like the Apache Beam Jenkins using these steps. (Running Beam Job DSL groovy scripts)
Later you can fork a new Docker container by copying the contents of the mapped directory and starting Jenkins from the new copy.
This might be useful in case if you want to test new plugins/configuration and want to have a clean roll-back option.
JENKINS_NEWHOME=$(readlink --canonicalize-missing ~/jenkins/homedir_v2) mkdir -p ${JENKINS_NEWHOME} cp -R ${JENKINS_HOME} ${JENKINS_NEWHOME} JENKINS_HOME=${JENKINS_NEWHOME} chmod -R 777 ${JENKINS_HOME} docker run -p 127.0.0.1:8080:8080 -v ${JENKINS_HOME}:/var/jenkins_home jenkins/jenkins:lts
-p 127.0.0.1:8080:8080
parameter to docker run
ensures that your Jenkins instance will only be available via localhost and not your machine hostname.On Beam we configure Jenkins jobs via groovy job dsl scripts. If you want to run those on your docker instance of Jenkins, you will need to do some setup on top of installing default plugins:
By default, Seed job imports DSL job definitions from the Apache Beam Github repository. But there is also a possibility to import these definitions from your local git repository, which makes testing much easier because you don't have to git push every time changes were made.
export BEAM_HOME=~/my/beam/directory
.docker run -d -p 127.0.0.1:8080:8080 -v $BEAM_HOME:/var/jenkins_real_home/beam:ro beamjenkins
. The only difference is the -v option which sets up a bind mount.You can choose any branch from your local repo. Just remember that all changes must be committed. You don’t have to checkout the branch you chose.
When you execute docker run
, it creates a new container for your image. Instead of creating a new container every time, you can restart a previously-exited container:
docker ps # Look for the previous container_id docker start <container_id>
One of the benefits of restarting is that it will have all the data and configuration parameters you have assigned to it upon creation.
docker rm $(docker ps -a -q) docker rmi $(docker images -q) docker volume prune