Go is all of the rage, all my friends are doing it so I feel like I have to do it too... So I've been playing with Go and Docker a bit recently to try and learn more about go and how I can use it for work and side projects.
I'm enjoying things so far, and I'm finding interesting tidbits as I go. This is one of them.
With go it is easy to build staticly linked binaries which can be distributed. This is awesome by itself, but with docker it is even more awesome.
You can build your go binaries like so:
CGO_ENABLED=0 go build -a ../src/worker.go
Once you've built your staticly linked binary you can build a docker image on top of the
scratch base image. You can read about the Scratch Image on the docker site, but basically its an empty image that you can drop your binary into that will run as a very minimal container.
FROM scratch MAINTAINER Jake Dahn <firstname.lastname@example.org> ADD bin/worker /worker ENTRYPOINT ["/worker"]
Below is output of some shell output from building and running a docker container that contains a go binary. Notice the image size is 5MB. If I use ubuntu or other distros as a base image my docker image is significanly larger:
root@packer-vmware-iso:/vagrant# docker pull scratch Pulling repository scratch 511136ea3c5a: Download complete Status: Image is up to date for scratch:latest root@packer-vmware-iso:/vagrant$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE scratch latest 511136ea3c5a 16 months ago 0 B root@packer-vmware-iso:/vagrant$ docker build -t jake . Sending build context to Docker daemon 53.73 MB Sending build context to Docker daemon Step 0 : FROM scratch ---> 511136ea3c5a Step 1 : MAINTAINER Jake Dahn <email@example.com> ---> Running in 27f068468755 ---> a063241634df Removing intermediate container 27f068468755 Step 2 : ADD bin/worker /worker ---> 5053eed5787c Removing intermediate container 3a36a6c5e19b Step 3 : ENTRYPOINT /worker ---> Running in 1f8014d919b5 ---> ae0bd28d3e2e Removing intermediate container 1f8014d919b5 Successfully built ae0bd28d3e2e root@packer-vmware-iso:/vagrant$ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE jake latest ae0bd28d3e2e 4 seconds ago 5.191 MB scratch latest 511136ea3c5a 16 months ago 0 B root@packer-vmware-iso:/vagrant$ docker run -t -i jake 2014/10/24 05:40:02 [*] Waiting for messages. To exit press CTRL+C # this is a running go program that is a rabbitmq consumer
This is an interesting way to ship and deploy software.