Importing files

Hi all,

Thanks for creating Caddy!

1. The problem I’m having:

Trying to import files or snippets from other files into the main Caddyfile, without success.
The incentive is to use separate files (or even import specific snippets from external files) to allow extra functionality without bloating the main Caddyfile.

The docs are a little dull around importing files (import (Caddyfile directive) — Caddy Documentation)
I’ve tried several approaches:

  • Tried to import a specific filename.
  • Tried to import a folder with foldername/* with a few files in it.
  • Tried to create snippets in other files and import them to the main Caddyfile.

2. Error messages and/or full log output:

I’m usually getting either of those errors -

Error during parsing: File to import not found: methods.conf
No files matching import glob pattern","pattern":"folder/*"

3. Caddy version:

2.7.4-alpine

4. How I installed and ran Caddy:

a. System environment:

Docker compose:

CMD ["caddy", "run", "--config", "Caddyfile"]

b. Command:

docker compose up --build

c. Service/unit/compose file:

version: "3"
networks:
  caddy:
services:
  caddy:
    image: caddy:2.7.4-alpine
    env_file:
      - .env.local
    extra_hosts:
      - "host.docker.internal:host-gateway"
    restart: unless-stopped
    build:
      context: ./
      args:
        CADDYFILE: 'Caddyfile'
    ports:
      - "8000:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./site:/srv
      - caddy_data:/data
      - caddy_config:/config
    networks:
      caddy:

volumes:
  caddy_data:
  caddy_config:

d. My complete Caddy config:

My folder structure looks like this:

= folder
== methods.config # file
= Caddyfile
# also tried placing the methods.config file here in the root folder.
{
	debug
	local_certs
	admin off
}

:80 {
	log
	respond /health-check "healthy" 200
}

:443 {
	log
	import folder/*
#### also tried: 
# import methods.conf
	
	tls {
		on_demand
	}

	reverse_proxy {
    to {$DOMAIN}
    header_up Host {$DOMAIN}
    header_up Domain {http.request.host}
  }

}

The content of the methods.conf file is currently:

  @blockDelete method DELETE POST PATCH PUT
  respond @blockDelete "Unauthorized" 405

I’ve tried different type of content within the file (snippets, handlers etc.) and also to move the import around or change the folder/file structures a few this, but the above should be the minimum reproducible example

No, you asked a support question. The template is important and required.

Without seeing exactly what you tried, and what logs you got, etc, then it’s impossible to help.

1 Like

Sorry, you’re absolutely right. Just added the required info above

You’ve only mounted your Caddyfile to the container, no other files. The other config files need to be in the container as well.

1 Like

Wow, thank you so much for seeing that! I’m a little embarrassed :slight_smile:
Was missing the mount of the external folder/files

volumes:
    - ./:/folder-name

Quick follow up question - is everything allowed in those external files in terms of Caddy functionality? Any best practices or pitfalls?

Really appreciate your help @francislavoie!!

import is just a fancy copy-paste. You can use it anywhere. It will not override other config, or whatever, it’s just an inline token replacement at the position import appears, before actually parsing the config text.

1 Like

Understood. Thanks again for the help!

1 Like

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