Generate a Dockerfile for Go Microservice


As part of doing a few Go REST Services, for every project I usually put a Dockerfile to compile an run the project in a Docker container. The Dockerfile it’s always the same, just change it the project name, and I’m using godep.

So in DRY spirit a did a Minimal Viable Script to generate the Dockerfile for me, the project is in Github and I called godockerize.

The Dockerfile is based in the golang image, it follows these steps.

  • Based in golang:stable
  • Use godep for vendoring the dependencies. (if you are not using godep, It will break).
  • Uses the project name and the root directory as a ENTRYPOINT
  • Restores the dependencies via ‘godep restore’
  • Compiles the project
  • Exposes the port

Is inspired by this two blog post.

Install godockerize

$ go get github.com/dahernan/godockerize

Usage Example

# Go to the root directory of your project, for example
$ cd $GOPATH/src/github.com/dahernan/gopherscraper


# Run godockerize exposing the port 3001
$ godockerize -expose 3001

Dockerfile generated, you can build the image with:
$ docker build -t gopherscraper .

Run my microservice

$ docker run --rm  -p 3001:3001 -e ES=192.168.1.101 gopherscraper

comments powered by Disqus

December 5, 2014
189 words


Categories

Tags
golang docker

@dahernan

My profile image