From 1a704bd6ba3b6219733fb75dfe296636954e5679 Mon Sep 17 00:00:00 2001 From: Shelken <33972006+shelken@users.noreply.github.com> Date: Wed, 30 Sep 2026 03:21:19 +0800 Subject: [PATCH] docker: a server image and a multi-arch build workflow (#245) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docker: a server image and a multi-arch build workflow The image is the terminal-only binary built with -tags nogui on distroless static, run as nonroot with magpie's files on a /config volume, created and chowned at build time so a fresh named volume is writable by uid 65532. The workflow builds amd64 and arm64 on native runners, smoke-tests each image before anything is pushed (ready probe, unknown-model 4xx JSON, /config write), pushes by digest, merges a multi-arch manifest (semver trio plus latest on v* tags, edge otherwise) and attests build provenance. PR runs build and smoke-test only. The README documents running the image, the web UI mode, and why subscription sign-ins must start where a browser shares loopback with magpie. Co-Authored-By: glm-5.3-flash Generated-By: omp 18.4.3 * docker: README publishes the ports on the host's loopback and says how to require a key — until the gateway is shared from Settings it takes any key from anyone who reaches it, and Docker's -p 3425:3425 puts it on every interface of the host past its firewall. Checked in the built image: with settings lan+lanKey a request from the host through the published port without the key gets 401 and with it 200; without them any key gets 200. --------- Co-authored-by: glm-5.3-flash Co-authored-by: yetone --- .dockerignore | 9 +++ .github/workflows/docker.yml | 150 +++++++++++++++++++++++++++++++++++ Dockerfile | 28 +++++++ README.md | 28 +++++++ 4 files changed, 215 insertions(+) create mode 100644 .dockerignore create mode 100644 .github/workflows/docker.yml create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..fb56a7174 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +# internal/ 不可排除: web 资源与提示词通过 go:embed 整目录嵌入二进制 +.git +.github +dist +site +build +magpie.app +/*.md +.DS_Store diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 000000000..6a297faf8 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,150 @@ +name: Docker + +on: + push: + branches: [main] + tags: ['v*'] + pull_request: + paths: [Dockerfile, .dockerignore, .github/workflows/docker.yml] + workflow_dispatch: + +env: + IMAGE: ghcr.io/${{ github.repository_owner }}/magpie + +jobs: + build: + name: build (${{ matrix.platform }}) + strategy: + fail-fast: false + matrix: + platform: [linux/amd64, linux/arm64] + # native arm runner instead of QEMU + runs-on: ${{ startsWith(matrix.platform, 'linux/arm') && 'ubuntu-24.04-arm' || 'ubuntu-24.04' }} + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v5 + + - uses: docker/setup-buildx-action@v4 + + - uses: docker/login-action@v4 + if: github.event_name != 'pull_request' + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ github.token }} + + - name: Smoke test the image + run: | + docker build --load \ + --cache-from type=gha,scope=${{ matrix.platform }} \ + --cache-to type=gha,scope=${{ matrix.platform }},mode=max \ + -t magpie-smoke . + + docker run -d --name smoke -v smoke-ci:/config -p 13425:3425 magpie-smoke + + READY=0 + for _ in $(seq 1 30); do + if curl -s -o /dev/null http://127.0.0.1:13425/v1/models; then READY=1; break; fi + sleep 1 + done + test "$READY" = 1 + + CODE=$(curl -s -o /tmp/body.json -w '%{http_code}' \ + -H 'Authorization: Bearer magpie' -H 'Content-Type: application/json' \ + -d '{"model":"ci-unknown-model","messages":[{"role":"user","content":"hi"}]}' \ + http://127.0.0.1:13425/v1/chat/completions) + echo "chat/completions answered HTTP $CODE" + cat /tmp/body.json; echo + (( CODE >= 400 && CODE < 500 )) + jq -e '.error.type' /tmp/body.json > /dev/null + + OUT=$(docker run --rm -v smoke-ci:/config magpie-smoke provider add ci-smoke key=sk-ci url=https://example.com) + echo "$OUT" + echo "$OUT" | grep -q 'added ci-smoke' + + - name: Build and push by digest + id: build + uses: docker/build-push-action@v7 + with: + context: . + platforms: ${{ matrix.platform }} + provenance: false + push: ${{ github.event_name != 'pull_request' }} + build-args: | + VERSION=${{ startsWith(github.ref, 'refs/tags/v') && github.ref_name || github.sha }} + outputs: | + type=image,name=${{ env.IMAGE }},push-by-digest=true,name-canonical=true + cache-from: type=gha,scope=${{ matrix.platform }} + cache-to: type=gha,scope=${{ matrix.platform }},mode=max + + - name: Export digest + if: github.event_name != 'pull_request' + run: | + mkdir -p ${{ runner.temp }}/digests + digest="${{ steps.build.outputs.digest }}" + touch "${{ runner.temp }}/digests/${digest#sha256:}" + + - uses: actions/upload-artifact@v6 + if: github.event_name != 'pull_request' + with: + name: digests-${{ matrix.platform == 'linux/amd64' && 'amd64' || 'arm64' }} + path: ${{ runner.temp }}/digests/* + if-no-files-found: error + retention-days: 1 + + merge: + if: github.event_name != 'pull_request' + needs: [build] + runs-on: ubuntu-24.04 + permissions: + contents: read + packages: write + id-token: write + attestations: write + outputs: + digest: ${{ steps.manifest.outputs.digest }} + steps: + - uses: actions/download-artifact@v7 + with: + path: ${{ runner.temp }}/digests + pattern: digests-* + merge-multiple: true + + - uses: docker/setup-buildx-action@v4 + + - uses: docker/login-action@v4 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ github.token }} + + - name: Create manifest and tag + id: manifest + working-directory: ${{ runner.temp }}/digests + run: | + if [[ "$GITHUB_REF" == refs/tags/v* ]]; then + V="${GITHUB_REF_NAME#v}" + TAGS=(-t "$IMAGE:$V") + REF_TAG="$V" + if [[ "$V" != *-* ]]; then + TAGS+=(-t "$IMAGE:${V%.*}" -t "$IMAGE:${V%%.*}" -t "$IMAGE:latest") + fi + else + TAGS=(-t "$IMAGE:edge") + REF_TAG=edge + fi + DIGEST_ARGS=() + for d in *; do DIGEST_ARGS+=("$IMAGE@sha256:$d"); done + docker buildx imagetools create "${TAGS[@]}" "${DIGEST_ARGS[@]}" + docker buildx imagetools inspect "$IMAGE:$REF_TAG" + DIGEST=$(docker buildx imagetools inspect "$IMAGE:$REF_TAG" --format '{{json .}}' | jq -r '.manifest.digest') + echo "digest=$DIGEST" >> "$GITHUB_OUTPUT" + + - name: Attest build provenance + uses: actions/attest-build-provenance@v3 + with: + push-to-registry: true + subject-name: ${{ env.IMAGE }} + subject-digest: ${{ steps.manifest.outputs.digest }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..45960cc9e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +FROM golang:1.26-alpine AS build + +WORKDIR /src + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . + +ARG VERSION=dev +RUN CGO_ENABLED=0 go build -tags nogui -trimpath \ + -ldflags "-s -w -X main.version=${VERSION}" -o /out/magpie . \ + && mkdir /config + +FROM gcr.io/distroless/static-debian12:nonroot + +COPY --from=build /out/magpie /magpie +COPY --from=build --chown=65532:65532 /config /config + +ENV XDG_CONFIG_HOME=/config \ + MAGPIE_ADDR=0.0.0.0:3425 + +VOLUME /config + +EXPOSE 3425 3430 + +ENTRYPOINT ["/magpie"] +CMD ["serve"] diff --git a/README.md b/README.md index f3a6ffca1..6f18be642 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,34 @@ Linux needs `libgtk-3-dev` and `libwebkit2gtk-4.1-dev` for the app build (the Makefile adds the `gtk3` tag; with plain `go build`, pass `-tags gtk3`); Windows uses the WebView2 runtime that ships with the OS. +### Docker + +`docker build` makes a server image: the terminal-only binary on +distroless, run as nonroot, with magpie's files in a volume at `/config`. + +```sh +docker build -t magpie . +docker run -d --name magpie -p 127.0.0.1:3425:3425 -p 127.0.0.1:3430:3430 -v magpie-config:/config magpie +``` + +3425 is the gateway for agents. Until it is shared (below) it takes any key, +`Bearer magpie` included, from anyone who reaches it, so the ports above are +published on the host's loopback only; Docker's `-p 3425:3425` would put it +on every interface of the host, past its firewall. To reach it from other +machines, turn on Settings → Share on local network in the browser UI (or +put `"lan": true, "lanKey": "sk-magpie-…"` in `/config/magpie/settings.json`): +from then on a request from outside the container must carry that key as its +API key, and only then publish the port beyond 127.0.0.1. + +For the browser UI run the image with `magpie web --addr 0.0.0.0:3430 --no-open` +in place of the default `serve`, and open +`http://localhost:3430/?k=` (set `MAGPIE_WEB_KEY` +to keep one key across restarts). There you add providers and import +sign-ins from a file; a subscription sign-in started in the container cannot +finish, because the vendor sends the browser back to the container's own +loopback, so sign in on a machine where magpie runs with a browser. Keys and +sign-ins live in the volume, so a restart keeps them. + ### Developing ```sh