caddyserver / caddy-docker

Source for the official Caddy v2 Docker Image
https://hub.docker.com/_/caddy
Apache License 2.0
423 stars 75 forks source link

Allow to pass Caddyfile config via environment variable #248

Open haraldreingruber-dedalus opened 2 years ago

haraldreingruber-dedalus commented 2 years ago

Sometimes it can be convenient to pass the config via an environment variable in order to avoid mounting a config file into the docker container.

For example, I wasn't able to figure out an elegant way to pass a Caddyfile from an Azure Container Group YAML. This way I was able to avoid creating an Azure File Share that can be mounted.

Implementation

An entrypoint.sh script was added, which checks if the variable CADDYFILE is set. If it is, the content is written to ./Caddyfile. Afterward, the command passed to the container is executed.

Usage example

This PR can be tested like this with docker compose

version: "3.9"
services:
  caddy:
    image: haraldreingruberdedalus/caddy:latest
    command: 'cat Caddyfile'
    environment:
      CADDYFILE: |
        # Reverse proxy from localhost:80 to localhost:8080
        localhost:80 {
          reverse_proxy localhost:8080
        }

Maybe there are more elegant ideas to achieve this. Looking forward to your input.

hairyhenderson commented 2 years ago

I don't think that entrypoint script will correctly propagate signals to the Caddy process.

In general I would recommend either mounting the Caddyfile in, or creating a new image with the Caddyfile baked-in.

haraldreingruber-dedalus commented 2 years ago

I don't think that entrypoint script will correctly propagate signals to the Caddy process.

Thanks for the great remark.

I've looked up how entrypoint scripts can forward signals to the main process. Apparently, when using exec instead of eval caddy is not started as a child process and handles signals correctly. I've updated the PR accordingly.