❔ Get Console App args inside Docker container with Docker-compose

Hi, I have trouble with a .NET6 Console App and Docker with the use of args. If I launch the Console App without Docker in VS22 everything is okay, my args are retrieved, but when I use with Docker container inside a Docker-compose nothing happen... I tried to edit the Dockerfile to hardcode the value of the args, use the "command" part of docker-compose but nothing is working. Someone have a solution ? I would like to not use env variable to handle args. Thank you, Tom.
24 Replies
cap5lut
cap5lut2y ago
show how u provide the args and other relevant code.
Pobiega
Pobiega2y ago
show your dockerfile
backtrack5r3
backtrack5r3OP2y ago
Sorry, When running locally I used these launchProfiles :
"profiles": {
"Development": {
"commandName": "Project",
"commandLineArgs": "args1 args2 args3",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
},
"Docker": {
"commandName": "Docker",
"commandLineArgs": "args1 args2 args3"
}
}
"profiles": {
"Development": {
"commandName": "Project",
"commandLineArgs": "args1 args2 args3",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
},
"Docker": {
"commandName": "Docker",
"commandLineArgs": "args1 args2 args3"
}
}
Both profiles are working well. But when I try to use the Docker inside à Docker-compose I dont success to retrieve args to my console app inside container. I tried inside Docker-compose :
command : ["args1", "args2", "args3"]
command : ["args1", "args2", "args3"]
I tried inside Dockerfile :
CMD ["args1", "args2", "args3"]
CMD ["args1", "args2", "args3"]
Pobiega
Pobiega2y ago
Can you show the full dockerfile please? do you have an ENTRYPOINT in your dockerfile?
backtrack5r3
backtrack5r3OP2y ago
It's the default Dockerfile generated by visual studio Yes
Pobiega
Pobiega2y ago
just show it, please.
backtrack5r3
backtrack5r3OP2y ago
ENTRYPOINT ["dotnet", "MyDLL.dll"]
ENTRYPOINT ["dotnet", "MyDLL.dll"]
Pobiega
Pobiega2y ago
the entire file
backtrack5r3
backtrack5r3OP2y ago
okay i'm on it
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["ConsoleApp/Console.csproj", "Console/"]
COPY ["ConsoleCore/Core.csproj", "Core/"]
COPY ["ConsoleInfrastructure/Infrastructure.csproj", "Infrastructure/"]
COPY ["ConsoleMock/Mock.csproj", "Mock/"]
RUN dotnet restore "ConsoleApp/Console.csproj"
COPY . .
WORKDIR "/src/ConsoleApp"
RUN dotnet build "Console.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Console.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Console.dll"]
FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["ConsoleApp/Console.csproj", "Console/"]
COPY ["ConsoleCore/Core.csproj", "Core/"]
COPY ["ConsoleInfrastructure/Infrastructure.csproj", "Infrastructure/"]
COPY ["ConsoleMock/Mock.csproj", "Mock/"]
RUN dotnet restore "ConsoleApp/Console.csproj"
COPY . .
WORKDIR "/src/ConsoleApp"
RUN dotnet build "Console.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Console.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Console.dll"]
The weird things is that with launchProfiles and the Docker container launched alone the args are working well with the instruction :
"commandLineArgs": "args1 args2 args3",
"commandLineArgs": "args1 args2 args3",
But I cant figure out how to make the equivalent instruction for my docker-compose file
Pobiega
Pobiega2y ago
so what exactly are you trying to do set command like args via the dockerfile? set it via the compose file?
backtrack5r3
backtrack5r3OP2y ago
i am trying to pass "args1", "args2", "args3" while running my console app from a docker-compose via the compose file i would like to not rebuild my dockerfile each time I change the args value, just recreate the container with the right args maybe I am wrong, but the commandLineArgs seems to create the container with passing args ex :
docker run myfile args1 args2 args3
docker run myfile args1 args2 args3
and I dont need to rebuild my dockerfile each time ?
Pobiega
Pobiega2y ago
commandLineArgs, in what file?
backtrack5r3
backtrack5r3OP2y ago
launchSettings.json it's basically a launch option for the project
Pobiega
Pobiega2y ago
well yes, but that file is part of your image. container
backtrack5r3
backtrack5r3OP2y ago
so i am wrong and even running the Docker container alone, I need to rebuild the Dockerfile If I change the value inside my launchSettings.json ?
Pobiega
Pobiega2y ago
You'd need to rebuild the docker image if you change the launchSettings file, yes so I don't recommend setting it that way
backtrack5r3
backtrack5r3OP2y ago
Do you have a better way to advise me ?
Pobiega
Pobiega2y ago
hm, this is very weird... my initial thought was to use CMD in the dockerfile and command in compose to override it, but I can't even get it to work with ENTRYPOINT ENTRYPOINT ["dotnet", "EchoArgs.dll", "asd", "asd"] doesnt seem to work, while running that exact command myself works fine
backtrack5r3
backtrack5r3OP2y ago
it seems like vs22 is doing more thing than we thought
Pobiega
Pobiega2y ago
I'm not using dockertool thou, and Im very confused as to why this doesnt work
backtrack5r3
backtrack5r3OP2y ago
When I try to use this in my Docker-compose :
command: "-- args1 args2 args3"
command: "-- args1 args2 args3"
According to ms doc https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-run#options I have these logs in my container :
==> /dev/null <==
tail: cannot open 'args1' for reading: No such file or directory
tail: cannot open 'args2' for reading: No such file or directory
tail: cannot open 'args3' for reading: No such file or directory
==> /dev/null <==
tail: cannot open 'args1' for reading: No such file or directory
tail: cannot open 'args2' for reading: No such file or directory
tail: cannot open 'args3' for reading: No such file or directory
very weird....
dotnet run command - .NET CLI
The dotnet run command provides a convenient option to run your application from the source code.
Pobiega
Pobiega2y ago
dotnet run is not invoked for your app thou just to be clear
string[] arguments = Environment.GetCommandLineArgs();
Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
Console.Write($"Showing {args.Length} args: ");
Console.WriteLine(string.Join(',', args));
string[] arguments = Environment.GetCommandLineArgs();
Console.WriteLine("GetCommandLineArgs: {0}", String.Join(", ", arguments));
Console.Write($"Showing {args.Length} args: ");
Console.WriteLine(string.Join(',', args));
ENTRYPOINT ["dotnet", "EchoArgs.dll", "--", "asd", "asd"]
CMD [ "Hello", "World" ]
ENTRYPOINT ["dotnet", "EchoArgs.dll", "--", "asd", "asd"]
CMD [ "Hello", "World" ]
GetCommandLineArgs: /app/bin/Debug/net7.0/EchoArgs.dll
Showing 0 args:
GetCommandLineArgs: /app/bin/Debug/net7.0/EchoArgs.dll
Showing 0 args:
it seems to ignore whatever is after the dll in the entrypoint, and the cmd is not applied either okay, its related to how Rider runs it, and possibly dockertool if I build that same dockerfile myself using the cli, it works
PS C:\code\temp\EchoArgs> docker run echoasd
GetCommandLineArgs: /app/EchoArgs.dll, asd, asd, Hello, World
Showing 4 args: asd,asd,Hello,World
PS C:\code\temp\EchoArgs> docker build -t echoasd -f .\EchoArgs\Dockerfile .
PS C:\code\temp\EchoArgs> docker run echoasd
GetCommandLineArgs: /app/EchoArgs.dll, asd, asd, Hello, World
Showing 4 args: asd,asd,Hello,World
PS C:\code\temp\EchoArgs> docker build -t echoasd -f .\EchoArgs\Dockerfile .
that... infuriates me, but at least means I'm not going insane so, build the image yourself and it should work just fine
PS C:\code\temp\EchoArgs> docker run echoasd I can write stuff here and it works
GetCommandLineArgs: /app/EchoArgs.dll, I, can, write, stuff, here, and, it, works
Showing 8 args: I,can,write,stuff,here,and,it,works
PS C:\code\temp\EchoArgs> docker run echoasd I can write stuff here and it works
GetCommandLineArgs: /app/EchoArgs.dll, I, can, write, stuff, here, and, it, works
Showing 8 args: I,can,write,stuff,here,and,it,works
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server