如何使用dockerfile将parameter passing给.net核心项目
我有一个.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"]
我的问题是,如何将parameter passing到项目中?
public static void Main(string[] args) { // how does `args` get populated? }
我也使用了docker-compse.yml设置的环境variables
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; } } }
您可以使用ENTRYPOINT
来设置命令, CMD
来设置默认选项。
例如,对于一个ASP.NET核心应用程序:
ENTRYPOINT ["dotnet", "app.dll"] CMD ["argument"]
如果你没有命令运行容器,它将在容器启动时执行这个命令:
dotnet app.dll argument
args
将会有一个条目“参数”。 但是你可以通过docker run
命令来覆盖CMD
定义:
docker run app arg1 arg2
一种方法是读取环境variables。 你不会修改你的Dockerfile来做到这一点。 相反,说你运行你的容器如下:
$ FOO="bar" $ docker run -e FOO=$FOO devlife/myapp
你现在需要弄清楚的是如何在你的dotnetcore应用程序中读取FOO环境variables。 我会尝试这里logging的方法: ASP.net核心读取环境variables