Skip to content

Commit

Permalink
feat: faster Docker multi-platform builds and shrink Go binaries (#498)
Browse files Browse the repository at this point in the history
* feat(docker): faster multi-platform builds

See: https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/

* feat(make): disable symbol table and DWARF generation

by adding -s and -w options to -ldflags

See: https://pkg.go.dev/cmd/link
  • Loading branch information
WaterLemons2k authored Jan 30, 2023
1 parent 3adfc85 commit 606b44a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
# build stage
FROM golang:1.19 AS builder
FROM --platform=$BUILDPLATFORM golang:1.19-alpine AS builder

WORKDIR /app
COPY . .
RUN go env -w GO111MODULE=on \
ARG TARGETOS TARGETARCH
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
&& apk add git make tzdata \
&& go env -w GO111MODULE=on \
&& go env -w GOPROXY=https://goproxy.cn,direct \
&& make clean build
&& GOOS=$TARGETOS GOARCH=$TARGETARCH make clean build

# final stage
FROM alpine
LABEL name=ddns-go
LABEL url=https://github.com/jeessy2/ddns-go

WORKDIR /app
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories \
&& apk add --no-cache tzdata
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
ENV TZ=Asia/Shanghai
COPY --from=builder /app/ddns-go /app/ddns-go
EXPOSE 9876
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DIR_SRC=.
DOCKER_CMD=docker

GO_ENV=CGO_ENABLED=0
GO_FLAGS=-ldflags="-X main.version=$(VERSION) -X 'main.buildTime=`date`' -extldflags -static" -trimpath
GO_FLAGS=-ldflags="-X main.version=$(VERSION) -X 'main.buildTime=`date`' -extldflags -static -s -w" -trimpath
GO=$(GO_ENV) $(shell which go)
GOROOT=$(shell `which go` env GOROOT)
GOPATH=$(shell `which go` env GOPATH)
Expand Down

0 comments on commit 606b44a

Please sign in to comment.