Docker Swarm with Portainer¶
This page deploys the Spongram server on a Docker Swarm cluster from docker/docker-stack.yml,
with Portainer or with the Docker CLI. It covers the two images to publish, the network and
directories to prepare, how updates roll, why the Graphiti service is updated by hand, and which
settings live in the image rather than in the stack. The architecture is drawn in
Portainer / Docker Swarm.
The stack file is written for the vendor's cluster
docker/docker-stack.yml references the vendor's registry and public hostname in its
image: lines and in several ${VAR:-default} fallbacks. Before the first deployment,
replace the two image: references with your registry, and set SPONGRAM_PUBLIC_URL and
SPONGRAM_MARKETPLACE_DIRS explicitly. The file also carries opt-in variables for the
vendor's own website analytics and storefront; they do nothing when empty and can be removed.
What the stack defines¶
| Service | Image | Notes |
|---|---|---|
spongram |
<your-registry>/spongram:${TAG:-latest} |
One replica, port 8091 published, start-first updates, healthcheck on /readyz, networks spongram and webfacing |
graphiti-mcp |
<your-registry>/spongram-graphiti:${GRAPHITI_TAG:-1.1.3-spongram} |
One replica, no published port, restart_policy: on-failure, default (stop-first) updates |
postgresspg |
postgres:16-alpine, pinned by digest |
Data in /data/spongram/postgres |
neo4jspg |
neo4j:5.26-community, pinned by digest |
Data in /data/spongram/neo4j; ports 7475 → 7474 and 7688 → 7687 published |
Two networks are declared: spongram, an overlay created by the stack for the four services,
and webfacing, an external overlay that only spongram joins.
The file contains no placement constraint and no reverse-proxy label. Both are left to you (see State directories and TLS and the reverse proxy).
With a stack named spongram, the Swarm services are spongram_spongram,
spongram_graphiti-mcp, spongram_postgresspg and spongram_neo4jspg. The commands below use
those names.
Prepare the cluster¶
External network¶
Create the overlay once, on any manager:
The stack declares it external: true and fails to deploy if it does not exist. Your reverse
proxy joins the same network to reach spongram by service name.
State directories¶
| Host path | Service | Container path |
|---|---|---|
/data/spongram/postgres |
postgresspg |
/var/lib/postgresql/data |
/data/spongram/neo4j |
neo4jspg |
/data |
/data/spongram/marketplace |
spongram |
/data/spongram/marketplace (read-only) |
/data/spongram/downloads |
spongram |
/data/downloads (static files served under /downloads; can be removed from the file if unused) |
These are bind mounts, so the data is on whichever node runs the task. Because the file has no placement constraint, do one of the following before the first deployment:
- put
/data/spongramon storage shared by every node that can run the services, or - add a
deploy.placement.constraintsentry that pinspostgresspg,neo4jspgand, if you use the marketplace mount,spongramto the node that holds the directories.
Create the directories on that storage beforehand.
Images in your own registry¶
Build both images from the repository root and push them. If the cluster mixes CPU architectures, publish a multi-architecture manifest: a node whose architecture is missing from the image rejects the task, and the service is down until the scheduler picks another node.
docker buildx build --platform linux/amd64,linux/arm64 \
-f spongram_proxy/Dockerfile \
-t <your-registry>/spongram:<tag> --push .
docker buildx build --platform linux/amd64,linux/arm64 \
-f docker/Dockerfile.graphiti \
-t <your-registry>/spongram-graphiti:1.1.3-spongram --push .
make push-image REGISTRY=<your-registry> TAG=<tag> and
make push-graphiti REGISTRY=<your-registry> do the same for the architecture of the build host.
The spongram-graphiti image is the upstream Graphiti MCP image
(zepai/knowledge-graph-mcp:1.1.3-standalone, pinned by digest in docker/Dockerfile.graphiti)
rebuilt with:
integrations/graphiti/config-spongram.yamlbaked in as/app/mcp/config/config.yaml, so the stack mounts no file from the repository;- build-time patches that fail the build if upstream moved the code they target;
GRAPHITI_TELEMETRY_ENABLED=false, checked by the build;- the GPU libraries (PyTorch, CUDA, Triton) removed, because every inference call goes to the Spongram proxy.
Deploy¶
With Portainer¶
- Stacks → Add stack, name
spongram, Web editor: pastedocker/docker-stack.ymlwith your image references. - Add the environment variables below.
- Deploy. Optionally enable the stack webhook for later updates.
With the CLI¶
# variables exported in the shell or loaded from a private env file
docker stack deploy -c docker/docker-stack.yml --with-registry-auth spongram
make stack-deploy, make stack-logs and make stack-rm wrap the equivalent commands;
stack-rm removes the services and leaves /data/spongram untouched.
Stack variables¶
| Variable | Required | Meaning |
|---|---|---|
POSTGRES_PASSWORD |
yes | Password of the spongram Postgres user |
NEO4J_PASSWORD |
yes | Password of the neo4j user |
SPONGRAM_ADMIN_TOKEN |
yes | Admin bearer token |
SPONGRAM_INTERNAL_AUTH_TOKEN |
yes | Shared secret between graphiti-mcp and the inference proxy (openssl rand -hex 32) |
SPONGRAM_INFERENCE_API_KEY |
yes, unless your provider needs no key | Bootstrap key of the inference endpoint |
SPONGRAM_PUBLIC_URL |
set it | Public HTTPS URL, written into client bundles |
SPONGRAM_ENCRYPTION_KEY |
recommended | Fernet key for the provider key at rest |
TAG, GRAPHITI_TAG |
no | Image tags (latest, 1.1.3-spongram) |
SPONGRAM_MARKETPLACE_DIRS |
set it or empty it | Organisation catalogues of the persona marketplace |
The stack file uses plain ${VAR} for the required values. Unlike the Compose file it does not
refuse an empty one: a missing SPONGRAM_INTERNAL_AUTH_TOKEN deploys, and every Graphiti
inference call is then refused with a 503 (see Troubleshooting).
Every other variable in the file has a fallback. Configuration lists them, and notes where the stack fallback differs from the code default.
Check¶
curl -fsS https://spongram.example.com/readyz
curl -sS https://spongram.example.com/healthz # "version" is the release now serving
docker service ps spongram_spongram --no-trunc
Migrations are applied by the spongram container at start
(see Upgrades and migrations).
TLS and the reverse proxy¶
spongram serves plain HTTP on 8091. The stack publishes that port through the Swarm routing
mesh and attaches the service to webfacing; it defines no router, certificate or label.
Terminate TLS in the proxy you already run (Traefik, nginx, Caddy, a load balancer) and forward
to spongram:8091 over webfacing, or to port 8091 of any node. For Traefik, add your own
deploy.labels to the spongram service.
Requirements on the proxy:
- do not buffer responses on
/mcpand/v1/graph/stream, and set a generous read timeout there: the explorer stream stays open, and an MCP tool call such as a deletion can take tens of seconds before it answers; - expose only
spongram./_internal/*is protected by the internal token, but nothing outside the cluster needs it.
The Neo4j ports 7475 and 7688 are published on every node by the routing mesh. Restrict them
at the firewall; they exist for administration and are never given to clients.
How updates roll¶
spongram: start-first, judged on /readyz¶
update_config:
order: start-first
failure_action: rollback
monitor: 120s
rollback_config:
order: start-first
healthcheck: # GET http://127.0.0.1:8091/readyz
interval: 10s
timeout: 8s
retries: 3
start_period: 120s
Swarm's default order is stop-first. With a single replica that stops the running container
before the new one has finished waiting for Postgres, applying migrations and connecting to
Neo4j. During that gap MCP clients get a transport error, mark the server disconnected and do
not come back until the user reconnects them. With start-first the new container must report
healthy before the old one stops.
Two containers therefore overlap during an update. This is safe because code-map ingestion is serialised by a Postgres advisory lock, not only by an in-process lock, and because each migration bounds its lock wait so that a schema change never queues the live container behind it.
The healthcheck must call /readyz, never /healthz. /healthz is a diagnostic: it answers
503 as soon as a background worker is unhealthy. Used as a container healthcheck it would make
Swarm kill a container that serves requests correctly because a summary job is late. /readyz
answers 200 when the three hard dependencies (Graphiti, Postgres, Neo4j) answer, and ignores the
workers.
If the new container fails its healthcheck during monitor, Swarm rolls back and the previous
version keeps serving. A webhook that answered 200 is not proof of a deployment: compare the
version field of /healthz with the release you built.
graphiti-mcp: updated by hand¶
graphiti-mcp has no update_config, so it updates stop-first, without rollback. Readiness
of spongram depends on it: with the healthcheck above, about 30 seconds without Graphiti mark
the healthy spongram container unhealthy and Swarm replaces it too. Pulling and starting a new
Graphiti image can take longer than that. A new Graphiti image must therefore not be rolled by
the stack webhook together with everything else.
Procedure:
- Check the order. A newer Graphiti image needs a
spongramrelease that knows it; deployspongramfirst (see Upgrades and migrations). - Build and push the Graphiti image under a new tag.
- Choose a moment when no memory is being written:
components.pending_episodes_worker.queue_depthin/healthzis0and thegraphiti-mcplog shows that the extractions already forwarded have finished (a depth of0only means "forwarded"). The lock that keeps one writer per brain lives in the memory of a single Graphiti process, and two processes exist for a moment in the next step. -
Update the service alone, starting the new task before stopping the old one:
docker service update --with-registry-auth --update-order start-first \ --image <your-registry>/spongram-graphiti:<new-tag> spongram_graphiti-mcp--with-registry-authforwards your registry credentials to the node that pulls the image. 5. Check/readyz, then write a test memory and confirm it becomes searchable. 6. SetGRAPHITI_TAGto the new tag in the stored stack variables (Portainer stack editor, or your env file).docker service update --imagedoes not change the stack definition; the next redeploy from that definition would put the old image back.
With the Cloud overlay, do not overlap two Graphiti tasks: the receipt spool must have a single
owner. Update the service in a maintenance window with its default stop-first order instead,
and expect spongram to be restarted by its healthcheck if Graphiti stays down for more than
about 30 seconds.
Changing an environment variable of graphiti-mcp restarts it the same way. For that reason the
stack file keeps variables read only by spongram (SPONGRAM_RUN_MIGRATIONS,
SPONGRAM_ADOPT_SCHEMA, SPONGRAM_THINKING_GRAPHITI_ENTITIES, the Langfuse keys) in the
environment: block of spongram, outside the x-common-env anchor shared by both services.
Keep that separation when you add a variable.
Stack environment and variables baked in the image¶
A setting can come from three places, in increasing priority:
- the code default;
- an
ENVline ofspongram_proxy/Dockerfile(the "server image default"); - the
environment:of the service, from the stack file and the stack variables.
A Portainer stack webhook redeploys from the stack definition and variables stored in Portainer. It pulls new images, but it does not read the YAML in your repository. Two consequences:
- A change to
docker/docker-stack.ymltakes effect only after you paste the file into the Portainer stack editor and update the stack, or rundocker stack deployyourself. That re-applies every service definition: a change inside the sharedx-common-envanchor restartsgraphiti-mcpas well. docker service update --env-add NAME=value spongram_spongramtakes effect immediately, and is undone by the next redeploy from the stored definition. Use it for an immediate change or rollback, then record the value in the stack variables.
This is why the features that every server deployment is expected to run are switched on by
ENV lines in the image: they survive any redeploy, whatever the stored definition says. The
list is in Feature flags.
Roll back¶
Database migrations are not reverted by an image rollback; read Upgrades and migrations before rolling back across a schema change.
Pinned database images¶
postgres and neo4j are referenced as image:tag@sha256:<digest>. A redeploy pulls images
again; with a floating tag such as neo4j:5.26-community that silently moves the database to
whatever patch release the tag points at that day. With a digest, the version changes only when
you change the line. To upgrade a database: take a backup
(Backup and restore), replace the digest, apply the stack definition.
The upstream Graphiti base image is pinned by digest in docker/Dockerfile.graphiti for the
same reason.
Persona marketplace sources¶
The Standard catalogue of the persona marketplace ships inside the image. An Organisation catalogue is a local directory, never a URL: the service downloads nothing at run time.
- Put the catalogue on the node under
/data/spongram/marketplace/<source-name>. The source name is the last path segment. A read-only git clone works: a directory whose name starts with.(such as.git) is never read as an article. - The stack mounts the parent directory read-only
(
/data/spongram/marketplace:/data/spongram/marketplace:ro). Declare the sources inSPONGRAM_MARKETPLACE_DIRS, separated by:. -
Check what the service loaded:
A declared directory that is absent is logged as a warning and skipped; the Standard catalogue
still loads. To publish new catalogue content, update the directory on the node and call
POST /admin/api/spongram/marketplace/reload. No restart is needed.
Connections across nodes¶
Overlay networks drop idle TCP connections after roughly fifteen minutes. The image sets
SPONGRAM_NEO4J_MAX_CONN_LIFETIME_SEC=600 so that Bolt connections are recycled by age before
that happens. If spongram and Postgres run on different nodes, enable TCP keepalives on
Postgres:
ALTER SYSTEM SET tcp_keepalives_idle = 60;
ALTER SYSTEM SET tcp_keepalives_interval = 10;
ALTER SYSTEM SET tcp_keepalives_count = 6;
SELECT pg_reload_conf();
The helper script¶
deploy-portainer.sh builds both images with buildx, pushes them, calls a Portainer webhook
and waits until /healthz reports the built version. Its defaults are the vendor's registry,
webhook, public URL and manager node. To use it, set all of these:
| Variable | Meaning |
|---|---|
REGISTRY |
Your registry |
PORTAINER_WEBHOOK |
Your stack webhook URL; empty pushes the images without redeploying |
VERIFY_URL |
Public URL whose /healthz is polled for the new version; empty skips the check |
SWARM_MANAGER |
SSH name of a manager, used to compare the Graphiti image in the stack definition with the one in service |
TAG, GRAPHITI_TAG, PLATFORM |
Image tags and target platforms |
The script encodes the rules of this page: it refuses to call the webhook after it rebuilt the
Graphiti image (exit code 4), refuses when the stack definition and the running Graphiti service
name different images (exit code 5), and fails when /healthz does not report the built version
within the timeout (exit code 3).
The Cloud overlay¶
docker/docker-stack.cloud.yml is merged with the base file to enable the Cloud control plane:
docker stack config -c docker/docker-stack.yml -c docker/docker-stack.cloud.yml > /private/stack.yml
The rendered file contains secrets; write it to a private location. The overlay sets the same
variables as its Compose counterpart (see Docker Compose) and mounts
/data/spongram/execution-receipts into graphiti-mcp. Provision that directory on persistent
storage reachable from every node that can run Graphiti; a single Graphiti instance must own it.