Skip to content

Commit 2562b74

Browse files
BenTheElderaojea
authored andcommitted
optimize image build
- set GOARCH from TARGETARCH - cache go modules independently of source code changes - don't set redundant GOOS (the build env is inherently linux)
1 parent fe6605d commit 2562b74

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Dockerfile

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
ARG GOARCH="amd64"
16-
17-
FROM golang:1.24 AS builder
18-
# golang envs
19-
ARG GOARCH="amd64"
20-
ARG GOOS=linux
21-
ENV CGO_ENABLED=0
15+
# setup cross-compile env
16+
FROM --platform=$BUILDPLATFORM golang:1.24 AS builder
17+
ARG TARGETARCH
18+
ARG GOARCH=${TARGETARCH} CGO_ENABLED=0
2219

20+
# cache go modules
2321
WORKDIR /go/src/app
24-
COPY . .
22+
COPY go.mod go.sum .
2523
RUN go mod download
26-
RUN CGO_ENABLED=0 go build -o /go/bin/dranet ./cmd/dranet
2724

25+
# build
26+
COPY . .
27+
RUN go build -o /go/bin/dranet ./cmd/dranet
28+
29+
# copy binary onto base image
2830
FROM gcr.io/distroless/base-debian12
2931
COPY --from=builder --chown=root:root /go/bin/dranet /dranet
3032
CMD ["/dranet"]

0 commit comments

Comments
 (0)