我想通过Docker Compose在Docker容器内执行以下命令

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

这是该特定容器的基本码头组合

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

我想在这两个命令下方添加该多行命令。如果是一个命令,我可以简单地使用

- sh -c "echo 'single line command'" 但是,在这种情况下,如何执行此多行命令?

分析解答

您不会在compose文件中运行这样的命令。

请记住,一个容器仅运行一个命令,然后退出。例如,这使得很难使用command:创建配置文件,因为这将是容器唯一做的事情。对于此设置,如注释中所述,这更加复杂,因为command:实际上是其他命令的参数列表。您可以reverse-engineerimage的作用并覆盖其entrypoint:,但是很快就会变得非常复杂。

由于您只是尝试提供静态配置文件,因此应该在主机上创建它,并使用Compose volumes:通过绑定安装进行注入。看起来您正在尝试将内容附加到现有配置中,因此您可能需要从提取现有配置开始。

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