OpenCode in Docker: the official image, serve and volumes
Run OpenCode 2 in Docker: the ghcr.io/anomalyco/opencode image and its tags, the TUI, opencode serve on 127.0.0.1, volumes for sign-ins and sessions, and keys.
OpenCode publishes an official image, ghcr.io/anomalyco/opencode, tagged by version: use a 2.x tag such as 2.4.2, because latest still points at OpenCode 1 (1.19.6) as of November 2026. The image’s entrypoint is opencode and it runs as root, so everything OpenCode keeps (config, sign-ins, sessions) lands under /root: mount a volume there. For the terminal interface run it with -it; for the server run serve --hostname 0.0.0.0 --port 4096 inside the container and publish it as -p 127.0.0.1:4096:4096, so only the host itself can reach it. Pass keys with -e NAME or an --env-file, never in the image.
The official image and its tags
The image is built in OpenCode’s own release pipeline and pushed to GitHub’s registry for linux/amd64 and linux/arm64. OpenCode’s install page says only that images “use versioned tags, for example ghcr.io/anomalyco/opencode:2.0.0.” The catch is in the registry’s tag list: version 1 and version 2 are published to the same image name, and latest follows version 1. On 2026-11-27 it sat on 1.19.6 while 2.4.2 (released 26 November) was the newest tag. The version 1 docs’ command, docker run -it --rm ghcr.io/anomalyco/opencode, has no tag, so it gives you OpenCode 1. Always name a 2.x tag:
docker pull ghcr.io/anomalyco/opencode:2.4.2
docker run --rm ghcr.io/anomalyco/opencode:2.4.2 --versionCopiedThe Dockerfile at v2.4.2 is short, and it decides most of what follows: Alpine, plus three packages (libgcc, libstdc++ and ripgrep), the OpenCode binary in /usr/local/bin, and ENTRYPOINT ["opencode"]. There is no USER, so it runs as root; no WORKDIR, so you set one; no EXPOSE or VOLUME, so nothing is published or kept unless you say so. Anything after the image name is passed to opencode as its arguments.
Where OpenCode 2 keeps its state, inside the container
OpenCode 2 follows the XDG directories under the home directory, with the app name opencode. In a container running as root, home is /root:
| What | Path in the container |
|---|---|
Sessions, saved API keys and OAuth tokens ( opencode.db), logs | /root/.local/share/opencode/ |
Your global opencode.json and cli.json | /root/.config/opencode/ |
Runtime state, such as the background service’s registration | /root/.local/state/opencode/ |
Cache | /root/.cache/opencode/ |
The providers page puts it plainly: “Saved API keys and OAuth tokens live in the server’s SQLite database,” which is opencode.db in the data folder. So the one directory you cannot lose is /root/.local/share/opencode; without a volume, every sign-in and session goes with the container. The simplest course is one named volume over all of /root, which covers the four folders at once:
docker volume create opencode-home
docker run --rm -v opencode-home:/root ghcr.io/anomalyco/opencode:2.4.2 debug pathsCopiedopencode debug paths prints the paths that build actually uses, so run it once against your volume before trusting the table. XDG_DATA_HOME, XDG_CONFIG_HOME and the other XDG variables move the folders if you set them, and OPENCODE_DB moves the database alone. Which config file wins over which is covered in the OpenCode config guide.
Run the terminal interface in a container
Mount the project, set the working directory to it, and keep the terminal attached with -it:
docker run -it --rm \
-v opencode-home:/root \
-v "$PWD":/workspace -w /workspace \
-e ANTHROPIC_API_KEY \
ghcr.io/anomalyco/opencode:2.4.2 --standaloneCopiedBy default OpenCode “discovers or starts one shared background server for your user account,” the design that lets several clients on one machine share sessions. In a container you start for one terminal there is nothing to share with, so the example uses --standalone, which in the CLI docs’ words runs “with a private server.” Sessions still land in the database on the volume, so the next container finds them.
Without -it, the interface has no terminal to draw on, and the API-key prompt needs one too: the providers page says “API-key entry and provider forms require an interactive terminal.” For a one-shot task with no interface at all, pass run instead: docker run --rm ... ghcr.io/anomalyco/opencode:2.4.2 run --standalone "Explain this repository". The flags that make an unattended run safe are in running OpenCode headless.
Pass provider keys without baking them in
Two routes keep a key out of the image and out of your shell history. The first is an environment variable. OpenCode reads a provider’s variable (ANTHROPIC_API_KEY, OPENROUTER_API_KEY and so on) on “the server process that runs model requests,” which here is inside the container. -e ANTHROPIC_API_KEY with no value copies it from your shell, so the key never appears in the command. Put it in your shell without echoing it:
read -rs ANTHROPIC_API_KEY && export ANTHROPIC_API_KEYCopiedFor a long-running container, an env file is easier. Keep it readable by you alone and outside any directory you mount into the container or commit:
install -m 600 /dev/null ~/.opencode-docker.env
# then edit it to hold lines such as:
# ANTHROPIC_API_KEY=sk-ant-...
# OPENCODE_PASSWORD=<a long random string>Copiedand pass --env-file ~/.opencode-docker.env. A key from the environment is not a saved account: opencode auth logout cannot remove it, and taking the line out of the file is how you disconnect.
The second route is to sign in once inside the container, which saves the key or token in opencode.db on your volume:
docker run -it --rm -v opencode-home:/root \
ghcr.io/anomalyco/opencode:2.4.2 auth login anthropic --method keyCopiedThat volume now holds a credential, so treat it like the key itself: anyone who can run Docker on the host can read it. For a subscription sign-in, OpenCode’s OAuth methods offer “a browser URL, device code, or authorization-code prompt.” A container has no browser of its own, so choose a device-code or paste-the-code method where the provider offers one. The Linux install guide covers signing in on a machine with no browser.
Run opencode serve in a container, and reach it safely
opencode serve “starts the API and web server” in the foreground. Its default address is 127.0.0.1, and inside a container that is the container’s own loopback, which Docker’s port publishing cannot reach. So the server listens on every interface of the container, and the -p flag decides who on the host side gets in:
docker run -d --name opencode --restart unless-stopped \
-p 127.0.0.1:4096:4096 \
-v opencode-home:/root \
-v "$HOME/projects":/workspace -w /workspace \
--env-file ~/.opencode-docker.env \
ghcr.io/anomalyco/opencode:2.4.2 serve --hostname 0.0.0.0 --port 4096Copied-p 127.0.0.1:4096:4096 binds the published port to the host’s loopback only. Leave the address out (-p 4096:4096) and, in Docker’s words, “Docker publishes the port on all interfaces (address 0.0.0.0) by default,” which puts the server on every network the host is on, the public internet included on most servers. A host firewall does not save you there: Docker’s firewall page says published traffic “gets diverted before it goes through the ufw firewall settings.” The password is then the only thing between the internet and an agent that runs commands in your mounted folders.
Pass --port 4096 explicitly. Outside service mode the server picks a free port starting at 4096, and the published mapping only works if you know which one it took.
The password comes from OPENCODE_PASSWORD, here from the env file. Without it the server makes one up on every start and prints it, so it would change at each restart:
docker logs opencode # "server listening on ..." and, with no OPENCODE_PASSWORD, "server password ..."
set -a; . ~/.opencode-docker.env; set +a
curl -u "opencode:$OPENCODE_PASSWORD" http://127.0.0.1:4096/api/infoCopiedThe username is always opencode. The same server answers the terminal interface: on the host, with the same password in the environment, opencode --server http://127.0.0.1:4096 attaches to it. Keep that client on the same version as the image tag; a mismatch connects with a warning that the server “has version …; this client is …. Continuing anyway.” The endpoints and the client libraries are in the OpenCode API guide.
To use it from another machine, do not widen the bind. Carry the loopback port over SSH instead:
ssh -N -L 4096:127.0.0.1:4096 you@your-server
# then, on the laptop:
opencode --server http://127.0.0.1:4096CopiedOpening it from a browser or a phone over a private network is in OpenCode on a remote server. The same setup as a Compose file:
services:
opencode:
image: ghcr.io/anomalyco/opencode:2.4.2
command: ["serve", "--hostname", "0.0.0.0", "--port", "4096"]
ports:
- "127.0.0.1:4096:4096"
env_file: ${HOME}/.opencode-docker.env
volumes:
- opencode-home:/root
- ${HOME}/projects:/workspace
working_dir: /workspace
restart: unless-stopped
volumes:
opencode-home:CopiedHermes Agent’s Compose setup makes the same loopback choice for its dashboard; the Hermes Agent Docker guide is the comparison if you run both.
File ownership on mounted folders
Because the image runs as root, every file OpenCode writes into your mounted project is owned by root on the host, and your own user then cannot edit or delete it without sudo. Run the container as your own user instead. OpenCode finds its folders from the home directory, so set HOME yourself rather than rely on whatever Docker picks for a user ID the image does not list, and back it with a folder you own; all four OpenCode folders then live inside it:
mkdir -p ~/.opencode-docker-home
docker run -it --rm \
--user "$(id -u):$(id -g)" \
-e HOME=/home/opencode \
-v ~/.opencode-docker-home:/home/opencode \
-v "$PWD":/workspace -w /workspace \
-e ANTHROPIC_API_KEY \
ghcr.io/anomalyco/opencode:2.4.2 --standaloneCopiedA folder you own, rather than a named volume, is what makes the home writable for your user. Files written while the container ran as root stay owned by root, so take them back with sudo chown -R "$(id -u):$(id -g)" on the folder, or start a fresh one and sign in again.
What the container cannot do
- Your host’s tools are not there. The image has no git, no Node, no Python, no bash: only what Alpine ships and the three packages above. The server “owns … tool execution,” so every command the agent runs, runs in the container, even when the terminal interface is on your host. An MCP server started with
npxfails for the same reason. The fix is your own image on top, below. - No git identity or SSH keys. Your
~/.gitconfigand~/.sshstay on the host unless you mount them, and mounting a private key hands it to the agent. A deploy key with write access to one repository limits what a mistake can reach. - It is not a sandbox for what you mount. The agent can change or delete anything in the mounted folders, as root by default. Docker keeps it away from the rest of the host; it does not protect the project. Keep work committed and pushed.
- Updates come from the tag. The binary sits in
/usr/local/bin, part of the image, so upgrade by changing the tag and recreating the container. The volume carries your state across. A volume first used withlatestholds version 1 data, and moving it to a 2.x tag is the version 1 to 2 upgrade, with the sessions that seem to go missing.
An image with the usual tools, built with docker build -t opencode-tools . and run in place of the official tag:
FROM ghcr.io/anomalyco/opencode:2.4.2
RUN apk add --no-cache git openssh-client bash nodejs npm python3CopiedTo remove everything OpenCode kept, delete the volume. This is optional and permanent: sessions, saved keys and config go with it.
# Optional, and permanent: there is no undo
docker rm -f opencode
docker volume rm opencode-homeCopiedFor running Claude Code the same way, with Anthropic’s dev container and its firewall, see Claude Code in Docker.