Search code examples
stringdockerdocker-composeyamlquoting

Docker Compose mangling YAML, how do I see the YAML it renders?


My Docker Compose is acting up, starting from https://stackoverflow.com/a/30064175, I have something like:

services:
  stuff:
    image: alpine
    entrypoint: [ "/bin/sh", "-c" ]
    command: |
        export a=100 && \
        export b='f a' && \
        export c="a g" && \        
        echo "a = ${a} ; b = ${b} ; c = ${c}";

I've tried: command: >, and a large number of variants https://stackoverflow.com/a/21699210. I've tried with and without && with and without \.

Can I see what Docker Compose sees before it actually executes, i.e., after all the interpolation and other rendering?


Solution

  • I think not.

    You can docker compose up --dry-run but it doesn't give you any parsed YAML.

    You can lint but that only confirms that your YAML is valid.

    Your error is that you must escape the variable value references within the /bin/sh -c {string}

    So:

    services:
      stuff:
        image: alpine
        entrypoint: /bin/sh
        command:
        - -c
        - |
          a=100
          b="f a"
          c="a g"
          echo "a = $${a} ; b = $${b} ; c = $${c}"
    

    NOTE

    1. entrypoint is better overriden as just the shell /bin/sh
    2. command could include the /bin/sh instead of entrypoint
    3. command includes the -c and then the YAML multi-line scalar
    4. export is only necessary for subshells
    5. && is redundant since we can use newlines in a multi-line scalar
    docker compose rm --force && \
    docker compose up
    
    Going to remove 78898044-stuff-1
    [+] Removing 1/0
     ✔ Container 78898044-stuff-1  Removed                                                                                                                                                                                                                                     0.0s 
    [+] Running 1/0
     ✔ Container 78898044-stuff-1  Created                                                                                                                                                                                                                                     0.0s 
    Attaching to stuff-1
    stuff-1  | a = 100 ; b = f a ; c = a g
    stuff-1 exited with code 0