Search code examples
dockerdockerfiledocker-for-windowsdocker-desktop

How to pass parameters to a .NET core project with dockerfile


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


Solution

  • 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;
            }
        }
    }