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 - 只允许一台设备通过SSH访问服务器

linux - 使用 Ansible 获取 Linux 网络接口(interface)名称?

bash - 如何区分 bashrc 文件中的 ssh session 或 vnc session 或控制台 session

linux - Emacs 编译缓冲区显示引号的十六进制值

linux - 在 Linux 上使用 .net core 更改 IP 地址

linux - 如何找到时间编写 sh 脚本

python - 使用 python-xlsxwriter 或 bash 打开受密码保护的 ods 文件

amazon-web-services - 从浏览器访问两个容器

docker - 执行Docker Run并检查参数是否为空

docker - 未在dotnetcore visual studio模板的docker镜像上安装应用