Dockerfile 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # syntax=docker/dockerfile:1
  2. # NetPulse server image: the web UI (app/) and the agent binaries are
  3. # embedded into the Go binary the same way the release builds do it
  4. # (.goreleaser.yaml + scripts/build-agents.sh). The agents are only payload:
  5. # the server serves them for install, nothing agent-related runs here.
  6. #
  7. # Build args mirror the release ldflags; defaults produce a coherent
  8. # dev-stamped image (version 0.0.0-docker, agents stamped 0.0.0-docker):
  9. #
  10. # docker build -t netpulse .
  11. # docker build \
  12. # --build-arg VERSION=2.51.0 \
  13. # --build-arg BUILD_COMMIT=abc1234 \
  14. # --build-arg AGENT_VERSION=3.0.45 \
  15. # -t netpulse:2.51.0 .
  16. #
  17. # .github/workflows/docker.yml derives NODE_VERSION and GO_VERSION from the
  18. # repository (agent/VERSION, server-go/go.mod) and passes them as build args,
  19. # so these defaults only matter for a manual `docker build`.
  20. ARG NODE_VERSION=24
  21. ARG GO_VERSION=1.27
  22. # ---- Frontend: app/ -> app/dist --------------------------------------------
  23. # --platform=$BUILDPLATFORM on every builder stage: under multi-arch buildx
  24. # each platform would otherwise run the whole toolchain (npm, go, UPX) under
  25. # QEMU emulation. The outputs here are static files, builder-native is fine.
  26. FROM --platform=$BUILDPLATFORM node:${NODE_VERSION}-alpine AS frontend
  27. WORKDIR /src/app
  28. COPY app/package.json app/package-lock.json ./
  29. RUN npm ci --no-audit --no-fund
  30. COPY app/ .
  31. # npm run build = tsc -b && vite build (same as the workflows).
  32. RUN npm run build
  33. # ---- Agent binaries for the embed (scripts/build-agents.sh) ----------------
  34. # Pure-Go cross compilation with GOARCH/GOARM/GOMIPS, so this stage is
  35. # platform-independent: the same five binaries are produced natively for
  36. # both the amd64 and arm64 image builds. UPX is a build dependency only.
  37. FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS agents
  38. ARG AGENT_VERSION=0.0.0-docker
  39. RUN apt-get update \
  40. && apt-get install -y --no-install-recommends upx-ucl \
  41. && rm -rf /var/lib/apt/lists/*
  42. WORKDIR /src
  43. COPY agent/ agent/
  44. COPY scripts/build-agents.sh scripts/
  45. # Relative output dir: build-agents.sh resolves it from the repo root.
  46. RUN scripts/build-agents.sh "$AGENT_VERSION" out/agents
  47. # ---- Server: frontend + agents embedded, release ldflags --------------------
  48. # Also builder-native: the server binary is cross-compiled with GOOS/GOARCH
  49. # from the buildx target (defaults keep plain `docker build` working).
  50. FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS server
  51. ARG AGENT_VERSION=0.0.0-docker
  52. ARG VERSION=0.0.0-docker
  53. ARG BUILD_COMMIT=unknown
  54. ARG TARGETOS=linux
  55. ARG TARGETARCH=amd64
  56. WORKDIR /src
  57. COPY agent/ agent/
  58. COPY server-go/ server-go/
  59. COPY --from=frontend /src/app/dist server-go/internal/staticspa/dist
  60. COPY --from=agents /src/out/agents/ server-go/internal/agentbin/agents/
  61. WORKDIR server-go
  62. # Same ldflags as the netpulse build in .goreleaser.yaml.
  63. RUN --mount=type=cache,target=/go/pkg/mod \
  64. --mount=type=cache,target=/root/.cache/go-build \
  65. CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -trimpath \
  66. -ldflags "-s -w \
  67. -X github.com/gnacho/netpulse/server-go/internal/updater.BuildCommit=${BUILD_COMMIT} \
  68. -X github.com/gnacho/netpulse/server-go/internal/agentbin.EmbeddedAgentVersion=${AGENT_VERSION} \
  69. -X github.com/gnacho/netpulse/server-go/internal/httpapi.Version=${VERSION}" \
  70. -o /out/netpulse ./cmd/netpulse
  71. # ---- Runtime ----------------------------------------------------------------
  72. FROM alpine:3
  73. ARG VERSION=0.0.0-docker
  74. ARG BUILD_COMMIT=unknown
  75. LABEL org.opencontainers.image.title="NetPulse" \
  76. org.opencontainers.image.description="Self-hosted network monitoring: Go server with embedded web UI and agent binaries" \
  77. org.opencontainers.image.source="https://github.com/gnacho/netpulse" \
  78. org.opencontainers.image.revision="${BUILD_COMMIT}" \
  79. org.opencontainers.image.version="${VERSION}" \
  80. org.opencontainers.image.licenses="AGPL-3.0"
  81. # ca-certificates for outbound HTTPS (integrations, update checks), tzdata so
  82. # TZ works, busybox wget for the healthcheck. openssh-client is needed: the
  83. # gateway autodetect and per-router inventory call the ssh binary, and host
  84. # key verification calls ssh-keyscan (the polling pool itself is pure Go,
  85. # x/crypto). The server is self-contained otherwise; the updater cannot
  86. # apply inside a container (no deploy/update.sh, no root unit) by design.
  87. RUN apk add --no-cache ca-certificates tzdata openssh-client \
  88. && addgroup -g 1000 netpulse \
  89. && adduser -u 1000 -G netpulse -D -h /data -s /sbin/nologin netpulse \
  90. && mkdir -p /data \
  91. && chown netpulse:netpulse /data
  92. ENV PORT=3000 \
  93. DATA_DIR=/data \
  94. # Fresh installs bind 127.0.0.1 by default; a container must listen on
  95. # all interfaces for published ports to work. Override back to 127.0.0.1
  96. # (docker run -e NETPULSE_BIND=127.0.0.1) if you front it with a proxy in
  97. # the same network namespace.
  98. NETPULSE_BIND=0.0.0.0
  99. # Everything stateful (SQLite DB, session secret, generated SSH key, TLS
  100. # certs) lives in /data — keep it on a volume or the server regenerates
  101. # state on every recreate.
  102. VOLUME /data
  103. EXPOSE 3000
  104. COPY --from=server /out/netpulse /usr/local/bin/netpulse
  105. USER netpulse
  106. WORKDIR /data
  107. HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
  108. CMD wget -qO- "http://127.0.0.1:${PORT}/api/health" >/dev/null || exit 1
  109. ENTRYPOINT ["/usr/local/bin/netpulse"]