Skip to content

Commit dd2750d

Browse files
committed
docs: added README.md
1 parent 7a57cb6 commit dd2750d

File tree

1 file changed

+300
-0
lines changed

1 file changed

+300
-0
lines changed

README.md

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
# Codex CLI Container<!-- omit from toc -->
2+
3+
- [Container Architecture](#container-architecture)
4+
- [Building the Container Image](#building-the-container-image)
5+
- [Build Arguments](#build-arguments)
6+
- [Authentication Setup](#authentication-setup)
7+
- [Using OPENAI_API_KEY](#using-openai_api_key)
8+
- [Environment File Option](#environment-file-option)
9+
- [Optional Variables](#optional-variables)
10+
- [Verifying Authentication](#verifying-authentication)
11+
- [Azure OpenAI Setup](#azure-openai-setup)
12+
- [Environment Variables](#environment-variables)
13+
- [Configure ~/.codex/config.toml](#configure-codexconfigtoml)
14+
- [Docker Run Examples (Azure)](#docker-run-examples-azure)
15+
- [Working with Codex CLI from the Container](#working-with-codex-cli-from-the-container)
16+
- [Basic Usage Pattern](#basic-usage-pattern)
17+
- [Volume Mounts Explained](#volume-mounts-explained)
18+
- [Working Directory Context](#working-directory-context)
19+
- [Usage Examples](#usage-examples)
20+
- [Interactive Session](#interactive-session)
21+
- [Single Command Execution](#single-command-execution)
22+
- [Shell Alias for Convenience](#shell-alias-for-convenience)
23+
- [File Permissions](#file-permissions)
24+
- [Option 1: Build with Custom UID](#option-1-build-with-custom-uid)
25+
- [Option 2: Fix Permissions After Creation](#option-2-fix-permissions-after-creation)
26+
- [Troubleshooting](#troubleshooting)
27+
- [Authentication Issues](#authentication-issues)
28+
- [File Access Issues](#file-access-issues)
29+
- [Container Issues](#container-issues)
30+
31+
A containerized environment for running the OpenAI Codex CLI. This image provides a rootless, minimal setup so you can run `codex` commands with local file access and API key–based authentication.
32+
33+
## Container Architecture
34+
35+
- **Rootless execution**: Runs as user `codex` (UID 1000) instead of root
36+
- **Minimal base**: Uses `node:22-slim` for a smaller attack surface
37+
- **CLI entrypoint**: Container entrypoint is `codex`
38+
39+
## Building the Container Image
40+
41+
Build from the provided Dockerfile:
42+
43+
```bash
44+
docker build -t codex-cli:dev .
45+
```
46+
47+
### Build Arguments
48+
49+
Customize via build args if needed:
50+
51+
```bash
52+
docker build \
53+
--build-arg CODEX_CLI_VERSION=latest \
54+
--build-arg USERNAME=codex \
55+
--build-arg UID=1000 \
56+
--build-arg GID=1000 \
57+
-t codex-cli:dev .
58+
```
59+
60+
## Authentication Setup
61+
62+
The Codex CLI authenticates with OpenAI using an API key. Pass your key as an environment variable when running the container.
63+
64+
### Using OPENAI_API_KEY
65+
66+
Avoid placing secrets directly on the command line (they can leak via shell history and process inspection). Use an ephemeral prompt and pass the variable through:
67+
68+
```bash
69+
# Prompts without echoing; does not store secret in history
70+
read -s OPENAI_API_KEY && \
71+
docker run -it \
72+
-v ${PWD}:/work \
73+
-e OPENAI_API_KEY \
74+
--rm codex-cli:dev --help; \
75+
unset OPENAI_API_KEY
76+
```
77+
78+
### Environment File Option
79+
80+
```bash
81+
# Create a protected env file (avoid echoing secrets in your history)
82+
install -m 600 /dev/null .env
83+
84+
# Edit securely with your editor to add keys
85+
${EDITOR:-vi} .env
86+
87+
# Example contents to add (edit in the editor):
88+
# OPENAI_API_KEY=...
89+
# OPENAI_ORG_ID=org_...
90+
# OPENAI_PROJECT=proj_...
91+
# OPENAI_BASE_URL=https://api.openai.com/v1
92+
93+
# Use the environment file with Docker
94+
docker run -it -v ${PWD}:/work --env-file .env --rm codex-cli:dev --help
95+
```
96+
97+
Security tips for env files:
98+
99+
- Keep `.env` out of version control (add to `.gitignore`).
100+
- Restrict permissions to owner read/write only (`chmod 600 .env`).
101+
- Prefer short‑lived keys and rotate regularly.
102+
103+
### Optional Variables
104+
105+
- `OPENAI_ORG_ID`: Organization identifier if required by your account
106+
- `OPENAI_PROJECT`: Project identifier for scoping usage
107+
- `OPENAI_BASE_URL`: Alternate base URL if using a proxy or compatible endpoint
108+
109+
### Verifying Authentication
110+
111+
Run a simple command and confirm it executes without auth errors:
112+
113+
```bash
114+
docker run -it -v ${PWD}:/work --env-file .env --rm codex-cli:dev --version
115+
```
116+
117+
## Azure OpenAI Setup
118+
119+
Use this if your OpenAI models are deployed on Azure OpenAI. You will need:
120+
121+
- An Azure OpenAI resource and at least one model deployment name (e.g., a chat model and optionally an embeddings model)
122+
- The Azure OpenAI API key for that resource
123+
124+
There are two supported ways to configure Codex for Azure: via environment variables or via a config file at `~/.codex/config.toml` (as described in Microsoft’s guide).
125+
126+
### Environment Variables
127+
128+
Set these to point Codex at your Azure OpenAI endpoint:
129+
130+
- `AZURE_OPENAI_API_KEY`: Your Azure OpenAI resource API key
131+
- `OPENAI_BASE_URL`: Your Azure endpoint base URL, typically `https://<resource-name>.openai.azure.com/openai`
132+
- `OPENAI_API_VERSION`: The Azure OpenAI API version, for example `2024-05-01-preview`
133+
134+
When targeting a specific deployment, pass it as the model name (the deployment name), for example with `--model <your-deployment-name>` when invoking Codex commands.
135+
136+
Example `.env` for Azure:
137+
138+
```env
139+
AZURE_OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
140+
OPENAI_BASE_URL=https://<your-resource>.openai.azure.com/openai
141+
OPENAI_API_VERSION=2024-05-01-preview
142+
```
143+
144+
### Configure ~/.codex/config.toml
145+
146+
Codex also supports a TOML config file in your home directory. This is useful to persist Azure settings and deployment names without repeating flags. Create `~/.codex/config.toml` on your host with content like:
147+
148+
```toml
149+
# ~/.codex/config.toml
150+
[default]
151+
provider = "azure-openai"
152+
153+
[providers.azure-openai]
154+
# Your Azure OpenAI resource endpoint (no trailing /openai path needed here)
155+
endpoint = "https://<your-resource>.openai.azure.com"
156+
api_version = "2024-05-01-preview"
157+
158+
# Deployment names you created in the Azure OpenAI resource
159+
chat_deployment = "<your-chat-deployment>"
160+
embedding_deployment = "<your-embeddings-deployment>"
161+
162+
# Use this environment variable for the API key
163+
api_key_env = "AZURE_OPENAI_API_KEY"
164+
```
165+
166+
Notes:
167+
168+
- Mount your host home directory into the container so Codex can read `~/.codex/config.toml` at `/home/codex/.codex/config.toml`.
169+
- Keep your API key out of the file; the key is read from the environment variable specified by `api_key_env`.
170+
171+
### Docker Run Examples (Azure)
172+
173+
Using environment variables only (without exposing secrets on the command line):
174+
175+
```bash
176+
# Prompt for the Azure key without echoing
177+
read -s AZURE_OPENAI_API_KEY && \
178+
docker run -it --rm \
179+
-v ${PWD}:/work \
180+
-e AZURE_OPENAI_API_KEY \
181+
-e OPENAI_BASE_URL="https://<your-resource>.openai.azure.com/openai" \
182+
-e OPENAI_API_VERSION="2024-05-01-preview" \
183+
codex-cli:dev --help; \
184+
unset AZURE_OPENAI_API_KEY
185+
```
186+
187+
Using `~/.codex/config.toml` plus an `.env` file that only carries the key:
188+
189+
```bash
190+
# Ensure ~/.codex/config.toml exists on host; .env carries only the key
191+
# Create protected .env (once):
192+
install -m 600 /dev/null .env
193+
${EDITOR:-vi} .env
194+
195+
# In .env, add only:
196+
# AZURE_OPENAI_API_KEY=...
197+
198+
docker run -it --rm \
199+
-v $HOME:/home/codex \
200+
-v ${PWD}:/work \
201+
--env-file .env \
202+
codex-cli:dev --help
203+
```
204+
205+
## Working with Codex CLI from the Container
206+
207+
Use a working directory mount so Codex can read/write files in your project.
208+
209+
### Basic Usage Pattern
210+
211+
```bash
212+
docker run -it -v ${PWD}:/work --env-file .env --rm codex-cli:dev [CODEX_ARGS]
213+
```
214+
215+
Replace `[CODEX_ARGS]` with the arguments supported by your installed `@openai/codex` version (see `--help`).
216+
217+
### Volume Mounts Explained
218+
219+
- `-v ${PWD}:/work`: Maps your current directory into the container working directory
220+
- `--rm`: Removes the container after the command finishes
221+
- `-it`: Interactive TTY for prompts and multi-step workflows
222+
223+
### Working Directory Context
224+
225+
The image sets `/work` as the working directory. This means:
226+
227+
- Files in your current directory are accessible within the container
228+
- Output files are written back to your current directory
229+
- Relative paths behave as expected
230+
231+
## Usage Examples
232+
233+
### Interactive Session
234+
235+
Start an interactive session to run multiple Codex commands:
236+
237+
```bash
238+
docker run -it -v ${PWD}:/work --env-file .env --rm codex-cli:dev
239+
```
240+
241+
### Single Command Execution
242+
243+
Run a single command, for example to see help or version information:
244+
245+
```bash
246+
# Help
247+
docker run -it -v ${PWD}:/work --env-file .env --rm codex-cli:dev --help
248+
249+
# Version
250+
docker run -it -v ${PWD}:/work --env-file .env --rm codex-cli:dev --version
251+
```
252+
253+
### Shell Alias for Convenience
254+
255+
Create a shell alias for shorter commands:
256+
257+
```bash
258+
# Add to your ~/.bashrc or ~/.zshrc
259+
alias codex='docker run -it -v ${PWD}:/work --env-file .env --rm codex-cli:dev'
260+
261+
# Then simply use:
262+
codex --help
263+
```
264+
265+
## File Permissions
266+
267+
The container runs as user `codex` with UID 1000. If your host user has a different UID/GID, you may encounter permission issues. To resolve this:
268+
269+
### Option 1: Build with Custom UID
270+
271+
```bash
272+
docker build \
273+
--build-arg UID=$(id -u) \
274+
--build-arg GID=$(id -g) \
275+
-t codex-cli:dev .
276+
```
277+
278+
### Option 2: Fix Permissions After Creation
279+
280+
```bash
281+
# If files are created with unexpected ownership
282+
sudo chown -R $(id -u):$(id -g) ./path-to-files
283+
```
284+
285+
## Troubleshooting
286+
287+
### Authentication Issues
288+
289+
- Missing or invalid API key: Ensure `OPENAI_API_KEY` is set (via `-e` or `--env-file`)
290+
- Org/project scoping: If required, set `OPENAI_ORG_ID` and/or `OPENAI_PROJECT`
291+
292+
### File Access Issues
293+
294+
- Files not visible: Confirm `-v ${PWD}:/work` is included
295+
- Wrong ownership: Rebuild with your UID/GID or adjust ownership afterward
296+
297+
### Container Issues
298+
299+
- Container fails to start: Verify Docker is running and the image built successfully
300+
- Command not found: Ensure arguments come after the image name and use `--help` to list supported commands

0 commit comments

Comments
 (0)