- 设置两个容器处于同一网段
- 容器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成功,容器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的网桥
- 修改名称为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