I’m working with Docker Compose to run a .NET C# console application. I need to pass command-line arguments to my application when it starts, but I’m getting the following error in the logs:
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:UsersuserRiderProjectsstaffpro.learn.redkozubovsrcServicesPersonnelInfrastructurePersonnel.Infrastructure.MigratorProgram.cs:line 21
Here’s my Docker Compose for the personnel_migrator container:
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}
My .env file contains:
CONNECTION_STRING="ConnectionString"
The application expects arguments to be passed, but the args array appears empty when the application starts. How can I correctly pass the command-line arguments to the .NET application within Docker Compose?
Here’s my Program.cs code
private static readonly Migrator Migrator = new();
internal static void Main(string[] args)
{
try
{
Guard.Against.NullOrEmpty(args);
Migrator.Migrate(args);
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
}
Here’s docker file:
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
USER $APP_UID
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["src/Services/Personnel/Infrastructure/Personnel.Infrastructure.Migrator/Personnel.Infrastructure.Migrator.csproj", "src/Services/Personnel/Infrastructure/Personnel.Infrastructure.Migrator/"]
COPY ["src/Services/Personnel/Infrastructure/Personnel.Infrastructure.Data/Personnel.Infrastructure.Data.csproj", "src/Services/Personnel/Infrastructure/Personnel.Infrastructure.Data/"]
COPY ["src/Services/Personnel/Domain/Personnel.Domain/Personnel.Domain.csproj", "src/Services/Personnel/Domain/Personnel.Domain/"]
RUN dotnet restore "src/Services/Personnel/Infrastructure/Personnel.Infrastructure.Migrator/Personnel.Infrastructure.Migrator.csproj"
COPY . .
WORKDIR "/src/src/Services/Personnel/Infrastructure/Personnel.Infrastructure.Migrator"
RUN dotnet build "Personnel.Infrastructure.Migrator.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "Personnel.Infrastructure.Migrator.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
2
Answers
Updated Docker Compose Service
I’m unable to reproduce your issue. With the following 3 files, the program prints ‘–ConnectionString’ when run with
docker compose up
.Dockerfile:
docker-compose.yml:
.env:
Since that works and yours doesn’t, I suspect that the cause of the issue is located outside of what you’ve shown us.