Search code examples
bashdockershelldocker-composesh

How do i execute a multi line command through docker compose?


I want to execute the following command inside my docker container through docker compose

cat <<EOT >> /config.toml
[[tls.certificates]]
  certFile = "/etc/letsencrypt/live/example.site/fullchain.pem"
  keyFile = "/etc/letsencrypt/live/example.site/privkey.pem"
EOT

Here's the basic docker compose for this particular container

  container:
    image: container-image
    command:
      - --api=true
      - --dashboard=true

I want to add that multi line command below those two commands. If it was a single command, i could have simply used

- sh -c "echo 'single line command'" But how do i execute this multi line command in this instance ?


Solution

  • You wouldn't run a command like this in a Compose file.

    Remember that a container only runs a single command, and then exits. This makes it hard to use command: to create a configuration file, for example, since that will be the only thing the container does. For this setup, as noted in comments, this is even more complicated because the command: is actually a list of arguments to some other command. You could reverse-engineer what the image does and override its entrypoint:, but that gets very complicated very quickly.

    Since you're just trying to provide a static config file, you should create it on the host and inject it via a bind mount using Compose volumes:. It looks like you're trying to append content to an existing configuration, so you may need to start by extracting the existing configuration.

    docker create --name tmp container-image
    docker cp tmp:/etc/letsencrypt/config.toml ./
    docker rm tmp
    
    $EDITOR config.toml  # and add the lines shown, on the host
    
    version: '3.8'
    services:
      container:
        image: container-image
        command:
          - --api=true
          - --dashboard=true
        volumes:                                       # <-- add if not present
          - ./config.toml:/etc/letsencrypt/config.toml # <-- add