docker-compsoe: network xxx declared as external, but could not be found

  1. 设置两个容器处于同一网段
  • 容器1 docker-compose.yml
version: '3'
services:
  nginx:
    restart: always
    container_name: nginx
    image: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./www:/var/www
      - ./logs:/var/log/nginx
      - ./nginx.conf/:/etc/nginx/nginx.conf
      - ./conf.d:/etc/nginx/conf.d
      - ./ssl:/etc/nginx/ssl
    networks:
      - default
      - aliyun-nextcloud

networks:
  aliyun-nextcloud:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: "192.168.1.0/24"
          gateway: "192.168.1.1"
  • 容器2 docker-compose.yml
version: '3'
services:
  nginx:
    container_name: nextcloud-nginx
    image: nginx
    restart: always
    expose:
      - '8080'
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      #- ./ssl:/etc/nginx/ssl
      - app_data:/var/www/html
      - ./log:/var/log/nginx
    networks:
      - default
      - aliyun-nextcloud
volumes:
  app_data:
networks:
  aliyun-nextcloud:
    external: true
  1. 运行容器1成功,容器2出现错误
  • 错误如下:
root@win:~/docker-onlyoffice-nextcloud# docker compose up -d
[+] Running 0/1
 ⠙ Network docker-onlyoffice-nextcloud_default  Created                                                                                                                                      0.1s 
network aliyun-nextcloud declared as external, but could not be found
  • 错误原因是找不到aliyun-nextcloud?
  • 输入 dokcer network ls 查看
root@win:~/docker-onlyoffice-nextcloud# docker network ls
NETWORK ID     NAME                                  DRIVER    SCOPE
9eaa413dce32   bridge                                bridge    local
dc162d6dfb7d   docker-onlyoffice-nextcloud_default   bridge    local
7ece0f564c1e   host                                  host      local
f7166b104e7d   nginx_aliyun-nextcloud                bridge    local
c62385488459   nginx_default                         bridge    local
fe622a1b7ccc   none                                  null      local
  • 找到了一个nginx_aliyun-nextcloud的网桥
  1. 修改名称为nginx_aliyun-nextcloud,运行成功。
version: '3'
services:
  nginx:
    container_name: nextcloud-nginx
    image: nginx
    restart: always
    expose:
      - '8080'
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      #- ./ssl:/etc/nginx/ssl
      - app_data:/var/www/html
      - ./log:/var/log/nginx
    networks:
      - default
      - nginx_aliyun-nextcloud
volumes:
  app_data:
networks:
  nginx_aliyun-nextcloud:
    external: true
  • 结果如下:
root@win:~/docker-onlyoffice-nextcloud# docker compose up -d
[+] Running 1/1
 ✔ Container nextcloud-nginx  Started                                                                                                                                                  

4.使用ping命令ping容器

root@b90297c3b9df:/# ping nextcloud-nginx
PING nextcloud-nginx (192.168.1.3) 56(84) bytes of data.
64 bytes from nextcloud-nginx.nginx_aliyun-nextcloud (192.168.1.3): icmp_seq=1 ttl=64 time=0.222 ms
64 bytes from nextcloud-nginx.nginx_aliyun-nextcloud (192.168.1.3): icmp_seq=2 ttl=64 time=0.081 ms
64 bytes from nextcloud-nginx.nginx_aliyun-nextcloud (192.168.1.3): icmp_seq=3 ttl=64 time=0.139 ms
^C
--- nextcloud-nginx ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2025ms
rtt min/avg/max/mdev = 0.081/0.147/0.222/0.057 ms
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值