Docker-Compose-file

Compos file 版本3以上

Compos file v3官网

build

只支持单机运行

1
2
3
4
5
6
7
8
version: "3" #指明版本3,docker stack需要版本3以上
services:
webapp:
build: #集群部署会忽略构建镜像,stack只支持提前构建好镜像
context: ./dir #构建上下文路径
dockerfile: Dockerfile-alternate #构建文件
args: #构建参数
buildno: 1

configs (swarm模式)

只支持集群模式运行,只需要在单机上引用,会自动在其他节点创建配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
version: "3.3"
services:
redis:
image: redis:latest
deploy:
replicas: 1
configs:
- my_config
- my_other_config
configs:
my_config:
file: ./my_config.txt
my_other_config:
external: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
version: '3'
services:
nginx:
image: harbor.iexxk.dev/library/web-gate-id-v3:S3.2.1
restart: unless-stopped
ports:
- "88:80"
# volumes:
# - ./config/default.conf:/etc/nginx/conf.d/default.conf 不支持集群使用configs代替
configs:
- source: nginx_config
target: /etc/nginx/conf.d/default.conf
env_file:
- ./config/nginx.env #env也支持集群
configs:
nginx_config:
file: ./config/default.conf #支持集群

command

会覆盖dockerfile里面的命令例如:

1
2
command: bundle exec thin -p 3000 #docker-compose.yml覆盖dockerfile的命令
command: ["bundle", "exec", "thin", "-p", "3000"] #dockerfile等效于上面的命令

deploy(swarm模式)

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
version: '3.2'
services:
redis:
image: redis:alpine
labels: #容器上的标签
com.example.description: " containers 上的标签"
deploy:
mode: global #可选global(全局模式,每台机一个)/replicated(多台)
replicas: 6 #部署的总数量
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
endpoint_mode: vip #可选vip(默认)/dnsrr,主要是IP啥的
labels: #服务器上的标签
com.example.description: "This label will appear on the web service"
placement: #指定部署的信息
constraints:
- node.role == manager #只在管理节点运行
- engine.labels.operatingsystem == ubuntu 14.04
preferences:
- spread: node.labels.zone
resources: #资源限制
limits:
#cpu个数0.5指一个cpu的50%,2.00指2个cpu(猜测99%)其中docker stats命令可以查看cpu使用率和内存使用,其中cpu使用率是指单核的使用率,也就是如果cpus设置为2,使用率200%就代表2个核都使用达到了100%
cpus: '0.50'
memory: 50M
reservations:
cpus: '0.25'
memory: 20M
restart_policy: #重启策略
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
update_config: #更新配置策略
parallelism: 2
delay: 10s
order: stop-first
depends_on: #依赖的服务模块,在db启动才启动服务,但是不能保证db启动完,如果要设置启动顺序见
#https://docs.docker.com/compose/startup-order/
- db
- redis
healthcheck: #健康检查
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 1m30s
timeout: 10s
retries: 3
start_period: 40s
logging: #日志服务
driver: syslog
options:
syslog-address: "tcp://192.168.0.42:123"
volumes: #自定义挂在卷
- type: volume
source: mydata
target: /data
volume:
nocopy: true
- type: bind
source: ./static
target: /opt/app/static
ports: #自定义端口
- target: 80
published: 8080
protocol: tcp
mode: host

docker-compose 网络互相访问

让一个docker-compos里面的服务verse-web-admin-novel-1访问另一个gate-v3_defaul中的spring-gateway:8080

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#查看网络
[root@frts-p1 verse]# docker network ls
NETWORK ID NAME DRIVER SCOPE
e8a9788c1674 bridge bridge local
305ea0a9d372 gate-v3_default bridge local
2770fd13abc4 host host local
0a9e80edc8d3 verse_default bridge local
#查看容器名
[root@frts-p1 verse]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db427829805e web:1 "/docker-entrypoint.…" 2 minute ago Up 2 second 92->80/tcp verse-web-admin-novel-1
69f449d6f2aa gat:1 "sh -c 'java $JAVA_O…" 2 months ago Up 2 months 8080/tcp gate-v3-spring-gateway-1
#连接网络 docker network connect <目标网络名称> <容器ID或名称>
[root@frts-p1 verse]# docker network connect gate-v3_default verse-web-admin-novel-1

docker-compose配置verse文件:

1
2
3
4
5
6
7
8
9
version: '3'
services:
web-admin-novel:
image: harbor.hcytech.dev/library/web-admin-novel:S1.3.1
restart: unless-stopped
ports:
- "92:80"
volumes:
- ./config/web-admin.conf:/etc/nginx/conf.d/default.conf

web-admin.conf配置:

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
# 后端接口代理地址
upstream api_server {
server spring-gateway:8080;
}
server {
listen 80;
server_name localhost;
underscores_in_headers on;
client_max_body_size 256m;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /api/ {
rewrite ~/api/(.*)$ /$1 break;
proxy_pass http://api_server/api/;
proxy_set_header Host $host;
proxy_pass_request_headers on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 60000;
proxy_send_timeout 60000;
proxy_intercept_errors on; # 开启拦截错误
error_page 500 = /500handler;
error_page 404 = /404handler;
}
}

docker-compose配置gate-v3文件:

1
2
3
4
5
6
7
8
9
version: '3'
services:
spring-gateway:
image: harbor.hcytech.dev/library/hcytech-gateway:S1.0.3
volumes:
- ./config/application.yaml:/home/application.yaml
env_file:
- ./config/gateway.env
- ./config/redis.env

问题

错误services.nginx.ports.0 must be a string or number是因为自定义端口,只支持3.2以上

1
2
3
4
5
6
version: '3.2' 
ports: #自定义端口
- target: 80
published: 8080
protocol: tcp
mode: host