之前我手动建设了自己的网络mynetwork,因此我写成下面这个时候就报错了
version: '3'
services:
test09:
hostname: ninetry
image: centos
networks:
mynetwork:
ipv4_address: 192.168.130.120
ports:
- 8000:80
command: tail -f /dev/null
networks:
mynetwork:
driver: bridge
ipam:
config:
- subnet: 192.168.0.0/16
Creating network "composetest_mynetwork" with driver "bridge"
ERROR: Pool overlaps with other one on this address space
改成
networks:
mynetwork:
external: true
加入已经存在的外部网络
再启动就可以了
great@great-PC:~/composetest$ docker-compose up -d
Recreating test09 ... done
great@great-PC:~/composetest$ cat docker-compose.yml
version: '3'
services:
test09:
hostname: ninetry
image: centos
container_name: test09
networks:
mynetwork:
ipv4_address: 192.168.130.120
ports:
- 8000:80
command: tail -f /dev/null
networks:
mynetwork:
external: true
great@great-PC:~/composetest$
great@great-PC:~/composetest$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
381d59cf0c30 centos "tail -f /dev/null" About a minute ago Up About a minute 0.0.0.0:8000->80/tcp test09
b1a7653bb22a centos "tail -f /dev/null" 7 days ago Exited (255) 8 hours ago test02
c0e350b3300b centos "tail -f /dev/null" 7 days ago Exited (255) 8 hours ago test06
c17e4fedfe7b centos "tail -f /dev/null" 7 days ago Exited (255) 8 hours ago test05
5444d21140d2 centos "tail -f /dev/null" 7 days ago Exited (255) 8 hours ago test04
b2df5d29cdc0 centos "tail -f /dev/null" 7 days ago Exited (255) 8 hours ago test03
8614c05210ac centos "tail -f /dev/null" 7 days ago Exited (255) 8 hours ago test01
84322865edba centos "tail -f /dev/null" 7 days ago Exited (137) About an hour ago test00
great@great-PC:~/composetest$
============================
external
如果设置为true,则指定此网络已在Compose之外创建。 docker-compose up不会尝试创建它,并且如果它不存在将会引发一个错误。
外部不能与其他网络配置键(driver,driver_opts,group_add,ipam,internal)一起使用。