【问题标题】:Use inline comments in docker-compose command在 docker-compose 命令中使用内联注释
【发布时间】:2020-01-26 17:43:48
【问题描述】:

在我的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

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

狂欢:

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

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

但是docker-compose up 说:

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

如果我将$ 转义为$$,它会说:

错误:for foo 没有结束引号

我怎样才能让它工作?

【问题讨论】:

  • 最好放在shell脚本中,复制到dockerfield,
  • 尝试用另一个$ 转义$ 符号。您将摆脱 invalid interpolation 错误。
  • @b0gusb 谢谢这是个好主意,但现在它说ERROR: for foo No closing quotation
  • 另外,一个问题可能来自# don't …中的引用
  • @ErikMD 否,由去除内联 cmets 的 cut 处理

标签: linux bash docker docker-compose yaml


【解决方案1】:

这里有一个更好的方法。 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多