【问题标题】:Multiple docker-compose sharing network with a not yet known host for nginx多个 docker-compose 共享网络与一个未知的 nginx 主机
【发布时间】:2018-01-04 16:34:22
【问题描述】:

我使用多个 docker-compose 文件:

  • 一个用于在同一网络上运行:postgres 和 nginx => 这个容器集合应该一直在运行

  • 每个 asp 核心网站一个(每个在特定端口上) => 这个容器是通过 CI/CD 管道 (VSTS) 更新的

因为 Nginx 在定义上游时需要知道主机名,如果 asp 核心容器没有运行,那么它的主机名是未知的,那么 nginx 在 docker-compose up 命令上会抛出错误:

nginx            | 2018/01/04 15:59:17 [emerg] 1#1: host not found in upstream 
"webportalstage:5001" in /etc/nginx/nginx.conf:9
 nginx            | nginx: [emerg] host not found in upstream 
 "webportalstage:5001" in /etc/nginx/nginx.conf:9
 nginx exited with code 1

显然,如果之前运行过 asp 核心容器,那么 nginx 知道主机名 webportalstage 并且一切正常。但开始的顺序不是我所期望的。

有什么解决方案可以在上游使用未知的主机名启动 nginx 吗?

这是我的 nginx.conf 文件:

worker_processes 4;

 events { worker_connections 1024; }

 http {
sendfile on;

upstream webportalstage {
   server webportalstage:5001;
}

server {
    listen 80;

    location / {
        proxy_pass         http://webportalstage;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }
}  
}

还有两个 docker-compose 文件: Nginx + Postgres:

version: "3"

services:
  proxy:
    image: myPrivateRepo:latest
    ports:
      - "80:80"
    container_name: nginx
    networks:
      aspcore:
        aliases:
          - nginx

  postgres:
    image: postgres:latest
    environment:
      - POSTGRES_PASSWORD=myPWD
      - POSTGRES_USER=postgres
    ports:
      - "5432:5432"
    container_name: postgres
    networks:
      aspcore:
        aliases:
          - postgres

networks:
  aspcore:
driver: bridge

我的一个asp核心网站:

version: "3"

services:

  webportal:
    image: myPrivateRepo:latest
    environment:
      - ASPNETCORE_ENVIRONMENT=Staging
    container_name: webportal
    networks:
      common_aspcore:
        aliases:
          - webportal

networks:
  common_aspcore:
    external: true

【问题讨论】:

  • aspcore 在 nginx docker-compose 与 common_aspcore 在核心 docker-compose 配置中?
  • 是的,这很特别,但是当您使用 docker-compose 创建网络时,真实的网络名称是:directoryName_nameofyourservice 所以我在 aspcore 网站 docker-compose 中使用他的真实姓名将网络称为外部网络。我只尝试使用 aspcore 名称网络,但出现网络不存在之类的错误

标签: docker nginx asp.net-core docker-compose


【解决方案1】:

好吧,我在类似的情况下使用以下 hack:

location / {
    set $docker_host "webportalstage";
    proxy_pass       http://$docker_host:5001;
    ...
}

我不确定它是否适用于上游,可能应该。

我知道,这不是最好的解决方案,但我没有找到更好的解决方案。

【讨论】:

  • Nginx 在 doinf 的情况下不会崩溃吗?我的意思是,即使“webportalstage”存储到 docker 变量($docker_host)中,在 NGINX 启动时解析 http://$docker_host:5001 行时,它应该会导致与我的错误相同的行为没有?
  • @Cladoo 不,不是。我在一些项目中使用了这种解决方法。
  • 太好了,我会试试你的解决方案
【解决方案2】:

我终于在我的 nginx+postgres docker-compose.yml 文件中使用了 extra_host 功能来定义静态 IP:

    extra_hosts:
    webportalstage: 10.5.0.20

并为我的 asp 核心 docker-compose 文件设置相同的静态 IP。 它有效,但不像我想要的那样通用

【讨论】:

    猜你喜欢
    • 2020-08-09
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多