如果没指定网络的话,docker-compose会默认生成一个当前路径文件夹名字+default的网段,如下nginx6_default
[root@VM-100-16-centos nginx6]# docker network ls
NETWORK ID NAME DRIVER SCOPE
4ab28897a859 abc bridge local
4c8d28e424e6 bridge bridge local
068446b62f40 host host local
4e4e5254a039 nginx6_default bridge local
13aa89bbe8cd none null local
[root@VM-100-16-centos nginx6]#
cat docker-compose.ym
------------------------------
version: '3'
services:
nginx:
restart: always
container_name: nginx
image: docker.io/nginx
ports:
- 1001:80
networks:
aaaabc:
ipv4_address: 172.16.238.100
networks:
#这里的aaaabc是自己瞎取的,但是要和上面services中的networks匹配上,可以理解为networks新建一个网络,services使用该网络。这里是新建一个网段。
aaaabc:
ipam:
driver: default
config:
- subnet: "172.16.238.0/24"
- subnet: "2001:3984:3989::/64"
如果需要使用现有网段,则如下,这里abc1,abcd会新建nginx6_abc1,nginx6_abcd,abcaaa会使用现有的docker网络:,当然,abcaaa可以叫abc,那么external: true 即可,后面的name也就不需要了
version: '3'
services:
nginx:
restart: always
container_name: nginx
image: docker.io/nginx
ports:
- 1001:80
networks:
- abc1
- hahahaha
- default
- abcaaa
networks:
abc1:
abcd:
abcaaa:
external:
name: abc