Skip to content

Generate a Dockerfile for you Go microservice, the Dockefile compiles, get the dependencies with godep, and sets the entrypoint

License

Notifications You must be signed in to change notification settings

dmcd/godockerize

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

godockerize

I was tired to write the same Dockerfile again and again, so I did a simple tool to generate, a Dockerfile for your Go microservice.

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

The Dockerfile is inspired by this two blog post.

Install

$ go get github.com/dmcd/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 .

Build from the scratch image instead of using golang image

Warning Only tested in Mac, you have to enable cross compilation, check below to see how to do it

If you use the flag 'scratch', the script is going to:

  1. Crosscompile the project for linux
  2. Generate a Dockerfile ready to build, from the scratch
# Go to the root directory of your project, for example
$ cd $GOPATH/src/github.com/dahernan/gopherscraper

# Run godockerize using -scratch
$ godockerize -expose 3001 -scratch

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

How to enable cross compile on Mac to build Linux executable

Follow this steps

$ cd /usr/local/go/src

$ sudo GOOS=linux CGO_ENABLED=0 ./make.bash --no-clean

You can try it doing for example:

$ GOOS=linux CGO_ENABLED=0 go build -o gopherscraper

$ file gopherscraper
gopherscraper: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

About

Generate a Dockerfile for you Go microservice, the Dockefile compiles, get the dependencies with godep, and sets the entrypoint

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%