MOVED: I've created a new follow on project for Go and other languages here: https://github.com/treeder/dj
This is a Docker image to help you develop in Go (golang). The great thing is you don't need to have anything installed except Docker, you don't even need Go installed. See this post about developing with Docker.
This image can perform the following functions:
- vendor - vendors your dependencies into a /vendor directory.
- build - builds your program using the vendored dependencies, with no import rewriting.
- remote - this one will produce a binary from a GitHub repo. Equivalent to cloning, vendoring and building.
- image - this will build and create a Docker image out of your program.
- cross - cross compile your program into a variety of platforms. Based on this.
- static - statically compile your program. This is great for making the tiniest Docker image possible.
Usage
Vendor dependencies:
docker run --rm -it -v "$PWD":/app -w /app treeder/go vendor
You may need to add more options if you have subdirectory imports:
docker run --rm -it -v "$PWD":/app -w /app -e "SRCPATH=github.com/username/reponame" treeder/go vendor
The SRCPATH should match your local import statements. Only required if you have subdirectories in the current repository that you are using in imports.
Build:
docker run --rm -v "$PWD":/app -w /app treeder/go build
Run:
This is just a normal Docker run. I'm using iron/base here because it's a tiny image that has everything you need to run your Go binary on.
docker run --rm -v "$PWD":/app -w /app -p 8080:8080 iron/base ./app
Format:
docker run --rm -v "$PWD":/app -w /app treeder/go fmt
Advanced Commands
Build a remote git repo:
This produces a binary given a remote git repo containing a Go program.
docker run --rm -v "$PWD":/app -w /app treeder/go remote https://github.com/treeder/hello-app.go.git
You'll end up with a binary called app
which you can run with the same command as above.
Build a Docker image out of your program:
This will build a Docker image with your program inside it.
The argument after image is IMAGE_NAME:tag
. Also, note the extra mount here, that's required to talk to the Docker host.
docker run --rm -v "$PWD":/app -w /app -v /var/run/docker.sock:/var/run/docker.sock treeder/go image username/myapp:latest
Boom, creates a small Docker image for you. Run docker images
to check it out, should be about ~12MB total.
Test your new image:
docker run --rm -v "$PWD":/app -w /app -p 8080:8080 username/myapp
Cross compile:
This uses a different image, treeder/go-cross, to do a cross compile.
docker run --rm -v "$PWD":/app -w /app treeder/go-cross cross
Build static binary:
This is great for making the tiniest Docker image possible
docker run --rm -v "$PWD":/app -w /app treeder/go static
Check Go version:
docker run --rm treeder/go version
Wrapper Script
We've provided a wrapper.sh
script to make running each command
more convenient.
To use it, download it and put it in your $PATH
. Then, create an alias for it
by pasting the following into your shell config file (e.g. .bashrc
, .zshrc
, etc...):
alias dgo='wrapper.sh'
Now you're ready to run the wrapper. It supports the following commands:
dgo build
dgo cross
dgo static
dgo vendor
dgo image
dgo run
dgo run-static
TODO:
...
Building this image
docker build -t treeder/go:latest .
Tag it with Go version too (can check with docker run --rm treeder/go version
):
docker tag treeder/go:latest treeder/go:1.4.2
Push:
docker push treeder/go