From a43bb1bb40d73974f1541416b0eabd8e05fcdd04 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Sun, 8 Aug 2021 17:39:39 +0100 Subject: [PATCH] Improve Dockerfile This commit makes several changes to the dockerfile: - Add go.mod and go.sum in a seperate stage, subsequently calling `go mod download` to make it cache dependencies and speed up builds - Use ubuntu:latest (28MB larger) instead of scratch, makes the image a lot easier to debug (e.g. it has a shell and a package manager) - Change ENTRYPOINT to CMD, this makes the behaviour of the image slightly different from a CLI perspective, but makes interacting with the image from code, docker-compose and kubernetes easier. --- Dockerfile | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index e3fd8ef..0c2af33 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,19 @@ FROM golang:latest AS build ENV GOPATH /go -COPY . /go/src/headscale + +COPY go.mod go.sum /go/src/headscale/ WORKDIR /go/src/headscale +RUN go mod download + +COPY . /go/src/headscale + RUN go install -a -ldflags="-extldflags=-static" -tags netgo,sqlite_omit_load_extension ./cmd/headscale RUN test -e /go/bin/headscale -FROM scratch -COPY --from=build /go/bin/headscale /go/bin/headscale +FROM ubuntu:latest + +COPY --from=build /go/bin/headscale /usr/local/bin/headscale ENV TZ UTC + EXPOSE 8080/tcp -ENTRYPOINT ["/go/bin/headscale"] +CMD ["headscale"]