開發容器Visual Studio Code延伸模組提供一種方式,讓開發小組在預先設定所有相依性的容器化環境中進行開發。 使用 .NET Aspire 9.1 時,新增了邏輯,可藉由自動設定埠轉送,在開發容器環境中更妥善地支援使用 .NET Aspire 。
在 9.1 之前 .NET Aspire ,可以在開發容器內使用 .NET Aspire ,不過需要更多手動設定。
開發容器與 GitHub Codespaces
在中使用 Visual Studio Code 開發容器類似於使用 GitHub Codespaces。 在發佈.NET Aspire9.1 版本時,Visual Studio Code和GitHub Codespaces 中的開發容器支援已得到增強。 雖然體驗很類似,但有一些差異。 如需了解關於將 .NET Aspire 用於 GitHub Codespaces 的詳細資訊,請參閱 .NET Aspire 和 GitHub Codespaces。
使用範本存放庫的快速入門
若要在 中 Visual Studio Code設定開發容器,請使用存放庫中的 _.devcontainer/devcontainer.json 檔案。 最簡單的開始使用方式是從 範本存放庫建立新的存放庫。 請考慮下列步驟:
使用我們的範本建立新的存放庫。
提供詳細數據並選取 [ 建立存放庫] 之後,就會建立存放庫並顯示在 中 GitHub。
使用下列命令,將存放庫複製到本機開發人員工作站:
git clone https://github.com/<org>/<username>/<repository>
在Visual Studio Code中開啟存放庫。 過了一會兒,Visual Studio Code 偵測到 .devcontainer/devcontainer.json 檔案並提示是否要在容器內開啟倉儲。 選取最適合您工作流程的選項。
幾分鐘后,檔案清單就會變成可見,且開發容器的本機組建將會完成。
在 [Ctrl+Shift+`] 中Visual Studio Code開啟新的終端機視窗,並使用
dotnet
命令行建立新的.NET.NET Aspire專案。dotnet new aspire-starter -n HelloAspire
幾分鐘后,專案將會建立並還原初始相依性。
在編輯器中開啟 ProjectName.AppHost/Program.cs 檔案,然後選取編輯器視窗右上角的執行按鈕。
Visual Studio Code 建置並啟動 .NET Aspire 應用程式主機,並自動開啟 .NET Aspire 儀錶板。 由於容器中裝載的端點首次使用自我簽署憑證,因此您在首次存取特定開發容器的端點時,會遇到憑證錯誤。
憑證錯誤是預期的。 確認要求的 URL 對應至開發容器中的儀錶板後,您可以忽略此警告。
.NET Aspire 會自動設定轉發埠,以便在儀錶板中的 .NET Aspire 端點上選取時,它們會透過隧道傳輸至開發容器內的進程和嵌套容器。
將變更提交至 GitHub 儲存庫
成功建立 .NET.NET Aspire 專案,並確認其已啟動且您能夠存取儀錶板之後,建議將變更提交至存放庫。
手動設定 devcontainer.json
上述逐步解說示範如何使用開發容器範本建立開發容器 .NET.NET Aspire 的簡化程式。 如果您已經有現有的存放庫,且想要搭配 .NET.NET Aspire使用開發容器功能,請將 devcontainer.json 檔案新增至存放庫內的 .devcontainer 資料夾:
└───📂 .devcontainer
└─── devcontainer.json
範本存放庫包含一份可做為起點的devcontainer.json 檔案複本,這應該就足夠了.NET.NET Aspire。 下列 JSON 代表範本中最新版的 .devcontainer/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/dotnet
{
"name": ".NET Aspire",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/dotnet:9.0-bookworm",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"ghcr.io/devcontainers/features/powershell:1": {},
},
"hostRequirements": {
"cpus": 8,
"memory": "32gb",
"storage": "64gb"
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [5000, 5001],
// "portsAttributes": {
// "5001": {
// "protocol": "https"
// }
// }
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "dotnet restore",
"onCreateCommand": "dotnet new install Aspire.ProjectTemplates::9.3.1 --force",
"postStartCommand": "dotnet dev-certs https --trust",
"customizations": {
"vscode": {
"extensions": [
"ms-dotnettools.csdevkit",
"GitHub.copilot-chat",
"GitHub.copilot"
]
}
}
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
開發容器使用場景
基本 .NET.NET Aspire 開發容器範本適用於簡單案例,但視您的特定需求而定,您可能需要額外的設定。 下列各節提供各種常見案例的範例。
僅限無狀態 .NET 應用程式
對於只使用.NET沒有外部容器或複雜協調流程之專案資源的簡單.NET Aspire專案,您可以使用最少的開發容器組態:
{
"name": ".NET Aspire - Simple",
"image": "mcr.microsoft.com/devcontainers/dotnet:9.0-bookworm",
"onCreateCommand": "dotnet new install Aspire.ProjectTemplates --force",
"postStartCommand": "dotnet dev-certs https --trust",
"customizations": {
"vscode": {
"extensions": [
"ms-dotnettools.csdevkit"
]
}
}
}
此最小設定適用於僅協調無外部相依性服務的 .NET Aspire 應用程式。
新增 Node.js 資源
如果您的 .NET Aspire 應用程式包含 Node.js 資源,請將 Node.js 此功能新增至您的開發容器:
{
"name": ".NET Aspire with Node.js",
"image": "mcr.microsoft.com/devcontainers/dotnet:9.0-bookworm",
"features": {
"ghcr.io/devcontainers/features/node:1": {
"version": "lts"
}
},
"onCreateCommand": "dotnet new install Aspire.ProjectTemplates --force",
"postStartCommand": "dotnet dev-certs https --trust",
"customizations": {
"vscode": {
"extensions": [
"ms-dotnettools.csdevkit",
"ms-vscode.vscode-typescript-next"
]
}
}
}
此組態會在相同的容器環境中提供 .NET 和 Node.js 開發功能。
使用 Docker 的容器编排-in-Docker
.NET Aspire當您的應用程式協調容器資源時,您需要 Docker-in-Docker(DinD)支援。 以下是基本組態:
{
"name": ".NET Aspire with Containers",
"image": "mcr.microsoft.com/devcontainers/dotnet:9.0-bookworm",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"version": "latest",
"enableNonRootDocker": true,
"moby": true
}
},
"hostRequirements": {
"cpus": 4,
"memory": "16gb",
"storage": "32gb"
},
"onCreateCommand": "dotnet new install Aspire.ProjectTemplates --force",
"postStartCommand": "dotnet dev-certs https --trust",
"customizations": {
"vscode": {
"extensions": [
"ms-dotnettools.csdevkit",
"ms-azuretools.vscode-docker"
]
}
}
}
進階容器網路功能
如果您在容器之間遇到網路問題,或需要 IPv6 支援,您可以新增其他網路設定:
{
"name": ".NET Aspire with Advanced Networking",
"image": "mcr.microsoft.com/devcontainers/dotnet:9.0-bookworm",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"version": "latest",
"enableNonRootDocker": true,
"moby": true
}
},
"runArgs": [
"--sysctl",
"net.ipv6.conf.all.disable_ipv6=0",
"--sysctl",
"net.ipv6.conf.default.forwarding=1",
"--sysctl",
"net.ipv6.conf.all.forwarding=1"
],
"hostRequirements": {
"cpus": 8,
"memory": "32gb",
"storage": "64gb"
},
"onCreateCommand": "dotnet new install Aspire.ProjectTemplates --force",
"postStartCommand": "dotnet dev-certs https --trust",
"customizations": {
"vscode": {
"extensions": [
"ms-dotnettools.csdevkit",
"ms-azuretools.vscode-docker"
]
}
}
}
這很重要
Docker-in-Docker 考慮:
- Docker-in-Docker 需要較高的資源配置,包括增加 CPU、記憶體和儲存裝置。
- 上述進階網路設定包含複雜容器對容器通訊案例可能需要的IPv6轉送設定。
- 此設定適用於 Docker 桌面,但可能會有 Rancher Desktop 的限制。
- 容器之間的網路連線可能需要額外的設定,視您的特定使用案例而定。
Dapr 整合範例
針對 .NET Aspire 與 Dapr整合的應用程式,您可以在開發容器中設定 Dapr 元件。 如需詳細資訊,請參閱 .NET AspireDapr 整合。
基本 Dapr 設定
{
"name": ".NET Aspire with Dapr",
"image": "mcr.microsoft.com/devcontainers/dotnet:9.0-bookworm",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"enableNonRootDocker": true
},
"ghcr.io/dapr/cli/dapr-cli:0": {}
},
"onCreateCommand": "dotnet new install Aspire.ProjectTemplates --force",
"postCreateCommand": "dotnet dev-certs https --trust && dapr init",
"customizations": {
"vscode": {
"extensions": [
"ms-dotnettools.csdevkit",
"ms-azuretools.vscode-dapr"
]
}
}
}
Dapr 具有外部後端
針對使用外部後端的較複雜 Dapr 案例(Redis,PostgreSQL),您可以使用 Docker Compose:
{
"name": ".NET Aspire with Dapr and Backends",
"image": "mcr.microsoft.com/devcontainers/dotnet:9.0-bookworm",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {
"enableNonRootDocker": true
},
"ghcr.io/dapr/cli/dapr-cli:0": {}
},
"runArgs": [
"--sysctl",
"net.ipv6.conf.all.disable_ipv6=0"
],
"onCreateCommand": "dotnet new install Aspire.ProjectTemplates --force",
"postCreateCommand": [
"dotnet dev-certs https --trust",
"docker compose up -d",
"dapr init"
],
"customizations": {
"vscode": {
"extensions": [
"ms-dotnettools.csdevkit",
"ms-azuretools.vscode-dapr",
"ms-azuretools.vscode-docker"
]
}
}
}
一般考量
搭配 .NET.NET Aspire使用開發容器時,請記住下列考慮:
資源需求
- 基本 .NET 應用程式:標準開發容器資源就足以用於簡單案例。
- 容器協調流程:建議至少 8 個 CPU、32GB 記憶體和 64GB 記憶體。
- 涉及 Dapr/Kubernetes 的複雜情境:建議較高的資源配置以獲得最佳效能。
網路
- 容器對容器通訊可能需要IPv6設定。
- 埠轉送會自動由 .NET.NET Aspire 9.1和更新版本處理。
- 外部服務連線取決於容器運行時間組態。
效能
- Docker-in-Docker 情境相較於原生 Docker,會產生效能負擔。
- 考慮在生產工作流程中使用 Docker 於 Docker 之外 (DooD)。
- 本機開發和部署情境可能需要不同的設定。
安全性
- 使用 Docker-in-Docker 時,開發容器會以提升的權限執行。
- 在大部分情況下,SSL 憑證信任會自動處理。
- 考慮在雲端環境中公開埠時的安全性影響。