Skip to content

Commit 275c17b

Browse files
committed
feat: allow mounting user config in run_in_container.sh
Enables users to persist Codex CLI configuration across container runs by optionally mounting their local `.codex` directory using the `--config` parameter. Fixes openai#789
1 parent 84e01f4 commit 275c17b

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

codex-cli/scripts/run_in_container.sh

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@ WORK_DIR="${WORKSPACE_ROOT_DIR:-$(pwd)}"
1313
# Default allowed domains - can be overridden with OPENAI_ALLOWED_DOMAINS env var
1414
OPENAI_ALLOWED_DOMAINS="${OPENAI_ALLOWED_DOMAINS:-api.openai.com}"
1515

16-
# Parse optional flag.
17-
if [ "$1" = "--work_dir" ]; then
18-
if [ -z "$2" ]; then
19-
echo "Error: --work_dir flag provided but no directory specified."
20-
exit 1
21-
fi
22-
WORK_DIR="$2"
23-
shift 2
24-
fi
16+
# Default config path - can be overridden with CONFIG_PATH env var
17+
CONFIG_PATH="${CONFIG_PATH:-$HOME/.codex}"
18+
VOLUME_FLAGS=()
19+
EXTRA_CODEX_ARGS=()
20+
21+
# Parse flags: --work_dir DIR, --config DIR & any extra arguments
22+
while [[ $# -gt 0 ]]; do
23+
case "$1" in
24+
--work-dir)
25+
WORK_DIR="$2"; shift 2 ;;
26+
--config)
27+
CONFIG_PATH="$2"; shift 2 ;;
28+
*)
29+
EXTRA_CODEX_ARGS+=("$1"); shift ;;
30+
esac
31+
done
2532

2633
WORK_DIR=$(realpath "$WORK_DIR")
2734

@@ -36,8 +43,8 @@ cleanup() {
3643
trap cleanup EXIT
3744

3845
# Ensure a command is provided.
39-
if [ "$#" -eq 0 ]; then
40-
echo "Usage: $0 [--work_dir directory] \"COMMAND\""
46+
if [ ${#EXTRA_CODEX_ARGS[@]} -eq 0 ]; then
47+
echo "Usage: $0 [--work-dir directory] \"COMMAND\""
4148
exit 1
4249
fi
4350

@@ -53,6 +60,11 @@ if [ -z "$OPENAI_ALLOWED_DOMAINS" ]; then
5360
exit 1
5461
fi
5562

63+
# check if the config directory exists
64+
if [[ -e "$CONFIG_PATH" ]]; then
65+
VOLUME_FLAGS+=("-v" "$CONFIG_PATH:/home/node/.codex:ro")
66+
fi
67+
5668
# Kill any existing container for the working directory using cleanup(), centralizing removal logic.
5769
cleanup
5870

@@ -62,6 +74,7 @@ docker run --name "$CONTAINER_NAME" -d \
6274
--cap-add=NET_ADMIN \
6375
--cap-add=NET_RAW \
6476
-v "$WORK_DIR:/app$WORK_DIR" \
77+
"${VOLUME_FLAGS[@]}" \
6578
codex \
6679
sleep infinity
6780

@@ -89,7 +102,9 @@ docker exec --user root "$CONTAINER_NAME" bash -c "rm -f /usr/local/bin/init_fir
89102
# We use a parameterized bash command to safely handle the command and directory.
90103

91104
quoted_args=""
92-
for arg in "$@"; do
105+
for arg in "${EXTRA_CODEX_ARGS[@]}"; do
93106
quoted_args+=" $(printf '%q' "$arg")"
94107
done
95-
docker exec -it "$CONTAINER_NAME" bash -c "cd \"/app$WORK_DIR\" && codex --full-auto ${quoted_args}"
108+
109+
docker exec -it "$CONTAINER_NAME" bash -c \
110+
"cd \"/app$WORK_DIR\" && codex ${quoted_args}"

0 commit comments

Comments
 (0)