如何通过 yaml 语法实现 docker-compose 内磁盘映射固定路径 + 自定义路径的统一挂载
比如我尝试过锚点方法,可以实现类似效果:
version: "3"
x-common-volumes: &common-volumes
- /data1/:/data1/
- /data2/:/data2/
- /etc/resolv.conf:/etc/resolv.conf
services:
nacos:
image: nacos:latest
volumes: *common-volumes
admin:
image: admin:latest
volumes: *common-volumes
解析后:
---
version: "3"
x-common-volumes:
- /data1/:/data1/
- /data2/:/data2/
- /etc/resolv.conf:/etc/resolv.conf
services:
nacos:
image: nacos:latest
volumes:
- /data1/:/data1/
- /data2/:/data2/
- /etc/resolv.conf:/etc/resolv.conf
admin:
image: admin:latest
volumes:
- /data1/:/data1/
- /data2/:/data2/
- /etc/resolv.conf:/etc/resolv.conf
-------------------------
但是在 volumes 中增加自定义挂载项时就无法使用
比如:
version: "3"
x-common-volumes: &common-volumes
- /data1/:/data1/
- /data2/:/data2/
- /etc/resolv.conf:/etc/resolv.conf
services:
nacos:
image: nacos:latest
volumes: *common-volumes
- ${data_dir}/nacos_data:/opt/nacos/data
admin:
image: admin:latest
volumes: *common-volumes
- ${data_dir}/admin-1:/data/
解析直接报错
-------------------------
我尝试换种写法,可以解析但因为语法问题无法被 docker-compose 读取
version: "3"
x-common-volumes: &common-volumes
- /data1/:/data1/
- /data2/:/data2/
- /etc/resolv.conf:/etc/resolv.conf
services:
nacos:
image: nacos:latest
volumes:
- ${data_dir}/nacos_data:/opt/nacos/data
- *common-volumes
admin:
image: admin:latest
volumes:
- ${data_dir}/admin-1:/data/
- *common-volumes
解析后:
---
version: "3"
x-common-volumes:
- /data1/:/data1/
- /data2/:/data2/
- /etc/resolv.conf:/etc/resolv.conf
services:
nacos:
image: nacos:latest
volumes:
- ${data_dir}/nacos_data:/opt/nacos/data
- - /data1/:/data1/
- /data2/:/data2/
- /etc/resolv.conf:/etc/resolv.conf
admin:
image: admin:latest
volumes:
- ${data_dir}/admin-1:/data/
- - /data1/:/data1/
- /data2/:/data2/
- /etc/resolv.conf:/etc/resolv.conf
yaml 语法检查网站:
https://www.yamllint.com/