87 lines
2.4 KiB
Docker
87 lines
2.4 KiB
Docker
# syntax=docker/dockerfile:1.6
|
|
|
|
ARG GO_VERSION=1.25
|
|
ARG GITEA_USER
|
|
ARG GITEA_TOKEN
|
|
|
|
############################
|
|
# Build stage
|
|
############################
|
|
FROM harbor.maximumdirect.net/proxy-dockerhub/golang:${GO_VERSION}-bookworm AS build
|
|
|
|
WORKDIR /src
|
|
ARG GITEA_USER
|
|
ARG GITEA_TOKEN
|
|
ENV GOPRIVATE=gitea.maximumdirect.net/ejr/* \
|
|
GONOSUMDB=gitea.maximumdirect.net/ejr/*
|
|
|
|
# Install baseline packages
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
ca-certificates tzdata git build-essential \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Cache dependencies first
|
|
COPY go.mod go.sum ./
|
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
|
if [ -n "${GITEA_USER}" ] && [ -n "${GITEA_TOKEN}" ]; then \
|
|
git config --global url."https://${GITEA_USER}:${GITEA_TOKEN}@gitea.maximumdirect.net/".insteadOf "https://gitea.maximumdirect.net/"; \
|
|
fi && \
|
|
go mod download && go mod verify && \
|
|
rm -f /root/.gitconfig
|
|
|
|
# Copy the rest of the source
|
|
COPY . .
|
|
|
|
# Default to a static build (no CGO)
|
|
# If errors, can build with: --build-arg CGO_ENABLED=1
|
|
ARG CGO_ENABLED=0
|
|
ARG TARGETOS=linux
|
|
ARG TARGETARCH=amd64
|
|
ENV CGO_ENABLED=${CGO_ENABLED} \
|
|
GOOS=${TARGETOS} \
|
|
GOARCH=${TARGETARCH}
|
|
|
|
# Run tests before building the final binary
|
|
RUN --mount=type=cache,target=/go/pkg/mod \
|
|
--mount=type=cache,target=/root/.cache/go-build \
|
|
go test ./...
|
|
|
|
# Build the cmd entrypoint
|
|
RUN --mount=type=cache,target=/root/.cache/go-build \
|
|
go build \
|
|
-trimpath \
|
|
-ldflags="-s -w" \
|
|
-o /out/weatherapi \
|
|
./cmd/weatherapi
|
|
|
|
|
|
############################
|
|
# Runtime stage
|
|
############################
|
|
FROM harbor.maximumdirect.net/proxy-dockerhub/debian:bookworm-slim AS runtime
|
|
|
|
# Install runtime necessities
|
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
ca-certificates tzdata curl \
|
|
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
# Define /weatherapi as the working directory
|
|
WORKDIR /weatherapi
|
|
|
|
# Create an unprivileged user
|
|
RUN useradd \
|
|
--uid 10001 \
|
|
--no-create-home \
|
|
--shell /usr/sbin/nologin \
|
|
weatherapi
|
|
|
|
# Copy the binary
|
|
COPY --chown=weatherapi:weatherapi --from=build /out/weatherapi /weatherapi/weatherapi
|
|
COPY --chown=weatherapi:weatherapi config.yml /weatherapi/config.yml
|
|
COPY --chown=weatherapi:weatherapi templates /weatherapi/templates
|
|
|
|
USER weatherapi
|
|
|
|
# The application expects config.yml in the same directory as the binary
|
|
ENTRYPOINT ["/weatherapi/weatherapi"]
|