Files
mtusi2026lab4/Dockerfile
T
2026-10-21 21:22:42 +03:00

54 lines
2.0 KiB
Docker

# syntax=docker/dockerfile:1.7
ARG GO_VERSION=1.27.1
ARG ALPINE_VERSION=3.22
FROM golang:${GO_VERSION}-alpine AS build
# Platform of the produced binaries. Left empty on purpose: the classic builder
# does not supply these args, and the previous hardcoded `amd64` default silently
# produced x86-64 binaries inside an arm64 image. Those ran under emulation and
# the Go runtime crashed under load (fatals like "found pointer to free object").
# Empty means "build for the builder's own platform"; cross-compile explicitly:
# docker build --build-arg TARGETARCH=amd64 ...
ARG TARGETOS
ARG TARGETARCH
ARG VERSION=dev
ENV GOTOOLCHAIN=local
WORKDIR /opt
COPY go.mod go.sum ./
RUN go mod download
# The Go compiler spikes well above 1 GiB per package on large dependencies, so
# an unbounded parallel build OOMs small Docker VMs and CI runners. Cap
# parallelism; override with --build-arg GOFLAGS=-p=$(nproc) on a bigger host.
ARG GOFLAGS="-p=2"
ENV GOFLAGS=$GOFLAGS
COPY . .
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build -trimpath -buildvcs=false -tags timetzdata \
-ldflags="-s -w -X main.version=${VERSION}" \
-o /out/messenger ./cmd/messenger \
&& CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} \
go build -trimpath -buildvcs=false -tags timetzdata \
-ldflags="-s -w -X main.version=${VERSION}" \
-o /out/migrate ./cmd/migrate
FROM alpine:${ALPINE_VERSION} AS runtime
RUN apk add --no-cache ca-certificates wget \
&& addgroup -S -g 10001 app \
&& adduser -S -D -H -u 10001 -G app app
COPY --from=build --chown=10001:10001 /out/messenger /usr/local/bin/messenger
COPY --from=build --chown=10001:10001 /out/migrate /usr/local/bin/migrate
COPY --chown=10001:10001 config/dev/config.yaml /etc/messenger/dev/config.yaml
USER 10001:10001
EXPOSE 8080
STOPSIGNAL SIGTERM
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
CMD wget -q -O /dev/null "http://127.0.0.1:${SERVER_PORT:-8080}/health/live" || exit 1
ENTRYPOINT ["/usr/local/bin/messenger"]