我正在使用 Docker Compose 来运行 .NET C# 控制台应用程序。我需要在应用程序启动时将命令行参数传递给它,但我在日志中收到以下错误:
2025-01-15 13:07:47 System.ArgumentException: Required input args was empty. (Parameter 'args')
2025-01-15 13:07:47 at Ardalis.GuardClauses.GuardClauseExtensions.NullOrEmpty[T](IGuardClause guardClause, IEnumerable`1 input, String parameterName, String message, Func`1 exceptionCreator)
2025-01-15 13:07:47 at Personnel.Infrastructure.Migrator.Program.Main(String[] args) in C:\Users\user\RiderProjects\staffpro.learn.redkozubov\src\Services\Personnel\Infrastructure\Personnel.Infrastructure.Migrator\Program.cs:line 21
这是我的 Personnel_migrator 容器的 Docker Compose:
personnel_migrator:
container_name: personnel_migrator
build:
context: .
dockerfile: src/Services/Personnel/Infrastructure/Personnel.Infrastructure.Migrator/Dockerfile
depends_on:
- personnel_db
networks:
- personnel_db
ports:
- "5001:5001"
environment:
- CONNECTION_STRING=${CONNECTION_STRING}
command: dotnet Personnel.Infrastructure.Migrator.dll --${CONNECTION_STRING}
我的 .env 文件包含:
CONNECTION_STRING="ConnectionString"
应用程序期望传递参数,但应用程序启动时 args 数组显示为空。如何在 Docker Compose 中正确地将命令行参数传递给 .NET 应用程序?
更新了 Docker Compose 服务
personnel_migrator:
container_name: personnel_migrator
build:
context: .
dockerfile: src/Services/Personnel/Infrastructure/Personnel.Infrastructure.Migrator/Dockerfile
depends_on:
- personnel_db
networks:
- personnel_db
ports:
- "5001:5001"
environment:
- CONNECTION_STRING=${CONNECTION_STRING}
command: ["dotnet", "Personnel.Infrastructure.Migrator.dll", "${CONNECTION_STRING}"]