服务 "nginx-proxy" 使用未定义的网络 "nginx-proxy"

Service "nginx-proxy" uses an undefined network "nginx-proxy"

我正在尝试 运行 使用 Nginx-Proxy.

Ubuntu VPS 上 docker 容器内的 WordPress 站点

我创建了以下 docker-compose.yml 文件


version: '3.4'
services:

  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - 80:80
      - 443:443
    restart: always
    networks:
      - nginx-proxy
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - /etc/nginx/vhost.d:/etc/nginx/vhost.d:ro
      - /etc/certificates:/etc/nginx/certs

  wordpress:
    image: wordpress
    container_name: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      - VIRTUAL_HOST=wordpress.domain.com
      - VIRTUAL_PORT=5500
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=db_username
      - WORDPRESS_DB_PASSWORD=db_password
      - WORDPRESS_DB_NAME=db_name
    depends_on:
      - nginx-proxy
      - db
    networks:
      - nginx-proxy
    volumes:
      - wordpress:/var/www/html
    ports:
      - 5500:5500
    expose:
      - 5500
  db:
    image: mysql:latest
    container_name: db
    restart: always
    environment:
       MYSQL_DATABASE: db_name
       MYSQL_USER: db_username
       MYSQL_PASSWORD: db_password
       MySQL_RANDOM_ROOT_PASSWORD: '1'
    depends_on:
      - nginx-proxy
    networks:
      - nginx-proxy
    volumes:
      - db:/var/lib/mysql
    ports:
      - 5600:5600
    expose:
      - 5600
volumes:
   wordpress:
   db:

每次我 运行 docker-compose up 我都会得到以下错误

Service "nginx-proxy" uses an undefined network "nginx-proxy"

我使用以下命令创建了一个网络

docker network create nginx-proxy

这是docker network ls

的输出

为什么会出现该错误?我该如何解决?

您在 per-service networks: 块中命名的任何内容都需要在 top-level networks: 块中声明。

version: '3.4'
services:
  nginx-proxy:
    networks:
      - nginx-proxy # <-- matches below
volumes: { ... }
networks:
  nginx-proxy:      # <-- matches above
    # may be empty, but this block is required

如果您根本不声明任何 networks:,请编写 creates a network named default and attaches containers to it。对于几乎所有用途,这就是您所需要的。因此,完全删除 networks: 块可能更简单。

version: '3.4'
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    # No networks:; just use automatic [default]

(您同样不需要在撰写级别手动提供 container_name:expose: 端口。)