💨

Codexをdevcontainerで毎回ログインしないで済む方法

に公開

修正版

devcontainer.json
{
        "name": "Ubuntu",
        "image": "mcr.microsoft.com/devcontainers/base:noble",
        "features": {
                "ghcr.io/devcontainers/features/node:1": {}
        },
        "mounts": [
                "source=${env:HOME}/.codex/auth.json,target=/home/vscode/.codex/auth.json,type=bind"
        ],
        "postCreateCommand": "mkdir -p /home/vscode/.codex && sudo chown vscode:vscode /home/vscode/.codex && npm i -g @openai/codex",
}

前の実装はマウントというよりコピーだったので、devcontainer or wsl のどちらかでセッションがリフレッシュされたら、片方はもうリフレッシュできなくなっていました。

何を血迷ったのか。

直接 /home/vscode/.codex/auth.json にマウントしようとすると、.codex の所有者がrootになってしまうのを避けるため、横着してあのような実装をしました。

以下もともとの記事

ローカル (wsl) の認証情報ファイル auth.json をマウントして使いまわすだけ。
何日か使って普通に使えてる感はあるけど副作用は不明。

devcontainer.json
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/ubuntu
{
	"name": "Ubuntu",
	// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
	"image": "mcr.microsoft.com/devcontainers/base:noble",
	"features": {
		"ghcr.io/devcontainers/features/node:1": {}
	},
	"mounts": [
		"source=${env:HOME}/.codex/auth.json,target=/tmp/codex-auth.json,type=bind"
	],
	// Features to add to the dev container. More info: https://containers.dev/features.
	// "features": {},
	// Use 'forwardPorts' to make a list of ports inside the container available locally.
	// "forwardPorts": [],
	// Use 'postCreateCommand' to run commands after the container is created.
	"postCreateCommand": "mkdir -p ~/.codex/ && cp /tmp/codex-auth.json ~/.codex/auth.json && npm i -g @openai/codex",
	// Configure tool-specific properties.
	// "customizations": {},
	// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
	// "remoteUser": "root"
}

Discussion