Docker-compose 简单使用

运行三个nginx实例。
编写 docker-compose.yml 文件

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
58
59
60
61
62
63
64
65
66
67
68
69
# 使用版本2,现在最新的到3.7了。
# https://docs.docker.com/compose/compose-file/
version: '2'
services:
# 配置第一个nginx
nginx:
restart: always
# 使用的镜像
image: nginx
# 映射端口
ports:
- 80:80
- 443:443
# 挂载本机文件,./代表跟我们的 docker-compose.yml 同目录下
volumes:
- ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/www:/usr/share/nginx/html
- ./nginx/logs:/var/log/nginx
# 指定使用docker创建的网络
networks:
my_net:
# 使用固定IP地址
ipv4_address: 172.16.238.10
# ipv6_address: 2001:3984:3989::10

# 配置第二个nginx
nginx1:
restart: always
image: nginx
# ports:
# - 8080:8080
volumes:
- ./nginx1/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx1/www:/usr/share/nginx/html
- ./nginx1/logs:/var/log/nginx
networks:
my_net:
ipv4_address: 172.16.238.20

# 配置第三个nginx
nginx2:
restart: always
image: nginx
# ports:
# - 8081:8081
volumes:
- ./nginx2/conf/nginx.conf:/etc/nginx/nginx.conf
- ./nginx2/www:/usr/share/nginx/html
- ./nginx2/logs:/var/log/nginx
networks:
my_net:
ipv4_address: 172.16.238.30

# 自定义网络
networks:
my_net:
driver: bridge
driver_opts:
# 是否开启 ipv6
com.docker.network.enable_ipv6: "false"
ipam:
driver: default
config:
# ipv4 配置
- subnet: 172.16.238.0/24
gateway: 172.16.238.1
# ipv6 配置
# - subnet: 2001:3984:3989::/64
# gateway: 2001:3984:3989::1

进入到 docker-compose.yml 所在目录执行

1
docker-compose up -d

就能运行起来了 up 是运行, -d 是后台运行,会默认寻找docker-compose.yml 的文件去执行
可以访问
http://localhost
http://localhost/nginx1
http://localhost/nginx2
测试

可以执行 docker ps 到跑起来的三个实例

1
docker ps

停止运行

1
docker-compose down