由于默认的bridge桥接网络,重启容器后会改变ip地址。在一些场景下我们希望固定容器IP地址。
docker-compose是docker的一个编排工具,相对于命令模式创建网络,容器等。使用配置文件相对来说更方便,可追溯问题。
直接粘贴docker-compose.yml文件
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.19.0.2
networks:
extnetwork:
ipam:
config:
- subnet: 172.19.0.0/16
gateway: 172.19.0.1
|
说明:
- gateway是网关地址
- subnet是网络号段
- extnetwork是自定义的网络名称
上面配置中我们的nginx容器固定ip为172.19.0.2
示例,自定义网络模式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
version: '2'
services:
nginx:
image: nginx:1.13.12
container_name: nginx
restart: always
networks:
extnetwork:
ports:
- 80:80
volumes:
- '/nginx/conf.d:/etc/nginx/conf.d'
nginx2:
image: nginx:1.13.12
container_name: nginx2
restart: always
networks:
extnetwork:
ipv4_address: 172.19.0.2
db:
image: mysql:5.7
container_name: db
volumes:
- /var/lib/mysql:/var/lib/mysql
restart: always
networks:
extnetwork:
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
image: wordpress:latest
container_name: wordpress
depends_on:
- db
ports:
- "8000:80"
restart: always
networks:
extnetwork:
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
extnetwork:
ipam:
config:
- subnet: 172.19.0.0/16
gateway: 172.19.0.1
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/hechaojie_com/article/details/83625265