|
| 1 | +# Codex Docker |
| 2 | + |
| 3 | +Run [OpenAI Codex CLI](https://github.com/openai/codex) in a Docker container. Codex is OpenAI's lightweight coding agent that runs in your terminal. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```bash |
| 8 | +# Pull and run (replace with your API key) |
| 9 | +docker run -it --rm \ |
| 10 | + -v $(pwd):/workspace \ |
| 11 | + -e OPENAI_API_KEY=your-key \ |
| 12 | + ungb/codex |
| 13 | +``` |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- [Docker](https://docs.docker.com/get-docker/) installed |
| 18 | +- [OpenAI API key](https://platform.openai.com/api-keys) or ChatGPT account for OAuth |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +### Using Docker Run |
| 23 | + |
| 24 | +```bash |
| 25 | +# Basic usage with API key |
| 26 | +docker run -it --rm \ |
| 27 | + -v $(pwd):/workspace \ |
| 28 | + -e OPENAI_API_KEY=$OPENAI_API_KEY \ |
| 29 | + ungb/codex |
| 30 | + |
| 31 | +# With persistent config (remembers settings between runs) |
| 32 | +docker run -it --rm \ |
| 33 | + -v $(pwd):/workspace \ |
| 34 | + -v codex-config:/home/coder/.codex \ |
| 35 | + -e OPENAI_API_KEY=$OPENAI_API_KEY \ |
| 36 | + ungb/codex |
| 37 | + |
| 38 | +# With git/ssh support |
| 39 | +docker run -it --rm \ |
| 40 | + -v $(pwd):/workspace \ |
| 41 | + -v codex-config:/home/coder/.codex \ |
| 42 | + -v ~/.ssh:/home/coder/.ssh:ro \ |
| 43 | + -v ~/.gitconfig:/home/coder/.gitconfig:ro \ |
| 44 | + -e OPENAI_API_KEY=$OPENAI_API_KEY \ |
| 45 | + ungb/codex |
| 46 | +``` |
| 47 | + |
| 48 | +### Using Docker Compose |
| 49 | + |
| 50 | +1. Clone this repo or copy `docker-compose.yml` to your project: |
| 51 | + |
| 52 | +```bash |
| 53 | +curl -O https://raw.githubusercontent.com/ungb/codex-docker/main/docker-compose.yml |
| 54 | +``` |
| 55 | + |
| 56 | +2. Create a `.env` file with your API key: |
| 57 | + |
| 58 | +```bash |
| 59 | +echo "OPENAI_API_KEY=your-key-here" > .env |
| 60 | +``` |
| 61 | + |
| 62 | +3. Run: |
| 63 | + |
| 64 | +```bash |
| 65 | +docker compose run --rm codex |
| 66 | +``` |
| 67 | + |
| 68 | +### Run a Specific Command |
| 69 | + |
| 70 | +```bash |
| 71 | +# Run codex with arguments |
| 72 | +docker run -it --rm \ |
| 73 | + -v $(pwd):/workspace \ |
| 74 | + -e OPENAI_API_KEY=$OPENAI_API_KEY \ |
| 75 | + ungb/codex \ |
| 76 | + codex "explain this codebase" |
| 77 | + |
| 78 | +# Check version |
| 79 | +docker run -it --rm ungb/codex codex --version |
| 80 | + |
| 81 | +# Run with full auto-approve (be careful!) |
| 82 | +docker run -it --rm \ |
| 83 | + -v $(pwd):/workspace \ |
| 84 | + -e OPENAI_API_KEY=$OPENAI_API_KEY \ |
| 85 | + ungb/codex \ |
| 86 | + codex --full-auto "fix the tests" |
| 87 | +``` |
| 88 | + |
| 89 | +## Authentication |
| 90 | + |
| 91 | +### Option 1: API Key (Recommended for Docker) |
| 92 | + |
| 93 | +Get an API key from [OpenAI Platform](https://platform.openai.com/api-keys) and pass it as an environment variable: |
| 94 | + |
| 95 | +```bash |
| 96 | +-e OPENAI_API_KEY=sk-... |
| 97 | +``` |
| 98 | + |
| 99 | +### Option 2: ChatGPT OAuth Login |
| 100 | + |
| 101 | +For browser-based OAuth, expose port 1455 for the callback: |
| 102 | + |
| 103 | +```bash |
| 104 | +docker run -it --rm \ |
| 105 | + -p 1455:1455 \ |
| 106 | + -v $(pwd):/workspace \ |
| 107 | + -v codex-config:/home/coder/.codex \ |
| 108 | + ungb/codex \ |
| 109 | + codex login |
| 110 | +``` |
| 111 | + |
| 112 | +Or use host network mode: |
| 113 | + |
| 114 | +```bash |
| 115 | +docker run -it --rm \ |
| 116 | + --network host \ |
| 117 | + -v $(pwd):/workspace \ |
| 118 | + -v codex-config:/home/coder/.codex \ |
| 119 | + ungb/codex \ |
| 120 | + codex login |
| 121 | +``` |
| 122 | + |
| 123 | +## Volume Mounts |
| 124 | + |
| 125 | +| Mount | Purpose | |
| 126 | +|-------|---------| |
| 127 | +| `/workspace` | Your project directory (required) | |
| 128 | +| `/home/coder/.codex` | Codex config and cache (optional, for persistence) | |
| 129 | +| `/home/coder/.ssh` | SSH keys for git operations (optional, read-only) | |
| 130 | +| `/home/coder/.gitconfig` | Git configuration (optional, read-only) | |
| 131 | + |
| 132 | +## Environment Variables |
| 133 | + |
| 134 | +| Variable | Required | Description | |
| 135 | +|----------|----------|-------------| |
| 136 | +| `OPENAI_API_KEY` | Yes* | Your OpenAI API key | |
| 137 | +| `OPENAI_ORG_ID` | No | OpenAI organization ID | |
| 138 | +| `OPENAI_API_BASE` | No | Custom API endpoint (for proxies) | |
| 139 | + |
| 140 | +*Required unless using OAuth login |
| 141 | + |
| 142 | +## Ports |
| 143 | + |
| 144 | +| Port | Purpose | |
| 145 | +|------|---------| |
| 146 | +| 1455 | OAuth callback for `codex login` | |
| 147 | + |
| 148 | +## Building Locally |
| 149 | + |
| 150 | +```bash |
| 151 | +git clone https://github.com/ungb/codex-docker.git |
| 152 | +cd codex-docker |
| 153 | +docker build -t codex . |
| 154 | +``` |
| 155 | + |
| 156 | +## Troubleshooting |
| 157 | + |
| 158 | +### Permission Denied on Mounted Files |
| 159 | + |
| 160 | +The container runs as user `coder` (UID 1000). If you have permission issues: |
| 161 | + |
| 162 | +```bash |
| 163 | +# Run with your user ID |
| 164 | +docker run -it --rm \ |
| 165 | + --user $(id -u):$(id -g) \ |
| 166 | + -v $(pwd):/workspace \ |
| 167 | + -e OPENAI_API_KEY=$OPENAI_API_KEY \ |
| 168 | + ungb/codex |
| 169 | +``` |
| 170 | + |
| 171 | +### Git Operations Failing |
| 172 | + |
| 173 | +Ensure SSH keys are mounted and git is configured: |
| 174 | + |
| 175 | +```bash |
| 176 | +docker run -it --rm \ |
| 177 | + -v $(pwd):/workspace \ |
| 178 | + -v ~/.ssh:/home/coder/.ssh:ro \ |
| 179 | + -v ~/.gitconfig:/home/coder/.gitconfig:ro \ |
| 180 | + -e OPENAI_API_KEY=$OPENAI_API_KEY \ |
| 181 | + ungb/codex |
| 182 | +``` |
| 183 | + |
| 184 | +### OAuth Login Not Working |
| 185 | + |
| 186 | +Ensure port 1455 is exposed: |
| 187 | + |
| 188 | +```bash |
| 189 | +docker run -it --rm \ |
| 190 | + -p 1455:1455 \ |
| 191 | + -v $(pwd):/workspace \ |
| 192 | + -v codex-config:/home/coder/.codex \ |
| 193 | + ungb/codex \ |
| 194 | + codex login |
| 195 | +``` |
| 196 | + |
| 197 | +## Sandbox Mode |
| 198 | + |
| 199 | +Codex supports running in sandbox mode using Docker. When you run Codex inside this container, it's already isolated. For nested Docker support (Docker-in-Docker), mount the Docker socket: |
| 200 | + |
| 201 | +```bash |
| 202 | +docker run -it --rm \ |
| 203 | + -v $(pwd):/workspace \ |
| 204 | + -v /var/run/docker.sock:/var/run/docker.sock \ |
| 205 | + -e OPENAI_API_KEY=$OPENAI_API_KEY \ |
| 206 | + ungb/codex |
| 207 | +``` |
| 208 | + |
| 209 | +## License |
| 210 | + |
| 211 | +MIT License - see [LICENSE](LICENSE) |
| 212 | + |
| 213 | +## Links |
| 214 | + |
| 215 | +- [Codex CLI Documentation](https://github.com/openai/codex) |
| 216 | +- [OpenAI Platform](https://platform.openai.com/) |
| 217 | +- [OpenAI API Keys](https://platform.openai.com/api-keys) |
0 commit comments