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

标签 linux bash docker docker-compose yaml

在我的 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 从每一行中去除行内注释

这个技巧在 shell 中非常有效!

但是 docker-compose up 说:

ERROR: Invalid interpolation format for "command" option in service "foo": "printf ' something ...

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

ERROR: for foo No closing quotation

我怎样才能让它工作?

最佳答案

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

关于linux - 在 docker-compose 命令中使用内联注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58116906/

相关文章:

linux - 使用 awk 和排序从多个平面文件更新 rrdtool

bash - 在 msys bash 上包含换行符时 PS1 命令替换失败

Linux/用宏替换旧字符串

docker - 通过 docker 日志查看 cron 输出,无需使用额外文件

linux - 输出包括使用 Awk 检查的每个条件的空行

linux - .htaccess文件破坏了我的WP网站

linux - svn 导出在命令行上工作,但在 shell 脚本中不工作

docker - Docker无法调和Kubernetes插件组件的状态

python-3.x - python pdfkit错误wkhtmltopdf以非零代码-6退出

linux - shell Printf : Invalid Number error