Docker指定容器IP

查看 docker 网络模式

1
docker network ls

创建新的 bridge 网络

1
docker network create --driver bridge --subnet=172.2.2.0/16 --gateway=172.2.1.1 mynet

查看网络信息

1
docker network inspect mynet

创建容器并指定IP

1
docker run -e TZ="Asia/Shanghai" --privileged -itd  --name redis-demo --network=mynet --ip 172.2.12.1 redis /usr/sbin/init

docker-compose指定IP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
version: '2'
services:
nginx:
image: nginx:1.13.12
container_name: nginx
restart: always
tty: true
networks:
extnetwork:
ipv4_address: 172.2.12.3
networks:
# 自定义的网络名称
extnetwork:
ipam:
config:
- subnet: 172.2.12.0/16
gateway: 172.2.1.1

删除网路模式

1
docker network rm mynet

本文地址: https://github.com/maxzhao-it/blog/post/11485/