在 docker-compose 命令中使用内联注释

lon*_*nix 7 linux bash yaml docker docker-compose

在我的docker-compose.yml我需要使用很长的command,我想记录它。这不容易完成,但我有一个几乎有效的解决方法。

foo:                                    # valid comment
  image: foo:latest                     # valid comment
  command: >
           printf '
           something                    # explanation for this command
           --arg                        # explanation for this switch
           --a                          # explanation
           --b hello
           -c                           # this does...
           --d spam                     # don't use this when...
           #some notes
           --e ham                      # hmmm
           --eggs                       # explanation
           ' |grep -v ^[[:space:]]*$ |grep -v ^# |cut -d# -f1   # valid comment
  restart: always                       # valid comment
Run Code Online (Sandbox Code Playgroud)

所以每个命令和开关都可以被注释。

猛击:

  • printf ' ... ' 将所有内容打印为文本,作为命令运行
  • grep -v ^[[:space:]]*$ 忽略第一个空行
  • grep -v ^# 忽略注释行
  • cut -d# -f1 从每一行中去除行内注释

这个技巧在shell中完美运行!

然而docker-compose up说:

错误:服务“foo”中“command”选项的插值格式无效:“printf ' something ...

如果我$$$它所说的那样逃脱:

错误:对于 foo 没有结束语

我怎样才能让它发挥作用?

lon*_*nix 12

这里有一个更好的方法。该command文档没有表现出来,但我认为这是标准的YAML,所以它是允许的。

foo:                               # valid comment
  image: foo:latest                # valid comment
  command:
    - something                    # explanation for this command
    - --arg                        # explanation for this switch
    - --a                          # explanation
    - --b hello
    - -c                           # this does...
    - --d spam                     # don't use this when...
    #some notes
    - --e ham                      # hmmm
    - --eggs                       # explanation
  restart: always                  # valid comment
Run Code Online (Sandbox Code Playgroud)