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

devlife picture devlife · Oct 26, 2016 · Viewed 8.2k times · Source

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

Answer

Elton Stoneman picture Elton Stoneman · Oct 27, 2016

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