Is there a better way?
I’ve been thinking and writing a lot about Docker lately, and using it in almost everything I do (yes, I have a Docker man crush). Mostly because at Iron.io, we run containers… lot’s of containers. In doing that, I’ve come to realize how insanely powerful Docker can be. It’s not just hype, trust me.
We’ve been showing people how to build and test their Go programs inside Docker containers for a while now, but it’s always been a pain. Putting your code in specific places, setting up a GOPATH, using a tool like godep to vendor dependencies (which has its own requirements like your code must be in version control), mounting directories properly, etc. It can work, but it’s not pretty.
Well, today it just got prettier.
Introducing the Docker Go tool
I’ve created a Docker based Go tool, `treeder/go`, that makes developing in Go uber easy. The only thing you need to have installed is Docker. Here are some of the features and benefits:
- You don’t need to install Go.
- You don’t need to install this tool. :mindblown:
- You don’t need to setup a GOPATH.
- You don’t need put your code in a specific directory (eg: /src/github.com/user/hello) or have the Go directory structure, just put your code anywhere.
- Vendoring without import rewriting.
- Cross compiling.
- Static compiling.
- Build into a Docker image (one command and you’ll end up with a runnable Docker image containing your program).
- Build remote git repos.
Let’s try the basics. First, copy and paste the following program into a file named app.go, in any directory.
Notice it has a dependency? Let’s vendor it:
That will put the dependencies in a /vendor directory.
Now let’s build it:
And run it:
Check http://localhost:8080/ . Boom. We ran that in the iron/base image which is a really small image that has everything needed to run your Go program.
More Cool Stuff
How about we build a runnable Docker image containing our program:
Now run your fresh new image:
Check http://localhost:8080/ again. Boom again. And better yet, the image should be tiny. Run `docker images` to check how big it is, should be about ~12MB total.
Or build a remote repo:
You’ll end up with a binary for that remote repository in the directory where you run that command.
Docker for development wins again. If you’ve developed in Go before, you should probably appreciate the simplicity here. This is still pretty experimental and doesn’t implement a lot of the tool chain yet, but from what I know, this is probably the easiest way to get started with Go. Using this, you can point someone at a repo and they can clone, build and run without any setup other than Docker.
Full documentation and source code for the project is here:https://github.com/treeder/go
I’d love your feedback! Create a Github issue or comment here.