From 6cf07caaf600118d18e783826b628ffe248aaf5e Mon Sep 17 00:00:00 2001 From: Brad Ison Date: Fri, 25 Sep 2026 12:15:59 +0200 Subject: [PATCH] [misc] Build the upload server from source, nonroot on Chainguard (#7663) upload-server/Dockerfile only packaged the goreleaser-built binary, so the image could not be built from a checkout. It is now a multi-stage build on Chainguard static, running as the nonroot user (uid 65532), with a VARIANT=debug build arg that swaps in busybox for a shell. The goreleaser packaging file moves unchanged to Dockerfile.release and .goreleaser.yaml points at it, so the published netbirdio/upload image stays as it was: distroless and root. The bases are pinned by digest, since Chainguard publishes only :latest for free. A Dependabot docker entry for /upload-server moves them weekly, leaving the release base alone and holding golang to patch updates. --- .github/dependabot.yml | 22 +++++++++++++ .goreleaser.yaml | 2 +- upload-server/Dockerfile | 55 +++++++++++++++++++++++++++++--- upload-server/Dockerfile.release | 4 +++ 4 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 upload-server/Dockerfile.release diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 647e04936..ded77ec58 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -46,3 +46,25 @@ updates: wireguard: patterns: - "golang.zx2c4.com/wireguard*" + + # Base images of the source-build Dockerfiles, pinned by digest (Chainguard + # publishes only :latest for free). Dockerfile.release files feed goreleaser + # and keep the published images as they are, so their bases are left alone. + - package-ecosystem: "docker" + directories: + - "/upload-server" + schedule: + interval: "weekly" + open-pull-requests-limit: 3 + groups: + base-images: + patterns: + - "*" + ignore: + - dependency-name: "gcr.io/distroless/base" + # Go minor and major versions move with the rest of the repository; + # patch releases and new digests of the pinned tag still come through. + - dependency-name: "golang" + update-types: + - "version-update:semver-minor" + - "version-update:semver-major" diff --git a/.goreleaser.yaml b/.goreleaser.yaml index b6c563968..a08a7e92a 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -477,7 +477,7 @@ dockers_v2: tags: - "{{ .Version }}" - "{{ if eq .Env.SKIP_PUBLISH \"false\" }}latest{{ end }}" - dockerfile: upload-server/Dockerfile + dockerfile: upload-server/Dockerfile.release platforms: - linux/amd64 - linux/arm64 diff --git a/upload-server/Dockerfile b/upload-server/Dockerfile index 3713d6f2a..8098a0186 100644 --- a/upload-server/Dockerfile +++ b/upload-server/Dockerfile @@ -1,4 +1,51 @@ -FROM gcr.io/distroless/base:debug -ENTRYPOINT [ "/go/bin/netbird-upload" ] -ARG TARGETPLATFORM -COPY ${TARGETPLATFORM}/netbird-upload /go/bin/netbird-upload +# syntax=docker/dockerfile:1 + +# Builds the upload server from source. Run it from the repository root: +# +# docker build -f upload-server/Dockerfile . +# +# Releases package the goreleaser-built binary with Dockerfile.release instead, +# which keeps the published image as it was (distroless base, running as root). +# +# The image runs as the base image's nonroot user (uid 65532), which owns the +# default STORE_DIR, /var/lib/netbird. A volume mounted there must be writable +# by that uid: a named Docker volume takes the directory's ownership on first +# use, a bind mount needs chown, and Kubernetes needs fsGroup: 65532. +# +# Build args: +# VARIANT=release|debug debug swaps the base for Chainguard busybox (a shell) +# VERSION stamped into the binary the same way goreleaser does +# +# Chainguard publishes only :latest for free, so the bases are pinned by digest +# and moved by Dependabot. + +ARG VARIANT=release + +# Pure Go: cross-compile from the build host instead of emulating the target. +FROM --platform=$BUILDPLATFORM golang:1.26.7-bookworm@sha256:e8c859f5632dcfde7b32d2012b4351728f6437930887c2f6a91ea242459e5514 AS builder +WORKDIR /app + +COPY go.mod go.sum ./ +RUN --mount=type=cache,target=/go/pkg/mod go mod download + +COPY . . +ARG TARGETOS +ARG TARGETARCH +ARG VERSION=development +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -trimpath \ + -ldflags "-s -w -X github.com/netbirdio/netbird/version.version=${VERSION}" \ + -o /out/netbird-upload ./upload-server \ + && mkdir -p /out/var/lib/netbird + +FROM cgr.dev/chainguard/static:latest@sha256:41e17ed83c594a64a9396b6ab96dd26d5ddc290dacf4c177464712ff21ad534f AS base-release +FROM cgr.dev/chainguard/busybox:latest@sha256:b2953ab1cae4a6265e18cf675851bd99975211b150d7a014774911d76eb309ba AS base-debug + +# hadolint ignore=DL3006 +FROM base-${VARIANT} +COPY --from=builder --chown=65532:65532 /out/var/lib/netbird /var/lib/netbird +COPY --from=builder /out/netbird-upload /go/bin/netbird-upload +WORKDIR /var/lib/netbird +USER 65532:65532 +ENTRYPOINT ["/go/bin/netbird-upload"] diff --git a/upload-server/Dockerfile.release b/upload-server/Dockerfile.release new file mode 100644 index 000000000..3713d6f2a --- /dev/null +++ b/upload-server/Dockerfile.release @@ -0,0 +1,4 @@ +FROM gcr.io/distroless/base:debug +ENTRYPOINT [ "/go/bin/netbird-upload" ] +ARG TARGETPLATFORM +COPY ${TARGETPLATFORM}/netbird-upload /go/bin/netbird-upload