docker-compose container - nginx host not found in upstream

a0x5cqrl  于 2023-04-29  发布在  Nginx
关注(0)|答案(1)|浏览(262)

所以我试图创建一个nginx容器,它指向另一个运行nginx的容器,这是一个前端容器。我有我的default.conf文件用于nginx容器和前端容器,它自己的conf文件。
docker-compose.yml文件:

version: '3'
services:
  frontend:
    restart: always
    hostname: frontend
    build:
      context: ./demo-project
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://frontend/ping || exit 1"]
      interval: 1s
      timeout: 3s
      retries: 30
    networks:
      demo-net:
        aliases:
          - frontend

  proxy:
    hostname: proxy
    build:
      context: ./nginx
      dockerfile: Dockerfile
    depends_on:
      frontend:
        condition: service_healthy
    ports:
      - "80:80"
    networks:
      demo-net:
        aliases:
          - proxy
networks:
  demo-net:
    driver: bridge

我的nginx容器default.conf文件是:

upstream frontend_servers {
  server frontend:80;
}

server {
    listen 80;
    server_name localhost;
    location / {
        proxy_pass frontend_servers;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

nginx的Dockerfile是:

FROM nginx:latest

COPY ./default.conf /etc/nginx/conf.d/default.conf

我所面临的错误是

[emerg] 1#1: host not found in upstream "frontend:80" in /etc/nginx/nginx.conf:26
nginx: [emerg] host not found in upstream "frontend:80" in /etc/nginx/nginx.conf:26

我尝试使用这个命令docker run --rm -d --network demo_demo-networks --hostname proxy --name my_nginx nginx创建一个docker容器,并尝试使用这个命令的shell ping frontend,它给出了成功响应。同样的情况下,从docker-compose生成的图像,我试图手动运行它,仍然得到这个错误。
还尝试添加健康检查,它显示前端容器是健康的。出现这个问题是因为container甚至在连接网络之前就试图调用前端吗?

相关问题