0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[ClaudeCode] Python開発用Devcontainer設定備忘録

0
Last updated at Posted at 2025-09-06

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",
    "postCreateCommand": "bash .devcontainer/setup-git-hooks.sh",
	"initializeCommand": "echo \"GH_TOKEN=$(gh auth token)\" > .devcontainer/.env.devcontainer",
	"containerUser": "vscode"
}

ポイント

  1. docker-composeを使う。理由はいろんなプロジェクトで大体あとから別のコンポーネントに依存してdocker composeになるので、最初からdocker composeにしておく
  2. claude-code, gh, pre-commit, tig, actionlintなどはfeaturesで入れておく
  3. tigはfeaturesになかったので自分でfeaturesを作成した https://github.com/nakamasato/devcontainers-features
  4. CLAUDE CODE用の設定 CLAUDE_CONFIG_DIR とマウント設定 (Dockerfile)をする
  5. initializeCommandで、gh auth tokenを GH_TOKENにセットすることで毎回認証する手間を防ぐ
  6. postStartCommandでpre-commit installしておく
  7. postCreateCommandで ~/.zshrc に git --no-verifyを防ぐための設定を追加
  8. containerUserで"node" などの non-root ユーザを設定しておく

.devcontainer/setup-git-hooks.sh

Claude Codeが git commit --no-verifyを使ってかいくぐってしまうので、以下のスクリプトを追加

.devcontainer/setup-git-hooks.sh
#!/bin/bash

# Add git function to prevent --no-verify usage
cat >> ~/.zshrc << 'EOF'
git() {
    if [[ "$1" == "commit" ]]; then
        for arg in "$@"; do
            if [[ "$arg" == "--no-verify" || "$arg" == "-n" ]]; then
                echo "❌ ERROR: --no-verify bypasses quality checks and is forbidden"
                echo "Pre-commit hooks ensure code quality. Please fix issues instead of bypassing them."
                return 1
            fi
        done
    fi
    command git "$@"
}
EOF

AIエージェントによる git commit --no-verify を完全に防ぐ方法で紹介されていたのをDevcontainerでも使えるように設定しています。

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:
  1. Dockerfileを使って柔軟に設定できるようにしておく
  2. 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
  1. baseイメージに mcr.microsoft.com/devcontainers/python:1-3.12-bullseye を使う
  2. コマンドの永続化するfolderと権限を付与する

参照

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?