22

I've got a .NET Core project (using visual studio and adding the docker files via the Visual Studio Tools for Docker).

My DockerFile looks like this:

FROM microsoft/dotnet:1.0.1-core
ARG source=.
WORKDIR /app
COPY $source .
ENTRYPOINT ["dotnet", "MyApp.dll"]
CMD ["arg1", "arg2"]

My question is, how do I pass parameters into the project?

public static void Main(string[] args)
{
    // how does `args` get populated?
}

enter image description here

veben
  • 19,637
  • 14
  • 60
  • 80
devlife
  • 15,275
  • 27
  • 77
  • 131

5 Answers5

11

You can do this with a combination of ENTRYPOINT to set the command, and CMD to set default options.

Example, for an ASP.NET Core app:

ENTRYPOINT ["dotnet", "app.dll"]
CMD ["argument"]

If you run the container with no command, it will execute this command when the container starts:

dotnet app.dll argument

And the args array will have one entry, "argument". But you can pass a command o docker run to override the CMD definition:

docker run app arg1 arg2
Elton Stoneman
  • 17,906
  • 5
  • 47
  • 44
6

I used environment variables which can be set by docker-compse.yml too

public static class EnvironmentHelper
{
    public const string EnvironmentArguments = "DOTNETCORE_ARGUMENTS";
    private static string[] _arguments;
    public static string[] Arguments
    {
        get
        {
            bool argumentsExist = _arguments != null && _arguments.Any();
            if (!argumentsExist)
            {
                IDictionary environmentVariables = Environment.GetEnvironmentVariables();
                if (!environmentVariables.Contains(EnvironmentArguments))
                {
                    throw new Exception("Environment Arguments do not exist");
                }
                var argumentsHolder = environmentVariables[EnvironmentArguments] as string;
                const char argumentSeparator = ' ';
                _arguments = argumentsHolder?.Split(argumentSeparator);
            }
            return _arguments;
        }
    }
}
cilerler
  • 9,010
  • 10
  • 56
  • 91
  • any chance you can help with how to use this inside the Main and what will need to be added in the Dockerfile TIA? – user1715559 Dec 29 '20 at 19:11
2

The answer is already given by @Elton Stoneman, but might help someone else, as that approach did not work with a docker environment variable.

I was looking for way to consume argument from environment variable instead of pass it to docker run command. So here is my approch

ENV ARG1=default
ENV ARG2=default
CMD ["sh", "-c", "dotnet /app/StockPods.Core.Listener.dll $ARG1 $ARG2" ]

So it will be easy to run this on different platform without worrying to pass anything to docker run command.

To override this behaviour

docker run -it --rm -e ARG1=newvalue -e ARG2=newvalue my_docker_image
Adiii
  • 54,482
  • 7
  • 145
  • 148
1

One approach would be to read in environment variables. You would not modify your Dockerfile to do this. Rather, say you run up your container as follows:

$ FOO="bar"
$ docker run -e FOO=$FOO devlife/myapp

All you now need to figure out is how to read the FOO environment variable in your dotnetcore application. I would try the approach documented here: ASP .NET Core read environment variables

Community
  • 1
  • 1
Nico de Wet
  • 319
  • 2
  • 12
0

VS Tools for Docker appear to silently override CMD and ENTRYPOINT arguments. Please see How to pass command line when debugging docker-compose project in Visual Studio? and https://github.com/Microsoft/DockerTools/issues/75

Gazi
  • 21
  • 4