Auto Restart Caddy in Docker Container when configuration changes

1. Caddy version:

v2.6.2

2. How I installed, and run Caddy:

Official Docker Image

a. System environment:

Official Image under Docker under Debian 11 (under Proxmox VM in amd64 architecture)

b. Command:

caddy run --config /etc/caddy/Caddyfile --adapter caddyfile

c. Service/unit/compose file:

d. My complete Caddy config:

3. The problem I’m having:

Not a problem. I would like to know if it is possible to automatically restart Caddy when I change the config, i.e. the Caddyfile.

4. Error messages and/or full log output:

No errors

5. What I already tried:

There are other recommendation for this results but they are for Caddy run directly on a host machine and use systemd. I can’t configure this on the host machine, so I’m searching for a solution at the container level.

Actually I’m killing and restarting caddy manually

6. Links to relevant resources:

See the docs:

You can just run docker-compose exec -w /etc/caddy caddy caddy reload to reload your config.

Hello, thanks for reply, but this is not automatic.

You can use the --watch option to have Caddy continually check for changes to the config file. You have to override the command Caddy runs with in the container for this though.

Ok, this option will trigger a “graceful reload” with no downtime and rollback in case of configuration file error?

Yep. It’s not exactly a rollback though, because the new config is not actually active until after it’s loaded successfully. The old config continues to run until after the new config is already started. See Architecture — Caddy Documentation

The caddy reload command that I mentioned earlier does it the same way.

The thing is that --watch is not optimal for performance because it uses polling to check if the config file changed. That means there will always be some CPU/disk activity from Caddy on an interval to check if the config changed. This is minor, but it might matter depending on your needs.

Thanks. I understand. Btw this way seems suitable for my current development/test phase.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.