Search code examples
linuxbashdockerdocker-composeyaml

Use inline comments in docker-compose command


In my docker-compose.yml I need to use a very long command, and I want to document it. This is not easily done, but I have a workaround that's ALMOST working.

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

So every command and switch can be commented.

The bash:

  • printf ' ... ' prints all the stuff as text, to be run as a command
  • grep -v ^[[:space:]]*$ ignores the first empty line
  • grep -v ^# ignores comment lines
  • cut -d# -f1 strips inline comments from each line

This trick works perfectly in the shell !

However docker-compose up says:

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

If I escape the $ as $$ it says:

ERROR: for foo No closing quotation

How can I get this to work?


Solution

  • Here's a better way. The command docs don't show it, but I think it's standard YAML, so it's allowed.

    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