docker-compose 设置容器静态IP

方法一
1.创建网络
docker network create --driver bridge --subnet 172.33.0.0/16 --gateway 172.33.0.1 serv-net

PS:创建网络的时候,必须要指定对应的subnet和gateway,
如果是 docker network create --driver bridge 172.33.0.1 serv-net 命令创建的网络,启动的时候
会提示

ERROR: for serv-dns b’user specified IP address is supported only when connecting to networks with user configured subnets’

2.编写yaml文件
docker-compose.yaml 文件如下

version : “2”
networks:
default:
external:
name: serv-net

services:
nginx:
container_name: front-nginx
build: ./front-nginx
ports:
- “5001:80”
volumes:

links:

networks:
default:
ipv4_address: 172.33.0.21
servdns:
container_name: serv-dns
build: ./dns
ports:

volumes:

environment:

networks:
default:
ipv4_address: 172.33.0.2

docker-compose 设置容器静态IP_第1张图片
注意:红色框的网络名称应该是一致的,黄色就是已经存在的那个网络

如果不一致,启动时候会提示
ERROR: Service “XXXX” uses an undefined network “XXX”

方法二
参考以下链接
https://www.cnblogs.com/dablyo/p/11019632.html

你可能感兴趣的:(运维)