devcontainer.json
.devcontainer/devcontainer.json
{
"name": "Python 3",
"dockerComposeFile": "docker-compose.yml",
"service": "devcontainer",
"workspaceFolder": "/workspaces/flask-sample",
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/anthropics/devcontainer-features/claude-code:1.0": {},
"ghcr.io/devcontainers-extra/features/poetry:2": {},
"ghcr.io/devcontainers-extra/features/pre-commit:2": {},
"ghcr.io/devcontainers/features/common-utils:2": {
"installZsh": true,
"installOhMyZsh": true,
"configureZshAsDefaultShell": true
},
"ghcr.io/nakamasato/devcontainers-features/tig:1": {},
"ghcr.io/devcontainers-extra/features/actionlint:1": {}
},
"customizations": {
"vscode": {
"extensions": [
"shd101wyy.markdown-preview-enhanced"
]
}
},
"containerEnv": {
"CLAUDE_CONFIG_DIR": "/home/vscode/.claude"
},
"postStartCommand": "pre-commit install",
"initializeCommand": "echo \"GH_TOKEN=$(gh auth token)\" > .devcontainer/.env.devcontainer",
"containerUser": "vscode"
}
ポイント
- docker-composeを使う。理由はいろんなプロジェクトで大体あとから別のコンポーネントに依存してdocker composeになるので、最初からdocker composeにしておく
- claude-code, gh, pre-commit, tig, actionlintなどはfeaturesで入れておく
- tigはfeaturesになかったので自分でfeaturesを作成した https://github.com/nakamasato/devcontainers-features
- CLAUDE CODE用の設定
CLAUDE_CONFIG_DIR
とマウント設定 (Dockerfile)をする - initializeCommandで、gh auth tokenを GH_TOKENにセットすることで毎回認証する手間を防ぐ
- postStartCommandでpre-commit installしておく
- containerUserで"node" などの non-root ユーザを設定しておく
docker-compose.yml
.devcontainer/docker-compose.yml
services:
devcontainer:
build: .
volumes:
- ../..:/workspaces:cached
- commandhistory:/commandhistory
- claude-code-config:/home/vscode/.claude
env_file:
- ../docker/env_file/env.app
- ../docker/env_file/env.mysql
- .env.devcontainer
command: sleep infinity
ports:
- 8000:5000
# 必要に応じてpostgres, redisなど他の依存コンテナを追加
mysql:
image: mysql:8.0
restart: unless-stopped
volumes:
- db_data:/var/lib/mysql
- ../docker/mysql/schema/user.sql:/docker-entrypoint-initdb.d/01_schema_user.sql
- ../docker/mysql/data/user.sql:/docker-entrypoint-initdb.d/11_data_user.sql
- ../docker/mysql/conf.d/:/etc/mysql/conf.d/
env_file:
- ../docker/env_file/env.mysql
ports:
- 3306:3306
healthcheck:
test: ["CMD", "mysqladmin", "ping"]
interval: 10s
timeout: 5s
retries: 5
volumes:
db_data:
commandhistory:
claude-code-config:
- Dockerfileを使って柔軟に設定できるようにしておく
- home以下のcache, commandhistory, claude code configなどをvolumeに永続化しておく
Dockerfile
.devcontainer/Dockerfile
FROM mcr.microsoft.com/devcontainers/python:1-3.12-bullseye
ARG USERNAME=vscode
# Used to persist shell history
RUN mkdir /commandhistory \
&& touch /commandhistory/.bash_history \
&& touch /commandhistory/.zsh_history \
&& chown -R $USERNAME /commandhistory \
&& echo 'export PROMPT_COMMAND="history -a" && export HISTFILE=/commandhistory/.bash_history' >> "/home/$USERNAME/.bashrc" \
&& echo 'export HISTFILE=/commandhistory/.zsh_history' >> "/home/$USERNAME/.zshrc"
# Create claude config directories and set permissions
RUN mkdir -p /home/$USERNAME/.claude && \
chown -R $USERNAME /home/$USERNAME/.claude
- baseイメージに
mcr.microsoft.com/devcontainers/python:1-3.12-bullseye
を使う - コマンドの永続化するfolderと権限を付与する