Cross Compiled Go with Alpine Linux make a tiny Docker image
20 Aug 2015Update: Docker has since released Multi-stage builds to address the problem described below. It’s a far better solution, and you can consider the below article deprecated. Check it out!
Golang’s ability to create a self-contained executable makes deployment a breeze. You just copy the single file. No need to worry about versioned dependencies and your dependencies’ dependencies.
Golang’s compiler goes ever further by supporting cross compilation. Know your target architecture? Compile directly for it from your dev box. Linux example below:
Couple this with a minimalist docker image, such as alpine, and you have yourself a tiny 40MB image.
Dockerfile
Sometimes you’ll need an extra thing or two, like CA certificates to connect to HTTPS with SSL:
Dockerfile
Compare this to my original 500MB image built from the golang base image:
Dockerfile
With the new minimalist Dockerfile, pushing incremental changes only sends a trivial 11MB and the image is now a reasonable 40MB. A far cry from the hundred MB sized docker images floating around.
Cross Compiled Go + Alpine Linux + Docker = Win