Deploying OpenClaw in a Container on a Server
Table of Contents
I run an OpenClaw node on a Raspberry Pi and the gateway on a server. The node does the work on the robot; the gateway is the long-running service that holds the model credentials and coordinates the nodes. This post is about the server side: a single container, a bind-mounted data directory, and an update path that can be undone.
The whole deployment is one Compose service. There is no database container and
no orchestration layer. The only state that matters is openclaw-data/, which
makes backups and rollbacks much easier than they would be otherwise.
OpenClaw node(s)
│ Tailscale (tailnet)
▼
tailscale serve
│
▼
127.0.0.1:18789 ──► openclaw container ──► OpenRouter
The gateway is published on loopback only. The nodes and the browser UI reach
it through Tailscale Serve, which terminates TLS on the tailnet and proxies to
127.0.0.1:18789. The gateway port itself is never published on a public
interface.
The files #
openclaw/
├── .env
├── .dockerignore
├── Dockerfile
├── docker-compose.yml
└── openclaw-data/ # created at runtime
The Compose file #
services:
openclaw:
build: .
image: openclaw:local
container_name: openclaw
restart: unless-stopped
environment:
OPENCLAW_GATEWAY_TOKEN: ${OPENCLAW_GATEWAY_TOKEN}
OPENROUTER_API_KEY: ${OPENROUTER_API_KEY}
NODE_COMPILE_CACHE: /var/tmp/openclaw-compile-cache
OPENCLAW_NO_RESPAWN: "1"
ports:
- "127.0.0.1:18789:18789"
volumes:
- ./openclaw-data:/home/node/.openclaw
A few decisions are doing most of the work here:
build: .plusimage: openclaw:localbuilds the image locally and gives it a stable name.docker compose buildreplaces that tag.restart: unless-stoppedstarts the gateway at boot and restarts it after a crash, but leaves it stopped if you stop it by hand.container_name: openclawkeeps the name stable fordocker compose exec, logs, and the rest of the commands below.OPENCLAW_GATEWAY_TOKENis the shared secret nodes use to authenticate to the gateway. Keep it stable across rebuilds.OPENROUTER_API_KEYis the model-provider credential. It is billed, so it stays in.envand never goes into the image.NODE_COMPILE_CACHEpoints Node’s compile cache at/var/tmp. It speeds up later starts but is only a cache; it does not need to survive a container replacement.OPENCLAW_NO_RESPAWN=1stops OpenClaw from trying to respawn itself inside the container. Docker’s restart policy is the supervisor.127.0.0.1:18789:18789publishes the port on host loopback only. This is the most important line in the file../openclaw-data:/home/node/.openclawkeeps the gateway’s state outside the container. Rebuilding the image does not touch it.
The Dockerfile #
FROM node:26-trixie-slim
# git/curl are needed by some skills and by npm for git-based deps
RUN apt-get update \
&& apt-get install -y --no-install-recommends git curl ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Install OpenClaw globally
RUN npm install -g openclaw@latest
# RUN mkdir -p /home/node/.openclaw && chown -R node:node /home/node/.openclaw
RUN mkdir -p \
/home/node/.openclaw \
/var/tmp/openclaw-compile-cache \
&& chown -R node:node \
/home/node/.openclaw \
/var/tmp/openclaw-compile-cache
# Run as the non-root "node" user that ships with the image
USER node
WORKDIR /home/node
ENV HOME=/home/node
EXPOSE 18789
# --bind lan so the port is reachable from outside the container
CMD ["openclaw", "gateway", "--bind", "lan", "--port", "18789"]
The image is deliberately thin. It uses the official Node image, installs the
few native tools that skills and npm git dependencies need, installs OpenClaw
globally, and then drops to the node user that ships with the base image.
Two details are easy to misread:
EXPOSE 18789documents the port inside the container. It does not publish anything. The Composeportsmapping is what makes it reachable from the host, and that mapping is loopback-only.--bind lanmakes the gateway listen on the container’s network interfaces, not just container loopback. That is necessary for the published port to reach it, and it is safe because the host side is bound to127.0.0.1.
npm install -g openclaw@latest means a rebuild can pick up a new release.
That is convenient for the update flow below, but it is not reproducible. If
you need deterministic rebuilds, pin a version (openclaw@x.y.z) and update
the Dockerfile deliberately.
Secrets and .env #
Compose reads a .env file from the project directory automatically. It needs
the two values referenced by the Compose file:
OPENCLAW_GATEWAY_TOKEN=replace-with-a-long-random-token
OPENROUTER_API_KEY=replace-with-your-openrouter-key
Generate the gateway token with something like:
openssl rand -hex 32
Then lock the file down:
chmod 600 .env
Neither value belongs in the Dockerfile, the Compose file, or Git. If the gateway token leaks, rotate it and update the nodes; if the OpenRouter key leaks, revoke it in the OpenRouter dashboard.
Add a .gitignore:
.env
openclaw-data/
openclaw-data.bak-*
Add a .dockerignore too. The Dockerfile does not COPY the project into the
image, but Docker still sends the build context to the daemon. Excluding the
data directory and the secrets keeps the context small and keeps credentials
out of the daemon’s temporary build context:
.env
openclaw-data/
openclaw-data.bak-*
.git/
First deploy #
cd ~/openclaw
# the container runs as UID 1000; make the bind mount writable by that user
mkdir -p openclaw-data
sudo chown -R 1000:1000 openclaw-data
docker compose build
docker compose up -d
docker compose ps
Follow the logs until the gateway reports that it is listening:
docker compose logs -f openclaw
Ctrl-C stops following the logs; it does not stop the container. In another
terminal, check the version and the listening socket:
docker compose exec openclaw openclaw --version
ss -ltnp | grep 18789
The socket should be 127.0.0.1:18789, not 0.0.0.0:18789. If it is bound to
0.0.0.0, the Compose ports entry or the command-line flags have been
changed.
The gateway writes into openclaw-data/. The container runs as the node user,
which is UID 1000 in the official Node image. If the directory is owned by root
and the gateway logs permission errors for /home/node/.openclaw, fix it:
sudo chown -R 1000:1000 openclaw-data
Do this while the container is stopped, or at least before the gateway tries to write again.
Reaching it over Tailscale #
The Compose port is loopback-only, so the tailnet cannot reach 18789 directly.
Tailscale Serve is the
bridge: it listens on the server’s tailnet identity, terminates TLS, and proxies
to http://127.0.0.1:18789.
On the server, with both the server and the nodes joined to the same tailnet:
sudo tailscale serve --bg 18789
tailscale serve status
The output gives a URL like https://server.<tailnet>.ts.net/. That is the
address the nodes and the browser use: HTTPS on port 443, with the same gateway
token as before. --bg keeps the proxy running in the background. Serve needs
HTTPS certificates for the tailnet, which are normally enabled by default; if
the command prints a login URL, follow it once.
To stop serving:
sudo tailscale serve --https=443 off
Do not use tailscale funnel for this. Funnel publishes the service to the
public internet; Serve keeps it inside the tailnet. Tailscale ACLs decide which
devices can reach the server, and the OpenClaw gateway token is still required.
Publishing on the server’s Tailscale IP instead (100.x.y.z:18789:18789) would
also work, but then TLS and a stable URL are your problem. Keeping the port on
loopback and letting Serve handle the tailnet side is the simpler split.
If you are on a machine that is not on the tailnet at all, an SSH tunnel still works as a temporary fallback:
ssh -N -L 18789:127.0.0.1:18789 matt@server
Then open http://localhost:18789 on your machine. -N keeps the tunnel open
without starting a remote shell.
Updating #
The update is a stop, a backup, a rebuild, and a restart:
cd ~/openclaw
# stop for a consistent backup, then back up with a timestamp
docker compose stop
TS=$(date +%F-%H%M)
cp -a openclaw-data openclaw-data.bak-$TS
echo "backup: openclaw-data.bak-$TS"
# keep the current image for rollback
docker tag openclaw:local openclaw:previous
# rebuild and restart
docker compose build --no-cache --pull
docker compose up -d
# verify (Ctrl-C to stop following logs)
docker compose exec openclaw openclaw --version
docker compose logs -f openclaw
A few notes on why the order matters:
docker compose stopprevents the gateway from writing while the data directory is copied. That is what makes the backup consistent.cp -apreserves permissions and ownership, which matters because the container runs as UID 1000.docker tag openclaw:local openclaw:previousgives you a one-step rollback. The previous image is overwritten on the next update, so it is not a history. If you want more rollback depth, tag the current image with a date as well:docker tag openclaw:local openclaw:$(date +%F).--no-cache --pullforces a fresh base image and a freshnpm install -g openclaw@latest. Without it, Docker may reuse the cached install layer and you will “update” to the same version.- If the build fails, the old
openclaw:localimage is still there. Bring it back withdocker compose up -d; the service is only stopped, not gone.
docker compose logs -f openclaw is the real verification. The version command
proves the CLI starts; the logs prove the gateway is listening and has not
fallen into a restart loop.
Rolling back #
cd ~/openclaw
docker compose down
# restore the data (only if the new version changed/broke it; otherwise skip these two lines)
mv openclaw-data openclaw-data.failed
cp -a openclaw-data.bak-<TIMESTAMP> openclaw-data
# restore the old image
docker tag openclaw:previous openclaw:local
docker compose up -d --no-build
docker compose down removes the container but leaves the bind-mounted data
alone. The openclaw-data.failed directory is deliberate: it preserves the
state the new version produced so you can inspect it after the rollback. Do not
delete it until the old version has been running long enough to trust.
Only restore the backup if the new version migrated or changed the data. If
the problem is purely the image, skip the mv/cp lines, retag the old image,
and start it again. When in doubt, restore: a working gateway with slightly old
state is better than a new gateway with a half-migrated one.
Updating a node #
The gateway container and the node are separate installations. Rebuilding the container does not update a node. Run this on the machine running the node, as the user that owns that installation:
# stop the node service so files aren't replaced under a running process
openclaw node stop
openclaw update
openclaw restart
openclaw node stop first is important: openclaw update replaces files, and
replacing them under a running process is how you get a half-updated node.
openclaw restart brings it back after the CLI has been updated.
I update the gateway first, then the nodes. They are separate upgrades, so follow whatever order the release notes ask for; the important part is to do them deliberately rather than letting one side drift.
Backup notes #
openclaw-data/ is the only state worth backing up. It contains the gateway’s
configuration and runtime data, and it may contain credentials. Treat the
backups as secrets:
- Keep them outside the Git repository.
- Encrypt them if they leave the machine.
- Test a restore before you need it.
- Prune old backups so a small disk does not fill up silently.
For a large data directory, a tarball is easier to move around than a directory copy:
TS=$(date +%F-%H%M)
tar -czf openclaw-data-$TS.tar.gz openclaw-data
The docker compose stop step is still worth doing before the tarball.
Troubleshooting #
| Symptom | Check |
|---|---|
| Container restarts in a loop | docker compose logs --tail=100 openclaw. Missing or empty env vars, a bad token, or a data-directory permission error are the usual causes. |
EACCES on /home/node/.openclaw | The bind mount is not writable by UID 1000. Stop the container and sudo chown -R 1000:1000 openclaw-data. |
bind: address already in use | Something else owns 18789 on the host. ss -ltnp | grep 18789, then change the host side of the mapping (for example 127.0.0.1:18790:18789). |
| Cannot reach the gateway from another machine | Expected. The port is loopback-only. Check tailscale serve status and use the Serve URL from a tailnet device; use an SSH tunnel from a machine outside the tailnet. |
docker compose build fails | It needs network access to pull the base image and to run npm install. Check git/curl/CA certificates in the image and the build output. The old image is still available. |
docker compose exec says the service is not running | docker compose ps first. exec only works on a running container; use run --rm for a one-off command. |
.env changes have no effect | Compose only reads .env from the project directory, and the file needs KEY=value with no spaces around =. Re-run docker compose up -d after changing it. |
| The rollback image starts but the gateway errors on data | The new version changed the data format. Repeat the rollback and restore the matching openclaw-data.bak-*. |
Wrapping up #
The deployment is small on purpose: one service, one bind mount, two secrets,
and a loopback-only port. The container is disposable; openclaw-data/ is the
part that matters. Once that division is clear, updates and rollbacks are just
“stop, copy, retag, rebuild” and “retag, restore, start”.
That is also why the backup is taken before the build, not after: the point is to have a known-good copy of the state from before the new image ever touched it.