在我的docker-compose.yml中,我需要使用一个很长的command,我想对其进行记录。这不容易做到,但是我有一个可以解决ALMOST问题的方法。

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说:



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



    我怎样才能使它正常工作?

    最佳答案

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

    10-07 21:10