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 [ "" ] |
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)
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. 😄