kSet
kSet

Reputation: 107

Buiding docker containers for golang applications without calling go build

I'm building my first Dockerfile for a go application and I don't understand while go build or go install are considered a necessary part of the docker container.

I know this can be avoided using muilt-stage but I don't know why it was ever put in the container image in the first place.

What I would expect:

My Dockerfile contains not much more than

COPY go-awesome .

CMD ["go-awesome"]

What is the downside of this configuration? What do I gain by instead doing

COPY . .

RUN go get ./...

RUN go install ./..

Links to posts showing building go applications as part of the Dockerfile

https://www.callicoder.com/docker-golang-image-container-example/

https://blog.codeship.com/building-minimal-docker-containers-for-go-applications/

https://www.cloudreach.com/blog/containerize-this-golang-dockerfiles/

https://medium.com/travis-on-docker/how-to-dockerize-your-go-golang-app-542af15c27a2

Upvotes: 4

Views: 1398

Answers (1)

Opnauticus
Opnauticus

Reputation: 670

You are correct that you can compile your application locally and simply copy the executable into a suitable docker image.

However, there are benefits to compiling the application inside the docker build, particularly for larger projects with multiple collaborators. Specifically the following reasons come to mind:

  • There are no local dependencies (aside from docker) required to build the application source. Someone wouldn't even need to have go installed. This is especially valuable for projects in which multiple languages are in use. Consider someone who might want to edit an HTML template inside of a go project and see what that looked like in the container runtime..
  • The build environment (version of go, dependency managment, file paths...) is constant. Any external dependencies can be safely managed and maintained via the Dockerfile.

Upvotes: 1

Related Questions