開發容器 CLI

本主題涵蓋開發容器命令列介面 (dev container CLI),它允許您建置與管理開發容器,並作為 開發容器規範 (Development Containers Specification) 的配套工具。

開發容器 (Development containers)

一致且可預測的環境,是實現高生產力且愉悅的軟體開發體驗之關鍵。

容器(例如 Docker 容器)過去常被用於標準化應用程式的部署,但現在有了絕佳的機會將其應用於更多場景,包括持續整合 (CI)、測試自動化,以及功能完備的程式開發環境。開發容器 (Development container) 提供了此類工作環境,並確保您的專案具備所需的工具與軟體,無論您的專案是複雜的分散式架構,還是僅有少數相依需求。

Diagram comparing dev versus production containers

Visual Studio Code 透過 Dev Containers 擴充功能以及 GitHub Codespaces 支援開發容器。此支援由 devcontainer.json 提供技術支援,這是一種結構化的 JSON with Comments (jsonc) 中繼資料格式,用於設定容器化環境。

隨著容器化生產工作負載變得普遍,開發容器在 VS Code 以外的場景也變得廣泛應用。為了在任何環境中推廣開發容器,開發容器規範 的開發工作已經展開,該規範賦予任何工具的使用者能力,去設定一致的開發環境。開源的 dev container CLI 即作為該規範的參考實作。

開發容器 CLI

當 VS Code 和 Codespaces 等工具在使用者專案中偵測到 devcontainer.json 檔案時,它們會使用 CLI 來設定開發容器。dev container CLI 是一個參考實作,讓個人使用者與其他工具皆能讀取 devcontainer.json 中繼資料並據此建立開發容器。

此 CLI 可以直接使用,也可以整合到產品體驗中,類似於目前與 Dev Containers 和 Codespaces 的整合方式。它目前同時支援簡單的單一容器選項,並整合了 Docker Compose 以處理多容器場景。

此 CLI 可於 devcontainers/cli 儲存庫取得。

安裝

您可以透過 Dev Containers 擴充功能快速試用此 CLI。從命令選擇區(F1)選取 Dev Containers: Install devcontainer CLI 指令。

替代安裝方式

在其他地方使用 CLI 還有額外的選項

  • 安裝其 npm 套件
  • 使用 GitHub Action 或 Azure DevOps Task
  • 從原始碼建置 CLI 儲存庫

在本頁中,我們將專注於使用 npm 套件。

npm install

若要安裝 npm 套件,您需要安裝 Python、Node.js(14 或更高版本)以及 C/C++ 以建置其中一個相依套件。VS Code 的 How to Contribute 維基頁面有關於建議工具組的詳細資訊。

npm install -g @devcontainers/cli

驗證您是否可以執行 CLI 並查看其說明文字

devcontainer <command>

Commands:
  devcontainer up                   Create and run dev container
  devcontainer build [path]         Build a dev container image
  devcontainer run-user-commands    Run user commands
  devcontainer read-configuration   Read configuration
  devcontainer features             Features commands
  devcontainer templates            Templates commands
  devcontainer exec <cmd> [args..]  Execute a command on a running dev container

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]

注意: 如果您是透過 VS Code 安裝的 CLI,將會列出用於開啟開發容器的 open 指令。

執行 CLI

擁有 CLI 後,您可以試著用範例專案執行看看,例如這個 Rust 範例

將 Rust 範例複製到您的機器,並使用 CLI 的 up 指令啟動開發容器

git clone https://github.com/microsoft/vscode-remote-try-rust
devcontainer up --workspace-folder <path-to-vscode-remote-try-rust>

這將從容器登錄檔 (container registry) 下載容器映像檔並啟動容器。您的 Rust 容器現在應該已經執行中。

[88 ms] dev-containers-cli 0.1.0.
[165 ms] Start: Run: docker build -f /home/node/vscode-remote-try-rust/.devcontainer/Dockerfile -t vsc-vscode-remote-try-rust-89420ad7399ba74f55921e49cc3ecfd2 --build-arg VARIANT=bullseye /home/node/vscode-remote-try-rust/.devcontainer
[+] Building 0.5s (5/5) FINISHED
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 38B                                        0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [internal] load metadata for mcr.microsoft.com/vscode/devcontainers/r  0.4s
 => CACHED [1/1] FROM mcr.microsoft.com/vscode/devcontainers/rust:1-bulls  0.0s
 => exporting to image                                                     0.0s
 => => exporting layers                                                    0.0s
 => => writing image sha256:39873ccb81e6fb613975e11e37438eee1d49c963a436d  0.0s
 => => naming to docker.io/library/vsc-vscode-remote-try-rust-89420ad7399  0.0s
