diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..8ec1a4c7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git/ +.idea/ +vendor/ +dist/ diff --git a/Dockerfile b/Dockerfile index 991302df..11a3977f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,34 +1,25 @@ -FROM golang:1.14-alpine3.11 as build - -RUN apk add --no-cache git tar - -WORKDIR /go/src/github.com/abh/geodns +FROM golang as prepare +COPY go.mod go.sum /goapp/ +WORKDIR /goapp/ +RUN go mod download +FROM golang as build ENV CGO_ENABLED=0 - -ADD vendor/ vendor/ -ADD applog/ applog/ -ADD countries/ countries/ -ADD geodns-logs/ geodns-logs/ -ADD health/ health/ -ADD monitor/ monitor/ -ADD querylog/ querylog/ -ADD server/ server/ -ADD targeting/ targeting/ -ADD typeutil/ typeutil/ -ADD zones/ zones/ -ADD service/ service/ -ADD service-logs/ service-logs/ -ADD .git/ .git/ -ADD *.go build ./ - -RUN ./build -RUN ls -l -RUN ls -l dist - -RUN ln dist/* / +ARG REVISION=${REVISION:-""} +ENV REVISION=${REVISION} +COPY --from=prepare /go/pkg/mod /go/pkg/mod +COPY . /goapp +WORKDIR /goapp/ +RUN go mod vendor +RUN go build -o dist/geodns \ + -mod=vendor \ + -trimpath \ + -ldflags "-X main.gitVersion=$REVISION -X main.buildTime=`TZ=UTC date "+%Y-%m-%dT%H:%MZ"`" \ + -v && \ + (cd geodns-logs && go build -trimpath -mod=vendor -v -o ../dist/geodns-logs && cd ..) FROM scratch -COPY --from=build /geodns-linux-amd64 /geodns -COPY --from=build /geodns-logs-linux-amd64 /geodns-logs +COPY --from=build /goapp/dist/geodns /geodns +COPY --from=build /goapp/dist/geodns-logs /geodns-logs +RUN mkdir ./dns/ ENTRYPOINT ["/geodns"] diff --git a/Makefile b/Makefile index 6911a190..817aae20 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,9 @@ docker-test: .PHONY golang:1.14-alpine3.11 -- \ go test ./... +docker-build: .PHONY + docker build --build-arg REVISION=`git rev-parse --short=5 HEAD` -t geodns . + devel: go build -tags devel