skip to Main Content

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


  1. Updated Docker Compose Service

    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}"]
    
    Login or Signup to reply.
  2. I’m unable to reproduce your issue. With the following 3 files, the program prints ‘–ConnectionString’ when run with docker compose up.

    Dockerfile:

    FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
    WORKDIR /src
    RUN dotnet new console --use-program-main -o . -n MyApp
    RUN cat <<EOF > Program.cs
    namespace MyApp;
    
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var arg in args)
            {
                Console.WriteLine(arg);
            }
        }
    }
    EOF
    RUN dotnet publish -o /app
    
    FROM mcr.microsoft.com/dotnet/runtime:8.0
    WORKDIR /app
    COPY --from=build /app .
    

    docker-compose.yml:

    services:
        personnel_migrator:
            container_name: personnel_migrator
            build:
                context: .
            command: dotnet MyApp.dll --${CONNECTION_STRING}
    

    .env:

    CONNECTION_STRING="ConnectionString"
    

    Since that works and yours doesn’t, I suspect that the cause of the issue is located outside of what you’ve shown us.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search