docker: sign-ins and plugins survive a restart, and the image checks its own health (Jorben on Discord) — only /config was a volume, while HOME (/home/nonroot) held the agents' sign-ins (~/.codex, ~/.claude…) and the cache held Bun: a new container lost them all, and distroless/static has no libc, so the Bun magpie downloads couldn't start (fork/exec … no such file or directory). HOME and XDG cache/data/state now live under /config (made on start, so an old volume keeps working), the base is distroless/cc (glibc), and magpie healthcheck backs a HEALTHCHECK with no curl needed. Verified in Docker: an old volume carried over, a market plugin installed and stayed across a recreate without downloading Bun again, the container reported healthy. TestHealthcheck and TestMakeDirs fail without the change.

This commit is contained in:
yetone
2026-10-01 11:38:37 +08:00
parent b4c6772677
commit 0bb119f87f
5 changed files with 151 additions and 6 deletions
+18 -3
View File
@@ -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"]
+16 -3
View File
@@ -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
+48
View File
@@ -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)
}
}
}
+64
View File
@@ -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)
}
}
}
+5
View File
@@ -81,6 +81,7 @@ const usage = `magpie — one place to pick every agent's model
magpie plugin move-back|unmigrate <subscription> 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()