diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d22b246 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +.git +.github +site +tests +packaging +scripts +dist +vyso_sflow_data +Dockerfile +.dockerignore +# docs/ is mostly images, but docs/embed.go compiles the operator manual +# (docs/MANUAL.md) into the binary, so those two files stay in the context. +docs/* +!docs/embed.go +!docs/MANUAL.md diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..c4a2189 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,127 @@ +# Single-arch (linux/amd64) image → Docker Hub + GHCR. +# 需要:仓库 Variables 里设 DOCKERHUB_USER;Secrets 里设 DOCKERHUB_TOKEN(Docker Hub Access Token, Read & Write) +# PR 只构建不推送,用来验证 Dockerfile。 +name: docker + +on: + push: + tags: ["v*"] + pull_request: + paths: + - Dockerfile + - .dockerignore + - go.mod + - go.sum + - .github/workflows/docker.yml + workflow_dispatch: + +jobs: + image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write # 推 GHCR 用 + env: + PUSH: ${{ github.event_name != 'pull_request' }} + steps: + - uses: actions/checkout@v7 + + - uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 + + # The binary's version has no leading "v" (see cmd/vyos-sflow/startup.go + # and the release build in ci.yml), so strip it from the tag name. + - id: ver + run: | + if [ "${{ github.ref_type }}" = "tag" ]; then + echo "version=${GITHUB_REF_NAME#v}" >> "$GITHUB_OUTPUT" + else + echo "version=dev" >> "$GITHUB_OUTPUT" + fi + + - id: meta + uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0 + with: + images: | + docker.io/${{ vars.DOCKERHUB_USER || 'githubflyideas' }}/vyos-sflow + ghcr.io/${{ github.repository_owner }}/vyos-sflow + tags: | + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') }} + type=ref,event=pr + + # Smoke amd64 (same VERSION as the later push) before publishing, so a + # broken image is not pushed. The push build reuses the GHA cache. + - name: Build amd64 image for smoke + uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 + with: + context: . + platforms: linux/amd64 + load: true + push: false + tags: vyos-sflow:smoke + build-args: | + VERSION=${{ steps.ver.outputs.version }} + COMMIT=${{ github.sha }} + provenance: false + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: smoke test (amd64) + env: + VERSION: ${{ steps.ver.outputs.version }} + run: | + set -euo pipefail + cleanup() { docker rm -f vs >/dev/null 2>&1 || true; } + trap cleanup EXIT + docker run --rm vyos-sflow:smoke version | grep -F "vyos-sflow ${VERSION} " >/dev/null || { + echo "::error::version output does not carry ${VERSION}"; exit 1 + } + docker run -d --name vs -p 8066:8066 -p 6343:6343/udp \ + -v vs-data:/data \ + vyos-sflow:smoke + ok=0 + for i in $(seq 1 20); do + if curl -fsS http://127.0.0.1:8066/healthz; then + ok=1 + break + fi + sleep 1 + done + test "$ok" = 1 + health=$(curl -fsS http://127.0.0.1:8066/healthz) + echo "$health" + echo "$health" | grep -F '"status":"ok"' >/dev/null || { + echo "::error::healthz is not ok: ${health}"; exit 1 + } + echo "$health" | grep -F "\"version\":\"${VERSION}\"" >/dev/null || { + echo "::error::version is not ${VERSION}: ${health}"; exit 1 + } + docker logs vs + + - if: env.PUSH == 'true' + uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 + with: + username: ${{ vars.DOCKERHUB_USER }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - if: env.PUSH == 'true' + uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - uses: docker/build-push-action@10e90e3645eae34f1e60eeb005ba3a3d33f178e8 # v6.19.2 + with: + context: . + platforms: linux/amd64 + push: ${{ env.PUSH == 'true' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + VERSION=${{ steps.ver.outputs.version }} + COMMIT=${{ github.sha }} + provenance: false # 避免部分平台(如 RouterOS)解析 manifest 时遇到 attestation 条目 + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9830bda --- /dev/null +++ b/Dockerfile @@ -0,0 +1,65 @@ +# syntax=docker/dockerfile:1 +# vyos-sflow image: linux/amd64 only (other architectures are out of scope). +# +# The binary links DuckDB through cgo, so the build mirrors the release +# build in .github/workflows/ci.yml: external link mode, fully static, +# with the libstdc++ archive made findable and getentropy wrapped. +# The result runs in an empty runtime layer; no libc is needed. + +ARG GO_VERSION=1.27.4 + +FROM golang:${GO_VERSION} AS build +RUN apt-get update \ + && apt-get install -y --no-install-recommends g++ \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /src +COPY go.mod go.sum ./ +RUN go mod download +COPY . . + +ARG VERSION=dev +ARG COMMIT=unknown +ENV CGO_ENABLED=1 GOFLAGS=-buildvcs=false +RUN static_dir="$(mktemp -d)" \ + && ln -s "$(g++ -print-file-name=libstdc++.a)" "$static_dir/libstdc++.a" \ + && CGO_LDFLAGS="-L$static_dir" go build -trimpath -tags netgo,osusergo \ + -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${COMMIT} -linkmode=external -extldflags '-static -Wl,--wrap=getentropy'" \ + -o /out/vyos-sflow ./cmd/vyos-sflow \ + && ldd /out/vyos-sflow 2>&1 | grep -Eq 'not a dynamic executable|statically linked' + +# busybox provides sh and wget (health check) next to the static binary. +FROM busybox:1.37-musl + +LABEL org.opencontainers.image.title="vyos-sflow" \ + org.opencontainers.image.description="Local sFlow traffic analysis for VyOS and Linux: one binary, embedded DuckDB, TUI and Web UI" \ + org.opencontainers.image.source="https://github.com/githubflyideas/vyos-sflow" \ + org.opencontainers.image.licenses="Apache-2.0" + +COPY --from=build /out/vyos-sflow /usr/local/bin/vyos-sflow + +# Accounts and history (embedded DuckDB + Parquet) live in the data +# directory; keep it on a volume. A named volume inherits this ownership; +# with a bind mount, chown the host directory to 65532 or pass +# --user $(id -u):$(id -g). +RUN mkdir -p /data && chown 65532:65532 /data +WORKDIR /data + +# Unlike a direct install, the Web UI binds all container interfaces so +# published ports (or host networking) can reach it; the sFlow listener +# already defaults to all interfaces. Both can be overridden with +# -e VYOS_SFLOW_ADDR=... / -e VYOS_SFLOW_LISTEN=... or with flags after +# the image name. +ENV TZ=UTC \ + VYOS_SFLOW_ADDR=0.0.0.0:8066 \ + VYOS_SFLOW_DATA=/data +VOLUME /data +EXPOSE 8066/tcp 6343/udp + +USER 65532:65532 + +HEALTHCHECK --interval=30s --timeout=3s --start-period=10s \ + CMD wget -q -O /dev/null http://127.0.0.1:8066/healthz || exit 1 + +ENTRYPOINT ["/usr/local/bin/vyos-sflow"] +CMD ["serve"] diff --git a/README.md b/README.md index 6fd2fed..a809c4c 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Local sFlow traffic analysis for VyOS and Linux. View bandwidth, top talkers and recent flow records in your browser or over SSH. -[Quick start](#get-started-in-3-steps) · [Terminal view](#temporary-terminal-view-tui) · [Screenshots](#screenshots) · [User manual](docs/MANUAL.md) · [Service installation](docs/VYOS.md) +[Quick start](#get-started-in-3-steps) · [Terminal view](#temporary-terminal-view-tui) · [Docker](#run-with-docker) · [Screenshots](#screenshots) · [User manual](docs/MANUAL.md) · [Service installation](docs/VYOS.md) [Buy Me a Coffee](https://www.buymeacoffee.com/route66traffic66) — if vyos-sflow is useful to you, you can support the project here. @@ -77,6 +77,31 @@ Standalone TUI history lasts only for that session. Its temporary data is remove Press `?` for keyboard help. The terminal view runs on the router itself, over SSH, and attaches to the service there with the service's own token; `-data DIR` names the service's data directory when it is not the default. From another machine, use the web page. Explicit connection options never start a replacement collector if the connection fails. +## Run with Docker + +The image is published to GHCR and Docker Hub as `githubflyideas/vyos-sflow` (Linux amd64). Host networking is the recommended way to run it: the collector then receives datagrams directly from each exporter, and the Web UI is reachable on the host's interfaces. + +```sh +docker run -d --name vyos-sflow --restart unless-stopped --network host \ + -v vyos-sflow-data:/data \ + ghcr.io/githubflyideas/vyos-sflow:latest +``` + +Open `http://ROUTER_IP:8066` and sign in with **`admin` / `tryvyossflow66`**, then change the password right away: with host networking the Web UI listens on the host's interfaces, not only on loopback. Point any sFlow exporter at UDP `6343` of the host. + +Accounts and history live in the `/data` volume, stored with embedded DuckDB and Parquet files — there is no separate database server to install or run. The container can be removed and recreated freely; keep the volume. + +Without host networking, publish the two ports instead: + +```sh +docker run -d --name vyos-sflow --restart unless-stopped \ + -p 8066:8066 -p 6343:6343/udp \ + -v vyos-sflow-data:/data \ + ghcr.io/githubflyideas/vyos-sflow:latest +``` + +Note that with published ports the collector sees datagrams through Docker's NAT rather than directly from each exporter, which can blur the per-source view; host networking avoids that. + ## Reading the numbers | View | What the numbers mean |