12

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?

5
  • 2
    better to place in shell script and copy at dockerfield, Commented Sep 26, 2019 at 13:01
  • Try to escape the $ sign with another $. You will get rid of the invalid interpolation error. Commented Sep 26, 2019 at 13:26
  • @b0gusb Thanks that was a good idea, but now it says ERROR: for foo No closing quotation Commented Sep 26, 2019 at 15:18
  • Also, an issue probably comes from the quote in # don't … Commented Sep 26, 2019 at 20:47
  • @ErikMD No that is handled by the cut that strips inline comments Commented Sep 27, 2019 at 7:11

1 Answer 1

27

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
Sign up to request clarification or add additional context in comments.

2 Comments

For some reason this does not work for me :(
@mvc I suggest you open a separate question with a minimal reproducible sample.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.