[Update: As of the end of 2015, it might be better to use the GoCD Docker images available on Docker Hub as they are newer and better maintained and used by many in the GoCD community]
I'm convinced that the ability to automatically create and destroy build agents is key to having a flexible continuous delivery setup. I'm equally convinced that this particular use case is better satisfied using very fast container technologies instead of virtual machines. This is blog entry talks about using Docker for this challenge.
IMPORTANT It should be noted that this blog entry and code is the result of a spike. This definitely isn't production ready, but could be useful if you're thinking about doing the same sort of thing. This will only work on Linux machines, because Docker only works on Linux machines.
If anyone with more current programmer skills wants to fork and make it more production like, I'd be very happy to assist in any way I can.
Setting it up
- Install Docker
- Install GoCD server
Install at least one GoCD agent if you want to use the pipeline - Yup, even though the whole idea here is to create and destroy agents automatically, we have to install at least one to run the process. The server needs this to actually run the scripts.
Once the GoCD agent is installed, go to the Agents tab in your GoCD user interface and assign this agent a resource of "manager" if you intend to use the GoCD pipeline below.
- In order for GoCD (and anyone else) to run this without having to become root, you'll have to follow the instructions under "Giving non-root access" at http://docs.docker.io/installation/ubuntulinux/. I make absolutely no warranties for the security of following those instructions! Again, the point here (for me) was to spike out creating agents on the fly, this is not production code.
All of the scripts I'm using can be found at https://github.com/kmugrage/go-agent-docker. Please feel free to clone / fork / send pull requests.
This is the file that defines how the docker image will be created. Normally you would run the docker build program against this file, but I used a shell script so that I could call it from GoCD easier later.
- go-agent-14.1.0-18882.deb (no longer included)
Update on 22 June - The Dockerfile has been updated to use wget to download the GoCD 14.1 agent debian installer. If you need the RPM version visit www.go.cd/download and change the Dockerfile to match your system.
You'll need to modify this file to set the IP address of your server. Just to make this more interesting, it needs to be the IP address that Docker is aware of, so probaly not the one you're used to using.
You can find this IP address using ifconfig…
ifconfig | grep -A 1 docker
On my system, that returns…
docker0 Link encap:Ethernet HWaddr 56:84:7a:fe:97:99 inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
Because that's my address, that's what you'll see in the go-agent file. As much as I might appreciate your help in supplying agents for me, they won't be able to connect if you don't make this match your GoCD server.
This file takes advantage of the autoregister feature in GoCD so that I don't have to manually approve the new agents. You'll need to update the key based on the instructions at https://docs.go.cd/current/advanced_usage/agent_auto_register.html
Once you've modified the above, you're ready to build your containers. Execute this script at a command prompt.
This step could take quite a while depending on the speed of your internet connection. It has to download the base box, add Java and a bunch of other things and then create the image you'll be using.
This shell script starts docker containers from the image. You can control how many containers get started by editing the script itself.
This shell script stops and removes all docker containers - WARNING: I do mean all. If you run this on your box every single Docker container will be stopped and deleted. Did I mention this will remove every single container on your machine?
If someone that's better with sed / awk / ? would like to modify to only remove the containers tagged with gocd/go-agent please feel free!
This Ruby script removes disables and removes all agents on the GoCD server tagged with a "docker_created" resource.
Everything above can be run from the command line just fine. Of course I wanted to have a GoCD pipeline manage all of this. This text file shows the pipeline definition for mine. You'll need to change the github URL to point to your own. You're welcome to use mine from above, but I removed it here just so your pipeline doesn't get executed with my GitHub changes if you don't want it to.
It's my sincere hope that my first attempt to actually use Docker (and Ruby for that matter) will help you get something real done. Please feel free to comment / ask questions / etc.