📂

VS Code + Docker Desktop デフォルトが親ディレクトリーをマウントする設定の理由

に公開

経緯

VS Code のコマンドパレット (Ctrl+Shift+P, ⇧⌘P, または F1) で
[Add dev container configuration files...] を選択すると追加される
Developing inside a Container の設定ファイルが、
2023 年頃、次のように変更されました:

.devcontainer/docker-compose.yml
version: '3.8'
services:
  # Update this to the name of the service you want to work with in your docker-compose.yml file
  your-service-name-here:

# ~~~ 中略 ~~~

    volumes:
      # Update this to wherever you want VS Code to mount the folder of your project
-       - .:/workspaces:cached
+       - ..:/workspaces:cached
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/docker-existing-docker-compose
{
  // ~~~ 中略 ~~~

  // The optional 'workspaceFolder' property is the path VS Code should open by default when
  // connected. This is typically a file mount in .devcontainer/docker-compose.yml
-   "workspaceFolder": "/workspace"
+.  "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}"
  // ~~~ 中略 ~~~
}

[Add dev container configuration files...] の使い方については次の記事を参照お願いします:

https://futureys.tokyo/lets-develop-program-without-installing-language-by-docker-and-vscode/

理由

Developing inside a Container の方式の 1 つである
clone repository in container volume を行う際の不具合のために
デフォルトの設定が変更となったようです:

https://github.com/microsoft/vscode-remote-release/issues/5388#issuecomment-1262582637

実際の仕様変更は次で行われています:

https://github.com/devcontainers/templates/pull/7

https://github.com/devcontainers/templates/commit/9bbc821f072cd2730b06a029e87476b026b83c47

clone repository in container volume については次の記事で詳しく解説されています:

https://zenn.dev/ki0i0ro0/articles/docker-developer-env

https://weseek.co.jp/tech/4112/

clone repository in container volume ではない場合の問題

この設定は
clone repository in container volume ではない Developing inside a Container をするとき、
リポジトリーの兄弟ディレクトリーがマウントされるので
次のようなリスクがあるため、筆者は昔のデフォルト: /workspace に戻しています:

  • 意図しないアクセス
  • パフォーマンス低下
  • 意図しないセキュリティ的な問題

ちなみに、このことは公式ドキュメントで殆ど解説されておらず、
むしろ /workspace のまま解説されていることが多いです
例:

https://code.visualstudio.com/docs/devcontainers/create-dev-container#_extend-your-docker-compose-file-for-development

Discussion