DEV Community

Niklas Merz
Niklas Merz

Posted on • Originally published at blog.merzlabs.com on

4 1

Containerize your Go application with Docker

Containerize your Go application with Docker

I am working on a small project with an API written in Go. After some time in development and testing was getting tired of building the binary and uploading it to a test server via SFTP, so I started to look for way putting the project in a container and deploying it with CI tools. More on the CI part in another post.

After some experiments I found that Jess Frazelle uses a Dockerfile like this for many projects with Go:

FROM golang:alpine as builder
ENV PATH /go/bin:/usr/local/go/bin:$PATH
ENV GOPATH /go
COPY . /go/src/github.com/niklasmerz/myproject
RUN set -x \
&& cd /go/src/github.com/niklasmerz/myproject \
&& go build \
&& mv myproject /usr/bin/myproject \
&& rm -rf /go \
&& echo "Build complete."
FROM alpine:latest
RUN apk add --no-cache \
ca-certificates
COPY --from=builder /usr/bin/myproject /usr/bin/myproject
ENTRYPOINT [ "myproject" ]
CMD [ "" ]
view raw Dockerfile hosted with ❤ by GitHub

I think this is a great and simple solution. It creates a builder container with the Go compiler and builds the binary which gets packed into a small container for deployment.

Top comments (1)

Collapse
 
shostarsson profile image
Rémi Lavedrine

That is a very interesting post.
I have developed something using Go and I would give it a try.
I have never used Docker yet, but I think that it could a good start project to learn Docker. 😄

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay