NGINX 如何解析 Docker DNS 主机名?

How does NGINX resolve the Docker DNS hostnames?

我想更好地理解这个过程,但我在这里找不到解释。
我有一个 app 应用程序,它在 / 上将容器 ID 打印为 http 响应,类似于 jwilder/whoami 图像。
我在撰写时使用 --scale app=2 将此应用程序与 nginx 进行负载平衡。它使用 Nginx 的默认 Round-Robin 方法在 2(或 N)个应用程序之间正确循环。
Docker 和 Nginx 如何在 nginx-conf 中解析和负载平衡 http://app/ URL?

编辑:更具体地说,nginx如何知道http://app/主机名后面有2个ip地址。

docker-compose.yml

version: "3.9"

services: 
  app:
    build:
      context: ./Dockerfile
      dockerfile: Dockerfile-Application
    expose: 
      - "80"
    networks: 
      - application-net

  loadbalancer:
    image: nginx:latest
    depends_on: 
      - app
    ports: 
      - "80:80"
    volumes: 
      - ./cfg/nginx.conf:/etc/nginx/nginx.conf:ro
    networks: 
      - application-net

networks: 
  application-net:
    ipam:
      driver: default
      config: 
        - subnet: "10.150.1.0/24"

nginx.conf

user  nginx;

events {
    worker_connections   1000;
}
http {
        server {
              listen 80;
              location / {
                proxy_pass http://app:80/;
              }
        }
}

How exactly does Docker and Nginx resolve and load-balance http://app/ URL in the nginx-conf?

Docker 运行您的应用程序用来解析容器名称的 DNS 服务。这就是为什么对于在默认 bridge 网络以外的网络上启动的容器,您会看到 resolv.conf 不指向您的常规 DNS 服务器:

$ docker run --rm --net=net0 alpine cat /etc/resolv.conf | grep nameserver
nameserver 127.0.0.11