加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何使用dockerfile将参数传递给.net核心项目

发布时间:2020-12-16 03:41:19 所属栏目:安全 来源:网络整理
导读:我有一个.NET Core项目(使用visual studio并通过Visual Studio Tools for Docker添加了docker文件). 我的DockerFile看起来像这样: FROM microsoft/dotnet:1.0.1-coreARG source=.WORKDIR /appCOPY $source .ENTRYPOINT ["dotnet","MyApp.dll"]CMD ["arg1","

我有一个.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?
}

enter image description here

最佳答案
我使用了可以通过docker-compse.yml设置的环境变量

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读