我有一个.NET Core项目(使用visual studio并通过Visual Studio Tools for Docker添加了docker文件).
我的DockerFile看起来像这样:
FROM microsoft/dotnet:1.0.1-core
ARG source=.
WORKDIR /app
COPY $source .
ENTRYPOINT ["dotnet","MyApp.dll"]
CMD ["arg1","arg2"]
我的问题是,如何将参数传递给项目?
public static void Main(string[] args)
{
// how does `args` get populated?
}
最佳答案
我使用了可以通过docker-compse.yml设置的环境变量
原文链接:https://www.f2er.com/docker/435942.htmlpublic 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;
}
}
}