Docker [emerg] 1#1:在单独的撰写文件之后,在上游找不到主机

时间:2019-02-17 12:15:58

标签: docker nginx docker-compose

我有一个撰写文件。当我跑步时,那正常工作

services:
[...]
  wordpress-1:
     depends_on:
       - database
     image: wordpress:latest
     expose:
       - 5000
     volumes:
       - ./site1/:/var/www/html/
    [...]

  nginx:
    container_name: nginx_
    build:
     context: ./services/nginx
     dockerfile: Dockerfile-prod
    ports:
      - 80:80
    depends_on:
      - wordpress-1
    networks:
      - my-network
[...]

和nginx conf:

server {

  listen 80;
  server_name example.com;

  location / {
    proxy_pass http://wordpress-1:80;
    proxy_redirect    default;
    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;
  }
}

但是,当我将其分成2个docker compose(一个用于wordpress-1服务,一个用于nginx服务)分离后,当我运行compose文件包含nginx时,出现以下错误:[emerg] 1#1:找不到主机在上游的“ wordpress-1”中

你能帮我吗? 谢谢

1 个答案:

答案 0 :(得分:1)

默认情况下,Docker-compose为每组服务(例如,每个docker-compose文件)创建一个网络。

如果确实需要单独的docker-compose文件,则可以在服务之间创建共享网络,如下所示:

$ cat a/docker-compose.yml 
version: '3.5'
services:
  a:
    image: alpine
    command: sleep 9999
    networks: ["mynet"]
networks:
  mynet:
    name: shared-net

$ cat b/docker-compose.yml 
version: '3.5'
services:
  b:
    image: alpine
    command: sleep 9999
    networks: ["mynet"]
networks:
  mynet:
    name: shared-net

启动每个按钮后,您可以从b ping到a:

$ docker exec -it b_b_1 ping -c 1 a_a_1
PING a_a_1 (172.21.0.3): 56 data bytes
64 bytes from 172.21.0.3: seq=0 ttl=64 time=0.081 ms

--- a_a_1 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 0.081/0.081/0.081 ms

它在重新启动之间起作用,例如,如果您重新启动容器。

请注意,如果nginx找不到主机,则是emerg错误,nginx可能会完全停止-这可能是服务重启之间的问题(因为dns解析不再起作用)。