2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Claude CodeをDevContainer内で使うための設定メモ

Last updated at Posted at 2025-06-04

Claude CodeをDockerで使う自分用メモ

Claude Codeとはといったことは詳しい説明がいたるところにあるので省略。ここではローカルにインストールして環境を変えることがないように、Docker(VscodのDevContainer)内で使えるように設定をしました。

Dockerfile

Dockerfile
FROM python:3

ARG USERNAME=user
ARG GROUPNAME=user
ARG UID=1000
ARG GID=1000

#Group and USER
RUN groupadd -g $GID $GROUPNAME \
&& useradd -m -s /bin/bash -u $UID -g $GID $USERNAME \
&& apt-get update && apt-get install -y sudo \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME 

USER $USERNAME

Dockerfileは必要最小限。利用するユーザー名を決めるだけ。とりあえずpythonのイメージとするけど、開発するものに合わせて変えてください。他に必要なものは、devcontainer.jsonでfeatureを使ってインストールします。user名はコンテナ内で使う名前を任意で決められます。

devcontainer.json

devcontainer.json
{
	"name": "ClaudeCode Container",
	"build": {
		"dockerfile": "Dockerfile",
		"context": "."
	},

	"features": {
		"ghcr.io/devcontainers/features/node:1": {},
    	"ghcr.io/anthropics/devcontainer-features/claude-code:1.0": {}
	},
	"mounts":[
		 {
			"source": "${localWorkspaceFolder}/workspace",
            "target": "${containerWorkspaceFolder}",
			"type": "bind"
		}
	],
	"remoteUser": "user",
    "workspaceFolder": "/home/user/workspace",
	"postCreateCommand": "/home/user/workspace/changebash.sh"
}

node.jsとClaude Codeはfeaturesでインストールする。
remoteUserはDockerfileと同じもの。postCreateCommandはなくても良いが、プロンプトを変えたいので次のようにchangebash.shを作っている。

postCreateCommand用のスクリプト

changebash.sh
#!/bin/bash

cp  /home/user/.bashrc /home/user/bashrc.bak
cp /home/user/workspace/bashrc /home/user/.bashrc
rm -f /home/user/bashrc.bak
source /home/user/.bashrc

元の.bashrcを自作のものと入れ替える。自作と言っても元のものを一箇所変えるだけ。

.bashrc
 # カレントディレクトリがGitリポジトリであれば、ブランチ名を表示するように変更
   PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]:\[\e[31m\]$(branch=$(git branch --show-current 2>/dev/null); [ -n "$branch" ] && echo "($branch)\[\e[0m\]:")\[\e[0m\]\$'

これでプロンプトにGitのブランチ表示がされるようになる。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?