Dockerfile 5.2 KB

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