Przeglądaj źródła

feat(docker): optional server image with embedded UI and agents (#2101)

* feat(docker): server image with embedded UI and agents

Multi-stage Dockerfile that mirrors the release pipeline inside the image:
the frontend is built with node 24 and embedded via staticspa, the five
agent architectures are cross-compiled and UPX-packed with
scripts/build-agents.sh (same script the workflows use), and the server is
linked with the exact .goreleaser.yaml ldflags (BuildCommit,
EmbeddedAgentVersion, httpapi.Version) so /api/health reports and the agent
update flow behave like the release binaries.

Runtime is alpine with ca-certificates, tzdata and openssh-client (gateway
autodetect and inventory call the ssh binary, host key checks call
ssh-keyscan), runs as non-root uid 1000, keeps all state in /data and
healthchecks /api/health with busybox wget. NETPULSE_BIND defaults to
0.0.0.0 in the image because fresh installs bind 127.0.0.1, which published
ports cannot reach.

Also adds docker-compose.yml (single service, named volume, optional .env),
Makefile docker-build/up/down helpers, and docs/docker.md with a README
pointer. The in-app updater stays inert in this mode (canApply false), so
image deploys update via docker pull while agents keep self-upgrading from
the server, version-coherent with the image.

* fix(docker): builder-native multi-arch, loopback publish, OCI labels

Review follow-ups from the upstream discussion (gnacho/netpulse#2097):

- Builder stages run with --platform=$BUILDPLATFORM: under multi-arch
  buildx each platform otherwise runs npm, the five-architecture agent
  build and UPX under QEMU emulation. The agent stage is platform-
  independent already (pure-Go cross compile), and the server stage now
  cross-compiles with GOOS/GOARCH from TARGETOS/TARGETARCH (defaults keep
  a plain docker build working). arm64 builds drop from emulated to
  native toolchain speed.
- Publish the compose port on the docker host loopback by default
  (NETPULSE_PUBLISH_ADDR, 127.0.0.1) so the panel never lands on a
  public interface just because the file was used as-is; docs and README
  examples follow, and the quickstart states that AUTH_PASS is required
  at boot (10+ characters).
- Full OCI labels on the runtime stage: source, revision (BUILD_COMMIT),
  version (VERSION), licenses (AGPL-3.0), so the GHCR package page points
  back to the repo and the built commit.

* docs(docker): point the image references at the upstream registry

ghcr.io/gnacho/netpulse instead of the fork registry in the compose
comment, the quickstart and the updates example.

* docs(docker): compose version note and bridge-network gateway caveat

Two review notes from gnacho/netpulse#2101: the long env_file syntax with
required: false needs Compose 2.24+, and on a bridge network the gateway
autodetect probes the Docker bridge address instead of the LAN, so the
quickstart now points at declaring routers in the UI or via ROUTERS_JSON
(and mentions network_mode: host for full LAN access on Linux).
Evgeniy 2 tygodni temu
rodzic
commit
d6cb09531b
6 zmienionych plików z 338 dodań i 0 usunięć
  1. 22 0
      .dockerignore
  2. 113 0
      Dockerfile
  3. 23 0
      Makefile
  4. 16 0
      README.md
  5. 37 0
      docker-compose.yml
  6. 127 0
      docs/docker.md

+ 22 - 0
.dockerignore

@@ -0,0 +1,22 @@
+# Build context hygiene: the image builds everything it needs inside its own
+# builder stages, so local build artifacts and unrelated trees stay out of the
+# context (smaller uploads, no stale-artifact leaks).
+
+.git
+.github
+data
+deploy
+docs
+web
+
+# Local frontend build output (rebuilt inside the image)
+app/dist
+app/node_modules
+
+# Embedded frontend dist (copied in from the frontend stage)
+server-go/internal/staticspa/dist
+
+# Host-built agent binaries (rebuilt inside the agents stage). Keep .gitkeep
+# so the embed pattern still matches in a dev-style build.
+server-go/internal/agentbin/agents/*
+!server-go/internal/agentbin/agents/.gitkeep

+ 113 - 0
Dockerfile

@@ -0,0 +1,113 @@
+# syntax=docker/dockerfile:1
+
+# NetPulse server image: the web UI (app/) and the agent binaries are
+# embedded into the Go binary the same way the release builds do it
+# (.goreleaser.yaml + scripts/build-agents.sh). The agents are only payload:
+# the server serves them for install, nothing agent-related runs here.
+#
+# Build args mirror the release ldflags; defaults produce a coherent
+# dev-stamped image (version 0.0.0-docker, agents stamped 0.0.0-docker):
+#
+#   docker build -t netpulse .
+#   docker build \
+#     --build-arg VERSION=2.51.0 \
+#     --build-arg BUILD_COMMIT=abc1234 \
+#     --build-arg AGENT_VERSION=3.0.45 \
+#     -t netpulse:2.51.0 .
+#
+# CI passes the real values on tag builds (.github/workflows/docker.yml).
+
+ARG NODE_VERSION=24
+ARG GO_VERSION=1.26
+
+# ---- Frontend: app/ -> app/dist --------------------------------------------
+# --platform=$BUILDPLATFORM on every builder stage: under multi-arch buildx
+# each platform would otherwise run the whole toolchain (npm, go, UPX) under
+# QEMU emulation. The outputs here are static files, builder-native is fine.
+FROM --platform=$BUILDPLATFORM node:${NODE_VERSION}-alpine AS frontend
+WORKDIR /src/app
+COPY app/package.json app/package-lock.json ./
+RUN npm ci --no-audit --no-fund
+COPY app/ .
+# npm run build = tsc -b && vite build (same as the workflows).
+RUN npm run build
+
+# ---- Agent binaries for the embed (scripts/build-agents.sh) ----------------
+# Pure-Go cross compilation with GOARCH/GOARM/GOMIPS, so this stage is
+# platform-independent: the same five binaries are produced natively for
+# both the amd64 and arm64 image builds. UPX is a build dependency only.
+FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS agents
+ARG AGENT_VERSION=0.0.0-docker
+RUN apt-get update \
+ && apt-get install -y --no-install-recommends upx-ucl \
+ && rm -rf /var/lib/apt/lists/*
+WORKDIR /src
+COPY agent/ agent/
+COPY scripts/build-agents.sh scripts/
+# Relative output dir: build-agents.sh resolves it from the repo root.
+RUN scripts/build-agents.sh "$AGENT_VERSION" out/agents
+
+# ---- Server: frontend + agents embedded, release ldflags --------------------
+# Also builder-native: the server binary is cross-compiled with GOOS/GOARCH
+# from the buildx target (defaults keep plain `docker build` working).
+FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS server
+ARG AGENT_VERSION=0.0.0-docker
+ARG VERSION=0.0.0-docker
+ARG BUILD_COMMIT=unknown
+ARG TARGETOS=linux
+ARG TARGETARCH=amd64
+WORKDIR /src
+COPY agent/ agent/
+COPY server-go/ server-go/
+COPY --from=frontend /src/app/dist server-go/internal/staticspa/dist
+COPY --from=agents /src/out/agents/ server-go/internal/agentbin/agents/
+WORKDIR server-go
+# Same ldflags as the netpulse build in .goreleaser.yaml.
+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/gnacho/netpulse/server-go/internal/updater.BuildCommit=${BUILD_COMMIT} \
+        -X github.com/gnacho/netpulse/server-go/internal/agentbin.EmbeddedAgentVersion=${AGENT_VERSION} \
+        -X github.com/gnacho/netpulse/server-go/internal/httpapi.Version=${VERSION}" \
+      -o /out/netpulse ./cmd/netpulse
+
+# ---- Runtime ----------------------------------------------------------------
+FROM alpine:3
+ARG VERSION=0.0.0-docker
+ARG BUILD_COMMIT=unknown
+LABEL org.opencontainers.image.title="NetPulse" \
+      org.opencontainers.image.description="Self-hosted network monitoring: Go server with embedded web UI and agent binaries" \
+      org.opencontainers.image.source="https://github.com/gnacho/netpulse" \
+      org.opencontainers.image.revision="${BUILD_COMMIT}" \
+      org.opencontainers.image.version="${VERSION}" \
+      org.opencontainers.image.licenses="AGPL-3.0"
+# ca-certificates for outbound HTTPS (integrations, update checks), tzdata so
+# TZ works, busybox wget for the healthcheck. openssh-client is needed: the
+# gateway autodetect and per-router inventory call the ssh binary, and host
+# key verification calls ssh-keyscan (the polling pool itself is pure Go,
+# x/crypto). The server is self-contained otherwise; the updater cannot
+# apply inside a container (no deploy/update.sh, no root unit) by design.
+RUN apk add --no-cache ca-certificates tzdata openssh-client \
+ && addgroup -g 1000 netpulse \
+ && adduser -u 1000 -G netpulse -D -h /data -s /sbin/nologin netpulse \
+ && mkdir -p /data \
+ && chown netpulse:netpulse /data
+ENV PORT=3000 \
+    DATA_DIR=/data \
+    # Fresh installs bind 127.0.0.1 by default; a container must listen on
+    # all interfaces for published ports to work. Override back to 127.0.0.1
+    # (docker run -e NETPULSE_BIND=127.0.0.1) if you front it with a proxy in
+    # the same network namespace.
+    NETPULSE_BIND=0.0.0.0
+# Everything stateful (SQLite DB, session secret, generated SSH key, TLS
+# certs) lives in /data — keep it on a volume or the server regenerates
+# state on every recreate.
+VOLUME /data
+EXPOSE 3000
+COPY --from=server /out/netpulse /usr/local/bin/netpulse
+USER netpulse
+WORKDIR /data
+HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
+  CMD wget -qO- "http://127.0.0.1:${PORT}/api/health" >/dev/null || exit 1
+ENTRYPOINT ["/usr/local/bin/netpulse"]

+ 23 - 0
Makefile

@@ -40,3 +40,26 @@ collector:
 
 clean:
 	rm -rf app/dist server-go/internal/staticspa/dist
+
+# --- Docker (optional install path, docs/docker.md) -------------------------
+# Not part of `make check`: helpers to build and run the server image. The
+# image builds the frontend and the agent binaries itself, so a plain
+# `docker build` is self-contained and needs no toolchain on the host.
+DOCKER_IMAGE ?= netpulse:local
+VERSION ?= 0.0.0-docker
+BUILD_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
+
+.PHONY: docker-build docker-up docker-down
+
+docker-build:
+	docker build \
+	  --build-arg VERSION=$(VERSION) \
+	  --build-arg BUILD_COMMIT=$(BUILD_COMMIT) \
+	  --build-arg AGENT_VERSION=$(shell tr -d '[:space:]' < agent/VERSION 2>/dev/null || echo 0.0.0-dev) \
+	  -t $(DOCKER_IMAGE) .
+
+docker-up:
+	docker compose up -d --build
+
+docker-down:
+	docker compose down

+ 16 - 0
README.md

@@ -280,6 +280,20 @@ curl -fsSL --retry 3 -o /tmp/netpulse-install-collector.sh https://raw.githubuse
 
 Each probe is a TCP connection to the router that is closed without authenticating, so a router with dropbear logs it as `Child connection` followed by `Exit before auth: Exited normally`. That is the probe, not a failed login. `PROBE_PORT` (default `22`) points it at a listener that does not log every connection, for example the router's web UI on `80`, and `PROBE_INTERVAL` (default `5s`) sets how often it runs.
 
+**Docker.** If you prefer containers over systemd, a server image with the
+embedded UI and agent binaries is published on every release:
+
+```bash
+docker run -d --name netpulse -p 127.0.0.1:3000:3000 -e AUTH_PASS=choose-one \
+  -v netpulse-data:/data --restart unless-stopped ghcr.io/gnacho/netpulse:latest
+```
+
+`AUTH_PASS` is required at boot (10+ characters); the port above is
+published on loopback — publish on a LAN interface instead if you want the
+panel reachable from other machines. Updates are `docker pull` + recreate;
+leave the in-app updater alone in this mode. Full guide:
+[docs/docker.md](docs/docker.md).
+
 **On-box mode.** No spare box? The server itself runs on an OpenWrt router,
 with UCI config and self-signed TLS with SPKI pinning.
 
@@ -299,6 +313,8 @@ is in [docs/ROADMAP.md](docs/ROADMAP.md).
 - **[User manual](docs/manual.en.md)** (also in
   [Spanish](docs/manual.es.md)): every screen explained, menu by menu, with
   step-by-step procedures.
+- **[Docker](docs/docker.md)**: optional container install path for the
+  server, with compose and GHCR images.
 - **[Website](https://netpulse.cloudless.club)**: the project in five
   minutes, with a [feature tour](https://netpulse.cloudless.club/features),
   FAQ and screenshots.

+ 37 - 0
docker-compose.yml

@@ -0,0 +1,37 @@
+# NetPulse server in Docker. The image contains the server with the embedded
+# web UI and the embedded agent binaries; nothing agent-related runs in the
+# container. Configuration is the same environment as the binary install
+# (server-go/.env.example); an optional .env file next to this compose file
+# is passed into the container.
+#
+#   docker compose up -d --build   # build from this checkout
+#   NETPULSE_IMAGE=ghcr.io/gnacho/netpulse:latest docker compose up -d
+#                                  # use a prebuilt image
+#
+# Full guide: docs/docker.md
+
+services:
+  netpulse:
+    image: ${NETPULSE_IMAGE:-netpulse:local}
+    build:
+      context: .
+    ports:
+      # Loopback by default: the panel must not land on a public interface
+      # just because the compose file was used as-is. To reach it from the
+      # LAN set NETPULSE_PUBLISH_ADDR=0.0.0.0 (or a specific host IP).
+      - "${NETPULSE_PUBLISH_ADDR:-127.0.0.1}:${NETPULSE_PORT:-3000}:3000"
+    volumes:
+      - netpulse-data:/data
+    environment:
+      # The healthcheck and the UI live on this port inside the container;
+      # map a different host port with NETPULSE_PORT instead of changing it.
+      PORT: 3000
+      DATA_DIR: /data
+      TZ: ${TZ:-UTC}
+    env_file:
+      - path: .env
+        required: false
+    restart: unless-stopped
+
+volumes:
+  netpulse-data:

+ 127 - 0
docs/docker.md

@@ -0,0 +1,127 @@
+# Docker
+
+Docker is an **optional** install path for the server — the project's
+position is "Docker is optional, never a requirement"
+([external integrations](external-integrations.md)), and the
+systemd/binary install remains the primary documented one. This page is for
+people who run everything in containers.
+
+The image ships the Go server with the embedded web UI **and** the embedded
+agent binaries, built with the same flags and version stamps as the release
+tarballs. The agents are payload only: the server serves them so you can
+install agents from the UI — nothing agent-related runs inside the
+container.
+
+## What you get in the image
+
+- Server + web UI, non-root user (uid/gid 1000), `/data` as the state volume.
+- Agent binaries for all five architectures embedded and version-coherent
+  with the server: after upgrading the server image, existing agents see
+  `update available` and self-upgrade from the server.
+- Healthcheck on `/api/health` (busybox `wget`, no extra tools).
+- `linux/amd64` and `linux/arm64` images on GHCR.
+
+## Quickstart: prebuilt image
+
+```bash
+docker run -d --name netpulse \
+  -p 127.0.0.1:3000:3000 \
+  -e AUTH_PASS=choose-one \
+  -v netpulse-data:/data \
+  --restart unless-stopped \
+  ghcr.io/gnacho/netpulse:latest
+```
+
+Open `http://localhost:3000` on the docker host and log in with `AUTH_PASS`.
+`AUTH_PASS` is required at boot (at least 10 characters — the server refuses
+to start without it); to reach the panel from other machines on the LAN,
+publish on the host interface instead of loopback
+(`-p 3000:3000`, or `-p <lan-ip>:3000:3000`). On first boot the server
+generates its SSH keypair into `/data/.ssh`. One caveat: on the default
+bridge network the container does not see your LAN, so the gateway
+autodetect probes the Docker bridge address, not your routers — add the
+routers from the UI (Settings) or seed them with `ROUTERS_JSON`. On a
+Linux host, `--network host` (or `network_mode: host` in compose) gives
+the container full LAN access and the usual autodetect works.
+
+## Quickstart: compose, from this checkout
+
+```bash
+docker compose up -d --build      # builds the local image netpulse:local
+```
+
+The optional `.env` passthrough below uses the long `env_file` syntax with
+`required: false`, which needs Compose 2.24 or newer (`docker compose
+version` to check); on older versions the compose file is a parse error
+before it can help.
+
+Compose reads an optional `.env` file next to `docker-compose.yml` and passes
+it to the container — set `AUTH_PASS` there before the first start (the
+server will not boot without it). The port is published on the docker
+host's loopback by default. Useful variables for compose itself:
+
+| Variable | Default | Purpose |
+|---|---|---|
+| `NETPULSE_PORT` | `3000` | Host port mapped to the container's 3000 |
+| `NETPULSE_PUBLISH_ADDR` | `127.0.0.1` | Host interface the port is published on; `0.0.0.0` exposes it on all interfaces |
+| `NETPULSE_IMAGE` | `netpulse:local` | Image to run; set it to a GHCR tag to skip the build |
+| `TZ` | `UTC` | Container timezone |
+
+## Configuration
+
+The container uses the same environment as the binary install — see
+[`server-go/.env.example`](../server-go/.env.example). The essentials:
+
+- `AUTH_PASS` — required to protect the web session (hash is persisted in
+  the DB after the first login).
+- `DEMO_MODE=1` — sample dataset, handy for a first look.
+- `NETPULSE_PUBLIC_URL` — set it when agents must reach the server under a
+  different address than the one they discover (used by agent install and
+  auto-reinstall).
+- `NETPULSE_TLS_ENABLED=1` + `NETPULSE_TLS_PORT=3443` — enable the extra
+  HTTPS listener and map the port in compose (`-p 3443:3443`).
+
+Do not change `PORT` or `DATA_DIR` in the container: the healthcheck and the
+volume layout assume the defaults; map a different host port with
+`NETPULSE_PORT` instead. `NETPULSE_BIND` ships as `0.0.0.0` because a
+container must listen on all interfaces for published ports to reach the
+fresh-install loopback default; setting it back to `127.0.0.1` locks the
+listener to the container's loopback (useful only with an in-container
+proxy).
+
+## Data and persistence
+
+Everything stateful lives in the `netpulse-data` volume at `/data`: the
+SQLite database, the auto-generated session secret, the server SSH keypair
+and any TLS certificates. Keep the volume across recreations or the server
+starts from scratch. Bind mounts work too (`-v /srv/netpulse:/data`); make
+the host directory writable by uid 1000.
+
+## Updates
+
+Image deploys update by pulling a new image and recreating the container —
+the data volume survives:
+
+```bash
+docker pull ghcr.io/gnacho/netpulse:latest
+docker compose up -d             # with compose
+# or, plain docker:
+docker stop netpulse && docker rm netpulse && <your docker run line again>
+```
+
+The in-app updater is inert here by design: without the repo's
+`deploy/update.sh` or the root systemd unit it reports available versions
+but cannot apply them (`canApply: false`), so the fleet upgrade path is
+image tags. The embedded agents are unaffected: they keep self-upgrading
+from the running server, which stays version-coherent with whatever image
+you run.
+
+## Notes
+
+- The collector (TCP latency sidecar) is not part of this image or the
+  compose file; keep using its binary/systemd install for now.
+- `armv7` images are not published (the release binaries cover it); if you
+  need one, `docker build --platform linux/arm/v7 .` works from a checkout.
+- Building locally takes a few minutes the first time: the frontend, five
+  agent architectures and the server are all compiled inside the image
+  (UPX included), so no toolchain is needed on the host.