此參考提供從 Docker Compose YAML 語法到對等 Aspire C# API 呼叫的系統對應。 將現有的 Docker Compose 檔案 Aspire 轉換成應用程式主機組態時,請使用這些數據表作為快速參考。 每個區段涵蓋容器協調流程的特定層面,從基本服務定義到進階網路和資源管理。
服務定義
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
services: |
var builder = DistributedApplication.CreateBuilder(args) |
用於新增和表示資源的根應用程式產生器。 |
service_name: |
builder.Add*("service_name") |
服務名稱會變成資源名稱。 |
相關連結:
映像和組建
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
image: nginx:latest |
builder.AddContainer("name", "nginx:latest") |
直接影像參考。 |
build: . |
builder.AddDockerfile("name", ".") |
從 Dockerfile建置。 |
build: ./path |
builder.AddDockerfile("name", "./path") |
從特定路徑建置。 |
build.context: ./app |
builder.AddDockerfile("name", "./app") |
建置內容。 |
build.dockerfile: Custom.dockerfile |
builder.Add*("name").WithDockerfile("Custom.dockerfile") |
自定義 Dockerfile 名稱。 |
相關連結:
.NET 專案
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
build: ./MyApi ( 適用於 .NET ) |
builder.AddProject<Projects.MyApi>("myapi") |
直接 .NET 項目參考。 |
相關連結:
埠對應
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
ports: ["8080:80"] |
.WithHttpEndpoint(port: 8080, targetPort: 80) |
HTTP 端點對應。 埠是選擇性的;如果省略,則會使用動態埠。 |
ports: ["443:443"] |
.WithHttpsEndpoint(port: 443, targetPort: 443) |
HTTPS 端點對應。 埠是選擇性的;如果省略,則會使用動態埠。 |
expose: ["8080"] |
.WithEndpoint(port: 8080) |
內部埠暴露。 埠是選擇性的;如果省略,則會使用動態埠。 |
相關連結:
環境變數
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
environment: KEY=value |
.WithEnvironment("KEY", "value") |
靜態環境變數。 |
environment: KEY=${HOST_VAR} |
.WithEnvironment(context => context.EnvironmentVariables["KEY"] = hostVar) |
具有回呼內容的環境變數。 |
env_file: .env |
不支援 | 環境檔案 (自定義實作)。 |
相關連結:
磁碟區和記憶體
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
volumes: ["data:/app/data"] |
.WithVolume("data", "/app/data") |
具名磁碟區。 |
volumes: ["./host:/container"] |
.WithBindMount("./host", "/container") |
系結掛接。 |
volumes: ["./config:/app:ro"] |
.WithBindMount("./config", "/app", isReadOnly: true) |
唯讀系結掛接。 |
相關連結:
相依性和排序
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
depends_on: [db] |
.WithReference(db) |
具有連線的服務相依性。 |
depends_on: db: condition: service_started |
.WaitFor(db) |
等候服務啟動。 |
depends_on: db: condition: service_healthy |
.WaitForCompletion(db) |
等候健康情況檢查。 |
相關連結:
網路
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
networks: [backend] |
自動 | Aspire 自動處理網路。 |
| 自定義網路 | 不需要 | 服務探索會處理服務間通訊。 |
相關連結:
資源限制
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
deploy.resources.limits.memory: 512m |
不支援 | 中不支援 Aspire資源限制。 |
deploy.resources.limits.cpus: 0.5 |
不支援 | 中不支援 Aspire資源限制。 |
相關連結:
健康檢查
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
healthcheck.test: ["CMD", "curl", "http://localhost/health"] |
內建整合。 | Aspire 整合包括性能檢查。 |
healthcheck.interval: 30s |
可在整合中設定。 | 健康情況檢查組態會因資源類型而異。 |
相關連結:
重新啟動原則
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
restart: unless-stopped |
不支援 | 中不支援 Aspire重新啟動原則。 |
restart: always |
不支援 | 中不支援 Aspire重新啟動原則。 |
restart: no |
預設 | 沒有重新啟動原則。 |
相關連結:
森林伐木業
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
logging.driver: json-file |
內置 | Aspire 提供整合式日誌記錄。 |
logging.options.max-size: 10m |
儀錶板設定 | 通過儀表板管理 Aspire 。 |
相關連結:
資料庫服務
| Docker 組成 | Aspire | 註釋 |
|---|---|---|
image: postgres:15 |
builder.AddPostgres("name") |
PostgreSQL 使用自動設定。 |
image: mysql:8 |
builder.AddMySql("name") |
MySQL 使用自動設定。 |
image: redis:7 |
builder.AddRedis("name") |
Redis 使用自動設定。 |
image: mongo:latest |
builder.AddMongoDB("name") |
MongoDB 使用自動設定。 |
相關連結: