From 0bb119f87f0da06f1754dff8cc05135e1c619d0c Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 Oct 2026 11:38:36 +0800 Subject: [PATCH] =?UTF-8?q?docker:=20sign-ins=20and=20plugins=20survive=20?= =?UTF-8?q?a=20restart,=20and=20the=20image=20checks=20its=20own=20health?= =?UTF-8?q?=20(Jorben=20on=20Discord)=20=E2=80=94=20only=20/config=20was?= =?UTF-8?q?=20a=20volume,=20while=20HOME=20(/home/nonroot)=20held=20the=20?= =?UTF-8?q?agents'=20sign-ins=20(~/.codex,=20~/.claude=E2=80=A6)=20and=20t?= =?UTF-8?q?he=20cache=20held=20Bun:=20a=20new=20container=20lost=20them=20?= =?UTF-8?q?all,=20and=20distroless/static=20has=20no=20libc,=20so=20the=20?= =?UTF-8?q?Bun=20magpie=20downloads=20couldn't=20start=20(fork/exec=20?= =?UTF-8?q?=E2=80=A6=20no=20such=20file=20or=20directory).=20HOME=20and=20?= =?UTF-8?q?XDG=20cache/data/state=20now=20live=20under=20/config=20(made?= =?UTF-8?q?=20on=20start,=20so=20an=20old=20volume=20keeps=20working),=20t?= =?UTF-8?q?he=20base=20is=20distroless/cc=20(glibc),=20and=20magpie=20heal?= =?UTF-8?q?thcheck=20backs=20a=20HEALTHCHECK=20with=20no=20curl=20needed.?= =?UTF-8?q?=20Verified=20in=20Docker:=20an=20old=20volume=20carried=20over?= =?UTF-8?q?,=20a=20market=20plugin=20installed=20and=20stayed=20across=20a?= =?UTF-8?q?=20recreate=20without=20downloading=20Bun=20again,=20the=20cont?= =?UTF-8?q?ainer=20reported=20healthy.=20TestHealthcheck=20and=20TestMakeD?= =?UTF-8?q?irs=20fail=20without=20the=20change.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 21 ++++++++++++-- README.md | 19 ++++++++++-- healthcheck_cli.go | 48 +++++++++++++++++++++++++++++++ healthcheck_cli_test.go | 64 +++++++++++++++++++++++++++++++++++++++++ main.go | 5 ++++ 5 files changed, 151 insertions(+), 6 deletions(-) create mode 100644 healthcheck_cli.go create mode 100644 healthcheck_cli_test.go diff --git a/Dockerfile b/Dockerfile index 45960cc9e..d3b7b14f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,18 +10,33 @@ 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 + && mkdir -p /config/home /config/cache /config/data /config/state -FROM gcr.io/distroless/static-debian12:nonroot +# cc, not static: plugins run on Bun, which magpie downloads on first use +# and which needs glibc (static has no libc at all, and Bun's musl build +# would need musl). cc is static plus glibc, libgcc and libstdc++. +FROM gcr.io/distroless/cc-debian12:nonroot COPY --from=build /out/magpie /magpie COPY --from=build --chown=65532:65532 /config /config -ENV XDG_CONFIG_HOME=/config \ +# Everything magpie and the sign-ins it manages write goes into the volume: +# its own files (/config/magpie), sign-ins kept where an agent keeps them +# (~/.codex, ~/.claude…, under HOME), and the cache holding Bun and the +# model catalog. magpie makes the folders a volume from an older image +# lacks. +ENV HOME=/config/home \ + XDG_CONFIG_HOME=/config \ + XDG_CACHE_HOME=/config/cache \ + XDG_DATA_HOME=/config/data \ + XDG_STATE_HOME=/config/state \ MAGPIE_ADDR=0.0.0.0:3425 VOLUME /config +HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \ + CMD ["/magpie", "healthcheck"] + EXPOSE 3425 3430 ENTRYPOINT ["/magpie"] diff --git a/README.md b/README.md index 260b65fce..9c5971c99 100644 --- a/README.md +++ b/README.md @@ -467,7 +467,14 @@ 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`. +distroless (`cc`, for the glibc the plugins' Bun needs), run as nonroot, +with everything it keeps in a volume at `/config`: magpie's own files +(`/config/magpie`), the sign-ins kept where their agent keeps them (HOME is +`/config/home`, so `~/.codex`, `~/.claude`… are in it) and the cache with the +Bun plugins run on (`/config/cache`, downloaded once). A volume made by an +older image keeps working: magpie adds these folders to it on start, and only +sign-ins made with that older image, which lived outside the volume, have to +be made again. ```sh docker build -t magpie . @@ -497,8 +504,14 @@ which is your own machine, not the container, so that page won't load: copy its whole address from the address bar and paste it into the sign-in's *Callback URL* field. `docker exec -it magpie /magpie accounts add codex` does the same in a terminal: open the link it prints, then paste the address -the browser ended on. Keys and sign-ins live in the volume, so a restart -keeps them. +the browser ended on. Keys, sign-ins and plugins live in the volume, so a +restart, or a new container on the same volume, keeps them. + +The image has a `HEALTHCHECK`: `magpie healthcheck` exits 0 while the gateway +answers on `MAGPIE_ADDR`, under `serve` and `web` alike, so `docker ps` shows +the container as healthy (Compose: `depends_on: condition: service_healthy`) +with no curl in the image. Bind-mounting a folder at `/config` in place of a +named volume works too; it has to be writable by uid 65532. ### Developing diff --git a/healthcheck_cli.go b/healthcheck_cli.go new file mode 100644 index 000000000..70037010b --- /dev/null +++ b/healthcheck_cli.go @@ -0,0 +1,48 @@ +package main + +import ( + "bytes" + "errors" + "fmt" + "io" + "net/http" + "os" + "path/filepath" + "time" + + "github.com/yetone/magpie/internal/gateway" +) + +// healthcheck: magpie healthcheck — exits 0 when a magpie gateway answers +// at MAGPIE_ADDR (127.0.0.1:3425 unless set), for a container's +// HEALTHCHECK, which has no curl to ask it with. +func healthcheck() error { + c := &http.Client{Timeout: 3 * time.Second} + res, err := c.Get(gateway.URL() + "/") + if err != nil { + return fmt.Errorf("no gateway at %s: %w", gateway.URL(), err) + } + defer res.Body.Close() + b, _ := io.ReadAll(io.LimitReader(res.Body, 4096)) + if res.StatusCode != http.StatusOK || !bytes.Contains(b, []byte(`"magpie"`)) { + return fmt.Errorf("%s answers %s, not as a magpie gateway", gateway.URL(), res.Status) + } + return nil +} + +// makeDirs creates $HOME and the XDG folders the environment names when +// they don't exist yet. The Docker image points them all into its volume +// (/config/home, /config/cache…), and a volume made by an older image has +// none of them: whatever writes there first must not find its parent +// missing. +func makeDirs() { + for _, k := range []string{"HOME", "XDG_CONFIG_HOME", "XDG_CACHE_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME"} { + d := os.Getenv(k) + if d == "" || !filepath.IsAbs(d) { + continue + } + if _, err := os.Stat(d); errors.Is(err, os.ErrNotExist) { + _ = os.MkdirAll(d, 0o700) + } + } +} diff --git a/healthcheck_cli_test.go b/healthcheck_cli_test.go new file mode 100644 index 000000000..7fc39c36d --- /dev/null +++ b/healthcheck_cli_test.go @@ -0,0 +1,64 @@ +package main + +import ( + "net" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" +) + +// magpie healthcheck, a container's HEALTHCHECK, passes only while a +// magpie gateway answers at MAGPIE_ADDR. +func TestHealthcheck(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + magpie := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`{"name":"magpie","version":"dev"}`)) + })) + defer magpie.Close() + t.Setenv("MAGPIE_ADDR", strings.TrimPrefix(magpie.URL, "http://")) + if err := run([]string{"healthcheck"}); err != nil { + t.Fatal("magpie answering:", err) + } + + other := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(`{"name":"nginx"}`)) + })) + defer other.Close() + t.Setenv("MAGPIE_ADDR", strings.TrimPrefix(other.URL, "http://")) + if err := run([]string{"healthcheck"}); err == nil { + t.Error("something else answering passed") + } + + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + addr := ln.Addr().String() + ln.Close() + t.Setenv("MAGPIE_ADDR", addr) + if err := run([]string{"healthcheck"}); err == nil { + t.Error("nothing listening passed") + } +} + +// The Docker image sets HOME and the XDG folders inside its volume, which +// a volume an older image made doesn't have yet: magpie makes them. +func TestMakeDirs(t *testing.T) { + vol := t.TempDir() + for k, d := range map[string]string{"HOME": "home", "XDG_CONFIG_HOME": "config", "XDG_CACHE_HOME": "cache", "XDG_DATA_HOME": "data", "XDG_STATE_HOME": "state"} { + t.Setenv(k, filepath.Join(vol, d)) + } + t.Setenv("MAGPIE_ADDR", "127.0.0.1:1") + if err := run([]string{"version"}); err != nil { + t.Fatal(err) + } + for _, d := range []string{"home", "config", "cache", "data", "state"} { + if fi, err := os.Stat(filepath.Join(vol, d)); err != nil || !fi.IsDir() { + t.Errorf("%s not made: %v", d, err) + } + } +} diff --git a/main.go b/main.go index b2188b86d..e6f18bf0e 100644 --- a/main.go +++ b/main.go @@ -81,6 +81,7 @@ const usage = `magpie — one place to pick every agent's model magpie plugin move-back|unmigrate go back to the built-in, with its accounts magpie serve run the gateway alone (the app runs it too) + magpie healthcheck exit 0 when the gateway answers (a container's HEALTHCHECK) magpie mcp image the image and video generation MCP server an agent is given from the library (stdio) magpie usage [today|7d|30d|all] tokens and cost per agent and model (30d) magpie usage --csv [today|7d|30d|all] every request as CSV: the model asked for, sent and served, tokens, cost, time, status @@ -115,6 +116,10 @@ func main() { } func run(args []string) error { + if len(args) > 0 && args[0] == "healthcheck" { + return healthcheck() // every few seconds in a container: nothing else + } + makeDirs() settings.Migrate() agent.RenameLegacy() agent.MoveCursorEfforts()