To run Codex Docker workloads, install the Codex CLI in a non-root container, mount your project, and keep login state in a separate volume. Use a full VM when the agent needs its own Docker daemon; Agent37 offers that option alongside a standard sandbox with Codex preinstalled. The key decision is which files, credentials, and network access your agent actually needs.
A disposable container can make your setup repeatable. It does not make everything mounted inside it disposable or protected. This guide gives you a Dockerfile, Compose configuration, authentication steps, and hosting costs with that distinction intact. Sources and CLI options checked October 26, 2026.
Choose your Codex Docker route
Three products appear in searches for this phrase. Pick the one that matches the job before copying commands.
Route
What you get
Best fit
Codex CLI in your own Docker image
Your chosen tools, project mounts, and container lifecycle
A server or development workflow you operate
Docker Sandboxes
Docker's agent environment, launched through sbx
A packaged sandbox workflow with managed credential handling
OpenAI's codex-universal
A reference image for reproducing cloud environment setup
Debugging cloud dependencies and language runtimes
Docker documents sbx run codex ~/my-project as its quick start. Its Codex integration authenticates on the host and keeps those provider credentials outside the sandbox. That differs from the plain Docker example below, which stores Codex credentials in a mounted volume the agent can access.
OpenAI describes codex-universal as an approximation of its cloud environment for development and debugging. Pulling that image is a different workflow from installing the CLI with its own login and entrypoint.
If your goal is simply a hosted Codex worker, the Agent37 Codex template already includes the CLI. Keep reading if you need to control the Docker image or run containers inside the worker.
Build a small image with a pinned CLI
Use Docker Engine with Compose on a host you control, or a compatible desktop installation. Budget memory for your repository's builds and tests as well as the agent. Four gigabytes is a starting allocation for a small project, not a guarantee that a large build will fit.
Save this as Dockerfile in your project:
FROM node:24-bookworm-slim
ARG CODEX_VERSION=0.164.0
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
git ca-certificates curl ripgrep \
&& rm -rf /var/lib/apt/lists/* \
&& npm install -g @openai/codex@${CODEX_VERSION}
USER node
RUN mkdir -p /home/node/.codex /home/node/workspace
WORKDIR /home/node/workspace
ENTRYPOINT ["codex"]
OpenAI supports npm installation. The version above was available from npm and its binary and help commands were checked for this revision. The full Docker build was not rerun here. Run the acceptance checks below on your intended host before using it unattended.
The image runs as the base image's node user instead of root. Its UID is 1000; on Linux, match that to the owner of the project directory or adjust the image's user mapping. Do not solve a permissions error by making the whole repository world-writable.
The CLI release is pinned, but the Node tag and apt packages can change. For a reproducible production build, resolve the base image to a digest, control package versions, and record the resulting image digest. Keep secrets out of the image and build context with an appropriate .dockerignore.
The project bind mount makes edits visible on your host. The named volume keeps Codex configuration and login state across disposable containers. Docker's volume documentation distinguishes that storage lifecycle from the container lifecycle.
This setup mounts only the project and a dedicated Codex home. Avoid mounting your whole home directory, SSH agent, cloud credentials, or Docker socket. A writable bind mount lets container processes change the corresponding host files.
Start with these checks:
docker compose config --quiet
docker compose build
docker compose run --rm codex --version
docker compose run --rm --entrypoint sh codex -c \
'id; test -w /home/node/workspace && test -w /home/node/.codex'
Expect the pinned CLI version and a non-root identity. After the permissions check succeeds, create a harmless file in the workspace and confirm it appears on the host. Remove the test file afterward. This checks the mount you will actually use, instead of assuming another machine's permissions match yours.
Authenticate without baking credentials into the image
For an interactive account, use device authentication:
docker compose run --rm codex login --device-auth
Follow the URL and code from a separate browser. OpenAI's authentication guide says device login must be enabled in your personal security settings or by the workspace administrator. Access also depends on your account and workspace permissions.
For API-key authentication, export OPENAI_API_KEY through your secret manager or shell, then pass the variable by name:
docker compose run --rm -e OPENAI_API_KEY \
--entrypoint sh codex -c \
'printenv OPENAI_API_KEY | codex login --with-api-key'
The key is read from standard input. Do not paste its value into the Compose file or Dockerfile. With file-based credential storage, authentication persists in the dedicated volume. Treat that volume as sensitive; processes with access to it may read the credentials.
OpenAI also documents copying an existing auth.json into a headless environment. Use that fallback only with your own trusted destination. A separate container login is usually easier to reason about than sharing your host's entire configuration directory.
Check authentication with docker compose run --rm codex login status, then start the interactive client with docker compose run --rm codex.
Decide which layer enforces isolation
Codex's Linux command sandbox uses bubblewrap and seccomp. In a container, it may fail when the surrounding configuration blocks the operations it needs. That is a compatibility question, not proof that every Docker installation behaves identically.
OpenAI's container guidance allows the outer environment to provide isolation when the inner sandbox cannot run. For a dedicated environment containing only a trusted repository and the credentials it needs, that can mean:
docker compose run --rm codex \
--dangerously-bypass-approvals-and-sandbox
This disables Codex's inner sandbox and approval prompts. It does not restrict the container's network, protect mounted files from edits, or hide credentials available inside it. A malicious dependency can still act with the access you supplied. Keep the inner sandbox when your environment supports it and it fits the workflow.
For untrusted repositories, separate the execution environment from valuable credentials and data. Apply an outbound network policy at the appropriate infrastructure layer. Do not mount the host Docker socket just to make a test pass: daemon access can expose much more of the host than the project directory.
If the job needs a Docker daemon, put that daemon inside a dedicated VM and keep its privileges scoped there. Our Codex sandbox guide explains how the CLI's command restrictions differ from an outer execution environment.
Run one task, capture its result, then review it
For automation, use codex exec. Here is a task for the dedicated outer sandbox described above:
docker compose run --rm -T codex exec \
--dangerously-bypass-approvals-and-sandbox \
--json -o /home/node/workspace/codex-result.txt \
"Run the existing test suite. Report failures without editing files."
The prompt requests reporting only; the bypass flag does not enforce that restriction. Use an appropriate read-only policy and mounts if writes must be impossible.
-T disables Compose's pseudo-terminal for automation. --json produces structured events, and -o saves the final response in the mounted workspace. Check the process exit status, test output, and Git diff before treating the task as successful. Add an explicit model available to your account when you need stable model selection and budgeting.
For one-shot API automation, OpenAI documents CODEX_API_KEY for codex exec; pass it only to the command that needs it. Its automation guidance recommends the official Codex GitHub Action for GitHub Actions and warns against exposing API keys to every step that executes repository code.
The CLI invocation above ends when its task ends. A container restart policy alone is not a task queue. Scheduled work needs a scheduler, a clear task payload, and a record of completion.
Fix the common container failures
Symptom
First check
Login disappears on the next run
Confirm the same Compose project and named volume are being reused
Permission denied while editing
Compare the container UID with the bind-mounted directory's ownership
Bubblewrap or namespace error
Check host and container sandbox restrictions; decide the outer boundary deliberately
Git repository check fails
Run inside the intended repository; use --skip-git-repo-check only for an intentional non-repository task
Docker command unavailable in the worker
Use an environment with a daemon, such as a full VM; installing a client alone is insufficient
A build runs out of memory
Measure the build's requirements and increase its allocation and host capacity
A fresh container can reset its writable layer while keeping both mounts. Conversely, deleting the credential volume removes the login state you intended to preserve. Avoid volume-pruning commands until you know which projects depend on them.
Price the hosted runtime your workload needs
Agent37 offers two relevant paths. Use the standard agent37-codex template when you want the CLI and its hosted chat API. It has no bundled browser or desktop, and model usage goes through your own OpenAI account. Standard gVisor instances cannot run a Docker daemon.
Choose a full VM when you need Docker or Compose inside the worker. The agent37-ubuntu template includes Docker, Compose, and buildx. Its default allocation is 2 vCPU, 4 GB RAM, and 12 GB disk. Keep container data on the billed persistent disk; the Ubuntu template already configures Docker that way.
The following estimates use the same 2 vCPU, 4 GB RAM, and 12 GB retained disk for both runtime types. Disk stays allocated for 730 hours in each case:
Runtime
60 running hours, disk retained all month
730 running hours
Standard gVisor
$1.44
$5.48
Full VM
$1.98
$12.08
At the published rates, standard compute is $4.40 per 730 hours for this CPU/RAM shape. VM compute is 2.5 times that, or $11.00. Twelve gigabytes of disk adds $1.08 in either case. For example, the 60-hour VM estimate is $11 × 60 / 730 + $1.08, rounded to $1.98.
Compute bills while the instance runs, even when CPU usage is idle. Stopping or sleeping pauses compute; retained disk keeps billing. Auto-sleep must be enabled. Model usage and separately billed tools are excluded from these infrastructure estimates, and the comparison makes no throughput claim.
Eligible new Cloud accounts receive $5 starter credit after adding a card; adding it does not charge the card. VM creation requires a funded workspace. Check the current offer and fund the runtime you need before provisioning.
Start with one repository. Verify authentication, file persistence, and a real test run. Then create an Agent37 sandbox and select standard Codex hosting or a full VM according to whether Docker must run inside it.
FAQ
Does deleting the container delete my work?
Not with the mounts shown here. Project files live in the host directory, and Codex state lives in the named volume. Unmounted files in the container's writable layer disappear when that container is removed.
Does containerizing Codex include model usage?
No. Container hosting and model access are separate. Use an eligible ChatGPT account or supported API authentication, and budget its usage independently of compute and disk.
Can I run this Compose setup inside a standard Agent37 sandbox?
The standard runtime cannot host a Docker daemon. Use a full VM for this Docker workflow, or use agent37-codex directly when your requirement is the coding agent itself.
Is Docker Sandboxes the same as this Compose file?
No. Docker Sandboxes uses sbx and has its own environment, policy, and credential handling. This guide's Compose file gives you a small CLI container that you operate. Choose based on the isolation and operations you need.
Founder at Agent37, which runs managed hosting for OpenClaw and Hermes agents for 1,000+ users. Writes about what actually breaks when you leave an AI agent running.