Using Template Functions in Container Entrypoints

While it isn’t obvious, template functions can be used within the entrypoint arrays for containers on the native scheduler - you just need to escape any quotation marks within the config option. This is because Replicated first tries to parse the untemplated string as a json array while falling back to using the entire entry as one string if that fails. Then, template functions are applied to each string and the resulting array (with only one member in the second case) is forwarded to Docker. A couple examples of how this works - and doesn’t work - are below.

BAD

components:
  - name: echo
    containers:
      - name: hello_world
        image_name: alpine
        source: public
        version: latest
        cmd: >-
          [
            "/bin/sh",
            "-c",
            "echo Hello", 
            "echo {{repl ConfigOption "name"}}"
          ]

As you can probably tell from the highlighting, this is not going to work perfectly. Replicated will attempt to parse ["/bin/sh", "-c", "echo Hello", "echo {{repl ConfigOption "name"}}"] as a json array and fail, so it will attempt to use the entire string as one entry. After applying the template function, you end up with a json array with one entry, ["/bin/sh", "-c", "echo Hello", "echo Andrew"]. Of course, things like quotation marks must be escaped to go into a double quoted string, so the array ends up looking like ["[\"/bin/sh\", \"-c\", \"echo Hello\", \"echo Andrew\"]"] - which unsurprisingly is not a command on alpine and so fails to run.

GOOD

components:
  - name: echo
    containers:
      - name: hello_world
        image_name: alpine
        source: public
        version: latest
        cmd: >-
          [
            "/bin/sh",
            "-c",
            "echo Hello", 
            "echo {{repl ConfigOption \"name\"}}"
          ]

This version, though - this works. ["/bin/sh", "-c", "echo Hello", "echo {{repl ConfigOption \"name\"}}"] parses properly as an array with members /bin/sh, -c echo Hello, and echo {{repl ConfigOption "name"}}. After parsing the template functions in each, we’re left with /bin/sh, -c, echo Hello, and echo Andrew. The resulting json array passed to docker is ["/bin/sh", "-c", "echo Hello", "echo {{repl ConfigOption Andrew}}"], and /bin/sh is an executable on Alpine.