Docker Compose 修改 YAML,如何查看它渲染的 YAML?

问题描述 投票:0回答:1

我的 Docker Compose 正在运行,从 https://stackoverflow.com/a/30064175 开始,我有类似的东西:

services:
  stuff:
    image: alpine
    entrypoint: [ "/bin/sh", "-c" ]
    command: |
        export a=100 && \
        export b='f a' && \
        export c="a g" && \        
        echo "a = ${a} ; b = ${b} ; c = ${c}";

我尝试过:

command: >
,以及大量变体https://stackoverflow.com/a/21699210。我尝试过有和没有
&&
有和没有
\

我可以看到 Docker Compose 在实际执行之前(即在所有插值和其他渲染之后)看到的内容吗?

string docker docker-compose yaml quoting
1个回答
0
投票

我想不是。

您可以

docker compose up --dry-run
,但它不会为您提供任何已解析的 YAML。

您可以 lint 但这只能确认您的 YAML 有效。

您的错误是您必须转义

/bin/sh -c {string}

中的变量值引用

所以:

services:
  stuff:
    image: alpine
    entrypoint: /bin/sh
    command:
    - -c
    - |
      a=100
      b="f a"
      c="a g"
      echo "a = $${a} ; b = $${b} ; c = $${c}"

注意

  1. entrypoint
    更好地定义为外壳
    /bin/sh
  2. command
    包括
    -c
    ,然后是 YAML 多行标量
  3. export
    仅对于子 shell 来说是必需的
  4. &&
    是多余的,因为我们可以在多行标量中使用换行符
docker compose rm --force && \
docker compose up
Going to remove 78898044-stuff-1
[+] Removing 1/0
 ✔ Container 78898044-stuff-1  Removed                                                                                                                                                                                                                                     0.0s 
[+] Running 1/0
 ✔ Container 78898044-stuff-1  Created                                                                                                                                                                                                                                     0.0s 
Attaching to stuff-1
stuff-1  | a = 100 ; b = f a ; c = a g
stuff-1 exited with code 0
© www.soinside.com 2019 - 2024. All rights reserved.