Multiple Caddyfiles without import

1. Caddy version (caddy version):

v2.5.1

2. How I run Caddy:

Caddy in Docker

a. System environment:

Mac OS 12.4 with Docker Desktop 4.9.0, Docker Engine 20.10.16, Compose 1.29.2

b. Command:

docker-compose up -d

c. Service/unit/compose file:

version: "2.2"
                
services:
  caddy:
    container_name: 'caddy-cluster-proxy'
    image: 'caddy:2.5.1-alpine'
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - caddy_data:/data
      - ./:/etc/caddy/
    command: caddy run --config /etc/caddy/*Caddyfile --watch
                
volumes:
  # Cached caddy data, e.g. certs
  caddy_data:

d. My complete Caddyfile or JSON config:

Caddyfile:

:80 {
	respond "Hello, world!"
}

https.Caddyfile:

:443 {
	"Hello world, HTTPS!"
}

3. The problem I’m having:

I want to configure Caddy to pick up all the Caddyfiles in a directory, and read config from all of them. I understand I can do this with imports but for flexibilities sake I’d like to do it without.

4. Error messages and/or full log output:

run: reading config file: open /etc/caddy/*Caddyfile: no such file or directory

5. What I already tried:

I read Organizing Sites into multiple Caddyfiles - #3 which indicates I can do this with -conf '/path/to/*.caddy'. This didn’t work; perhaps these instructions are for Caddy 1? I tried --config /etc/caddy/*Caddyfile but got the “no such file or directory” error above.

Is it possible, in Caddy 2, to load multiple Caddyfiles without using imports?

Probably. That’s not supported in Caddy v2.

You must use import if you want to use multiple files, with a primary config importing the rest.

Thanks, with your help I resolved it with a Caddyfile like this:

import "*.Caddyfile"

:80 {
	respond "Hello, world!"
}

Some notes for anyone else down this track:

  • Make sure you don’t set up a circular import loop - e.g. import "*Caddyfile" in a file called Caddyfile will create an infinite loop, but using import "*.Caddyfile" won’t, you just have to name your other files foo.Caddyfile and so on.
  • The wildcard import works fine even if you have no files that match the pattern!

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