【问题标题】:How reference volume and environment variable with docker in Api net core在 Api net core 中如何使用 docker 引用卷和环境变量
【发布时间】:2021-03-29 09:41:59
【问题描述】:

实际上在带有 net core 的 mi Apigateway 中,我有下一个文件夹结构:

在 Program.cs 中我需要参考文件配置,例如 _pathConfigOcelot:

public static void Main(string[] args)
    {
        string _pathConfigOcelot = Environment.GetEnvironmentVariable("OcelotConfigPath");
        CreateHostBuilder(args, _pathConfigOcelot).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args, string pathConfigOcelot) =>
        Host.CreateDefaultBuilder(args)
           .ConfigureAppConfiguration(
                      ic => ic.AddJsonFile(Path.Combine(pathConfigOcelot,
                                                        "configuration.json")))
            .ConfigureWebHostDefaults(webBuilder => 
            {
                webBuilder.UseStartup<Startup>();
            });

其实我使用 docker-compose 来使用volumes:

services: 
  tresfilos.webapigateway:
  image: ${DOCKER_REGISTRY-}tresfilosapigateway
  build: 
    context: .
    dockerfile: tresfilos.ApiGateway/ApiGw-Base/Dockerfile
  environment: 
   - ASPNETCORE_ENVIRONMENT=Development
   - IdentityUrl=http://identity-api
  ports:
   - "7000:80"
   - "7001:443"
  volumes:
   - ./tresfilos.ApiGateway/Web.Bff:/app/configuration

我不知道如何在项目中使用 docker volume like reference for configuration.js,有什么想法吗?

【问题讨论】:

    标签: docker asp.net-core environment-variables volumes


    【解决方案1】:

    我读到这个./tresfilos.ApiGateway/Web.Bff:/app/configuration,因为/app/configuration 是我的应用程序中./tresfilos.ApiGateway/Web.Bff 的别名。

    所以我会说您将文件引用为/app/configuration/configuration.json

    更新:

    在 Web.Bff 之后添加 / 可能会有所帮助。

    【讨论】:

    • 仅用于测试,我直接在代码中放入 docker 路径并出现此错误:“未找到配置文件 './tresfilos.ApiGateway/Web.Bff\configuration.json'可选的。”.......................然后,如果我输入代码/app/configuration,这不是在容器中产生错误??
    • 顺便说一句,我注意到您在: 之前缺少/
    • 我不明白,是不是因为改变了路径中的\ /? ./tresfilos.ApiGateway/Web.Bff\configuration.json
    • ./tresfilos.ApiGateway/Web.Bff/:/app/configuration 而不是 ./tresfilos.ApiGateway/Web.Bff:/app/configuration 然后将文件引用为 /app/configuration/configuration.json
    • 我使用 Path.Combine 然后该方法包括“\”之间。但首先(用于测试)我试图引用物理路径,例如: Path.Combine("./tresfilos.ApiGateway/Web.Bff", "configuration.json")
    猜你喜欢
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2021-02-15
    • 1970-01-01
    相关资源
    最近更新 更多