[1640 ms] Start: Run: docker run --sig-proxy=false -a STDOUT -a STDERR --mount type=bind,source=/home/node/vscode-remote-try-rust,target=/workspaces/vscode-remote-try-rust -l devcontainer.local_folder=/home/node/vscode-remote-try-rust --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --entrypoint /bin/sh vsc-vscode-remote-try-rust-89420ad7399ba74f55921e49cc3ecfd2-uid -c echo Container started
Container started
{"outcome":"success","containerId":"f0a055ff056c1c1bb99cc09930efbf3a0437c54d9b4644695aa23c1d57b4bd11","remoteUser":"vscode","remoteWorkspaceFolder":"/workspaces/vscode-remote-try-rust"}

接著,您可以在此開發容器中執行指令

devcontainer exec --workspace-folder <path-to-vscode-remote-try-rust> cargo run

這將會編譯並執行 Rust 範例,並輸出結果

[33 ms] dev-containers-cli 0.1.0.
   Compiling hello_remote_world v0.1.0 (/workspaces/vscode-remote-try-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 1.06s
     Running `target/debug/hello_remote_world`
Hello, VS Code Dev Containers!
{"outcome":"success"}

上述步驟也提供在 CLI 儲存庫的 README 中。

自動化

如果您希望在 CI/CD 建置流程或測試自動化中使用 dev container CLI,可以在 devcontainers/ci 儲存庫中找到 GitHub Actions 和 Azure DevOps Tasks 的範例。

預先建置 (Pre-building)

devcontainer build 指令允許您按照與 Dev Containers 擴充功能或 GitHub Codespaces 相同的步驟,快速建置開發容器映像檔。當您想要使用 GitHub Actions 等 CI 或 DevOps 產品預先建置開發容器映像檔時,此功能特別有用。

build 接受包含 .devcontainer 資料夾或 .devcontainer.json 檔案的資料夾路徑。例如,devcontainer build --workspace-folder <my_repo> 將會為 my_repo 建置容器映像檔。

建置並發佈映像檔的範例

例如,您可能希望預先建置多個映像檔,然後在多個專案或儲存庫中重複使用。若要這麼做,請遵循以下步驟

  1. 建立一個原始碼儲存庫。

  2. 為每個您想要預先建置的映像檔建立開發容器設定,並根據需要進行自訂(包括 開發容器功能 (Features))。例如,參考此 devcontainer.json 檔案

    {
      "build": {
        "dockerfile": "Dockerfile"
      },
      "features": {
        "ghcr.io/devcontainers/features/docker-in-docker:1": {
          "version": "latest"
        }
      }
    }
    
  3. 使用 devcontainer build 指令建置映像檔,並將其 推送 (push) 到您的映像檔登錄檔。請參閱您的映像檔登錄檔文件(例如 Azure Container RegistryGitHub Container RegistryDocker Hub),以了解映像檔命名規則以及驗證等額外步驟。

    devcontainer build --workspace-folder <my_repo> --push true --image-name <my_image_name>:<optional_image_version>
    

避免使用 Docker 建置映像檔時產生的問題

由於 Dockerfile 和 Docker Compose 檔案可以在沒有 VS Code 或 devcontainer CLI 的情況下使用,您可能需要提醒使用者不要直接嘗試建置映像檔。您可以在 進階開發容器文件 中進一步了解。

範本與功能 (Templates and Features)

您可以使用 dev container CLI 來處理開發容器 範本 (Templates)功能 (Features)。當您建立並使用範本時,您可能希望將其發佈給他人使用,您可以在 開發容器規範 中進一步了解。

意見回饋

dev container CLI 與規範目前處於活躍開發階段,我們歡迎您的回饋,您可以透過 此 issue 提供,或是透過 devcontainers/cli 儲存庫建立新的 issue 與 pull requests。

後續步驟

© . This site is unofficial and not affiliated with Microsoft.