Here is the Dockerfile for a small golang webservice I wrote, which I managed to make quite small. Saving it for myself and I guess I may as well share it since I think it will otherwise be thrown away.
Note I’ve replaced the more project-specific stuff. Like the project name and package. Probably there is some smarter way to compile it with the correct path.
# The file has two steps, first the builder then the actual running image. # This part identifies it as the builder. FROM golang:1.10 as builder # Create appuser to avoid running as root later RUN adduser --system appuser # Import the project onto where it would go on the gopath WORKDIR /go/src/github.com/<username>/<project> COPY . . # Fetch dependencies RUN go get -d -v ./... # Compile the binary. Mind the flags because it has to work in the next image. RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo . # Here we start the actual image. Scratch is super barebones. FROM scratch WORKDIR / # Copy over the files we need. # The ssl certs are needed for doing any kind of ssl connection COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ # The actual binary. COPY --from=builder /go/src/github.com/<username>/<project>/<project> /<project> COPY --from=builder /etc/passwd /etc/passwd # Use the non-root user. USER appuser # The tcp port to expose, in my case 1323 which is the default of the framework I use. EXPOSE 1323 # Start the binary as the entrypoint. ENTRYPOINT ["/<project>"]
When building this Dockerfile for my project, I get an image that is only 13.1MB big. Pretty good, compared to other services in e.g. NodeJS or Python that I have made before that usually end up 300MB+.