This repository demonstrates how to run the OpenAI Codex CLI inside a Docker container with your local Windows folder mounted for development. It uses an .env file to securely provide your API key and includes .gitignore and .dockerignore to keep secrets and unnecessary files out of version control.
C:\CodexApp
├── Dockerfile
├── docker-compose.yml
├── .env
├── .gitignore
└── .dockerignore
- Docker Desktop installed and running on Windows
- A valid OpenAI API key
In C:\CodexApp\.env, add:
OPENAI_API_KEY=sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Note:
- Never commit the
.envfile to Git.- Rotate or revoke your key by updating this file.
# Ignore Node modules node_modules/ # Ignore environment files .env # Ignore Docker Compose overrides docker-compose.override.yml
# Exclude secrets and VCS from build context
.env
.git
node_modules
Create C:\CodexApp\Dockerfile with:
FROM node:22-slim # Set working directory inside container WORKDIR /usr/src/app # Install the Codex CLI globally RUN npm install -g @openai/codex # Default to bash for interactive use CMD ["bash"]
Create C:\CodexApp\docker-compose.yml:
version: "3.9" services: codex: build: . volumes: - C:\\CodexApp:/usr/src/app # Mount local Windows folder working_dir: /usr/src/app env_file: - .env # Load OPENAI_API_KEY from .env tty: true # Keep the shell open for interaction
- Open PowerShell and navigate to your project folder:
cd C:\CodexApp
- Build the Docker image:
docker-compose build - Start an interactive shell session in the container:
docker-compose run codex- You will be at
/usr/src/appinside the container. - Try verifying your key:
echo $OPENAI_API_KEY
- Run Codex commands:
codex "Explain what this script does"
- You will be at
- Run one-off Codex commands without entering the shell:
docker-compose run codex codex "Generate a Node.js HTTP server"
- Editing Locally: Any edits you make in
C:\Workingare immediately available inside the container. - Safety: Secrets never get baked into the image or committed to Git.
- Extensibility: You can add other services (databases, caches) to the
docker-compose.ymllater.
Happy coding with OpenAI Codex in Docker!