Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice/way to develop Golang app to be run in Docker container

Basically what the title says... Is there a best practice or an efficient way to develop a Golang app that will be Dockerized? I know you can mount volumes to point to your source code, and it works great for languages like PHP where you don't need to compile your code. But for Go, it seems like it would be a pain to develop alongside Docker since you pretty much only have two options I guess.

First would be to have a Dockerfile that is just onbuild so it starts the go app when a container is run, thus having to build a new image on every change (whether it be small or not). Or, you do mount your source code dir to the container dir, then attach to the container itself and do the manual go build/run yourself as if you would normally.

Those two ways are really the only way that I see it happening unless you just don't develop your Go app in a docker container. Just develop it as normal, then use the scratch image method where you pre build the Go into a binary then copy that into your container when you are ready to run it. I assume that is probably the way to go, but I wanted to ask more professional people on the subject and maybe get some feedback on the topic.

like image 760
ZeroHour Avatar asked May 19 '17 04:05

ZeroHour


People also ask

How do I Dockerize a Go program?

In order to create a Dockerfile for our application, create a new Dockerfile in the root of our project. Inside the Dockerfile , proceed to write the instructions needed to build our Docker image for our Go application. Add a line inside the file that tells Docker which base image we should use for our application.

Is it a good practice to run Docker compose in production?

Docker Compose is an excellent tool for optimizing the process of creating development, testing, staging, and production environments. With Docker Compose, you'll use a single file to build your environment instead of several files with complex scripts with a lot of branching logic.


1 Answers

Not sure it's the best pratice but here is my way.

  1. Makefile is MANDATORY
  2. Use my local machine and my go tools for small iterations
  3. Use a dedicated build container based on golang:{1.X,latest}, mount code directory to build a release, mainly to ensure that my code will build correctly on the CI. (Tips, here is my standard go build command for release build : CGO_ENABLED=0 GOGC=off go build -ldflags -s -w)
  4. Test code
  5. Then use a FROM scratch to build a release container (copy the bin + entrypoint)
  6. Push you image to your registry

Steps 3 to 6 are tasks for the CI.

Important note : this is changing due to the new multistage builds feature : https://docs.docker.com/engine/userguide/eng-image/multistage-build/, no more build vs release containers.

Build container and release container will be merged in one multistage build so one Dockerfile with (not sure about the correct syntax but, you will get the idea) :

FROM golang:latest as build
WORKDIR /go/src/myrepos/myproject
RUN go build -o mybin

FROM scratch
COPY --from=build /go/src/myrepos/myproject/mybin /usr/local/bin/mybin
ENTRYPOINT [ "/usr/local/bin/mybin" ]
like image 174
papey Avatar answered Sep 21 '22 19:09

papey