1、docker-compose yaml指定具体容器网桥ip网段subnet
docker-compose 启动yaml有时可能的容器网段与宿主机的ip冲突导致宿主机上不了网,这时候可以更改yaml指定subnet
宿主机内网一般是192**,这时候容器可以指定172*
cpp
version: '3.9'
services:
coredns:
image: coredns/coredns:1.10.0
container_name: coredns
ports:
- 53:53/udp
networks:
mynetwork:
ipv4_address: 172.28.0.4
volumes:
- ./coredns/hostsfile:/etc/coredns/hostsfile
- ./coredns/Corefile:/Corefile
networks:
mynetwork1:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.28.0.0/16
gateway: 172.28.0.1
案例:
subnet 关键字设置了容器的网桥 IP 网段为 192.168.0.0/16,其中 ipv4_address 指定了容器的 IP 地址为 192.168.0.2。
cpp
version: '3'
services:
myservice:
image: <image>
networks:
mynetwork:
ipv4_address: 192.168.0.2
networks:
mynetwork:
driver: bridge
ipam:
config:
- subnet: 192.168.0.0/16
2、docker指定subnet
1)先创建自定义网络
cpp
docker network create --subnet=172.18.0.0/16 shadownet
2)运行docker指定具体net
cpp
docker run --net shadownet -p 6008:6008 registry.cn-hangzhou.aliyuncs.com/fastgpt_docker/m3e-large-api
3)查看容器ip
可以看到分到的ip是指定的172.18网段下面ip **0.2
cpp
docker inspect 0418bea8c6aecd3af266eb1
3、上面2把转出docker compose yml文件运行
1)首先相同网段名会冲突可以先删除前面的docker network rm shadownet;也可以换个名称和网段
2)运行指定yml:docker-compose -f C:\Users\loong\Downloads\subnet.yml up
subnet.yml
cpp
version: '3'
services:
myservice:
image: registry.cn-hangzhou.aliyuncs.com/fastgpt_docker/m3e-large-api
ports:
- 6008:6008
networks:
- shadownet
networks:
shadownet:
driver: bridge
ipam:
config:
- subnet: 172.18.0.0/16
3、docker取消自启动
https://stackoverflow.com/questions/37599128/docker-how-do-you-disable-auto-restart-on-a-container
--restart=always 下面更改
cpp
docker update --restart=no <container_name_or_id>
查看是否取消成功
cpp
docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' <container_name_or_id>