From e0f734c4dfaa0048925b2cb64be3e91635c4781c Mon Sep 17 00:00:00 2001 From: Nourrisse Florian Date: Wed, 22 Apr 2026 15:28:03 +0200 Subject: [PATCH] feat(server): multi-stage Dockerfile that builds Go from source The previous Dockerfile assumed that the Go binary was already built on the host and copied into the build context before running docker build. That required a separate prep step, only worked when the host arch matched the target, and made the docker.sh pre-build shell mandatory. The multi-stage version compiles the server inside the image using golang:1.24-alpine, so a plain "docker build" works out of the box on any platform supported by the builder (amd64, arm64...). This is what lets a homelab node cross-build and run the server natively on Raspberry Pi without juggling pre-compiled binaries. Also: - build with -trimpath and stripped symbols for smaller images - install ca-certificates and tzdata in the runtime layer so TLS and timezone-aware logging work correctly - EXPOSE 12800 so compose tools and scanners see the port - run the binary via absolute path to remove the implicit ./ in CMD --- server/manifest/docker/Dockerfile | 42 ++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/server/manifest/docker/Dockerfile b/server/manifest/docker/Dockerfile index c4828a9..370d00a 100644 --- a/server/manifest/docker/Dockerfile +++ b/server/manifest/docker/Dockerfile @@ -1,7 +1,37 @@ +# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD +# SPDX-License-Identifier: MIT +# +# Multi-stage build. Run from the server/ directory: +# cd server && docker build -t stackchan-server -f manifest/docker/Dockerfile . +# +# Or with docker compose using a Git URL context: +# build: +# context: https://github.com/m5stack/StackChan.git#main:server +# dockerfile: manifest/docker/Dockerfile + +FROM golang:1.24-alpine AS builder + +RUN apk add --no-cache gcc musl-dev sqlite-dev + +WORKDIR /src + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . + +ENV CGO_ENABLED=1 GOOS=linux +RUN go build -trimpath -ldflags="-s -w" -o /out/stackChan main.go + FROM alpine:latest -ENV WORKDIR=/app -WORKDIR $WORKDIR -COPY ./stackChan $WORKDIR/stackChan -COPY ./config.yaml $WORKDIR/config.yaml -RUN chmod +x $WORKDIR/stackChan -CMD ["./stackChan"] \ No newline at end of file + +RUN apk add --no-cache ca-certificates tzdata + +WORKDIR /app + +COPY --from=builder /out/stackChan /app/stackChan +COPY --from=builder /src/manifest/config/config.yaml /app/config.yaml + +EXPOSE 12800 + +CMD ["/app/stackChan"]