Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 417
devlife Avatar asked Oct 26 '16 21:10

devlife


People also ask

How do I pass a variable in Dockerfile?

Passing environment variables using the --env or -e flag The --name option is used to give a name to the container. The --env option is used to pass the environment variable called ENVVARIABLE with a value foobar to the container.

Can Dockerfile take arguments?

You can use the ARG command inside a Dockerfile to define the name of a parameter and its default value. This default value can also be overridden using a simple option with the Docker build command.


1 Answers

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
like image 123
Elton Stoneman Avatar answered Sep 23 '22 21:09

Elton Stoneman