gpt4 book ai didi

YAML 中的 docker-compose.yml 破折号语法

转载 作者:行者123 更新时间:2023-12-02 18:47:11 25 4
gpt4 key购买 nike

docker-compose.yml文件,为什么某些枚举用破折号完成 - ,而其他人没有?

services:
web: # the enumeration of [build, ports, volumes, environment] doesn't use a -
build: .
ports:
- "5000:5000" # why the - here? could we remove it and have ports: "5000:5000"?
volumes:
- .:/code # why the - here?
environment:
FLASK_ENV: development # why no - here?
redis:
image: "redis:alpine"
另一个例子:
version: '2'
services:
db:
image: mysql:5.7
volumes:
- ./mysql:/var/lib/mysql # could we remove this - prefix?
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress # no - in this enumeration, why?
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db # would it be ok without - ?
image: wordpress:latest
volumes:
- ./wp:/var/www/html # same
...

最佳答案

根据:https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
破折号代表列表。

All members of a list are lines beginning at the same indentation level starting with a -


# A list of tasty fruits
- Apple
- Orange
- Strawberry
- Mango
没有破折号意味着它们是字典的键值对。

A dictionary is represented in a simple key: value form


martin:
name: Martin D'vloper
job: Developer
skill: Elite
根据 Docker Compose 文档位于: https://docs.docker.com/compose/compose-file/compose-file-v2/
environment:
RACK_ENV: development
SHOW: 'true'
SESSION_SECRET:
是相同的:
environment:
- RACK_ENV=development
- SHOW=true
- SESSION_SECRET
请注意,docker 并不关心您在 environment 中使用的是哪一个。 key ,只要它们是一致的。 Docker-compose 语法恰好以这种方式定义它。

关于YAML 中的 docker-compose.yml 破折号语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64275361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com