caddy-docker-proxy icon indicating copy to clipboard operation
caddy-docker-proxy copied to clipboard

Is it possible to inject directives in an existing service?

Open wsw70 opened this issue 1 year ago • 4 comments

sorry -- this is a question and not an Issue but the ""Discussions" tab is not enabled"

I want to change an existing service's definitions in caddy, from another service's docker-compose. Specifically, if you take the example of wildcards in the caddy documentation

*.example.com {
	tls {
		dns <provider_name> [<params...>]
	}

	# Fallback for otherwise unhandled domains
	handle {
		abort
	}
}

This would go directly into caddy's docker-compose labels and then each new site, from its own docker-compose YAML, would need to "inject" into the configuration above blocks such as

	@foo host foo.example.com
	handle @foo {
		respond "Foo!"
	}

Is this possible?

wsw70 avatar Jul 04 '24 12:07 wsw70

Yes, labels with the same site address get merged.

francislavoie avatar Jul 04 '24 13:07 francislavoie

Yes, labels with the same site address get merged.

OK thank. I understand that you mean that when adding extra *.example.com entries in specific docker-compose files, it will be merged. If so I will try to go for configurations such as

caddy: *.example.com
caddy.@foo: host foo.example.com
caddy.handle.@foo: respond "Foo!"

and see how it goes

wsw70 avatar Jul 04 '24 13:07 wsw70

Yes, labels with the same site address get merged.

Sorry if I'm hijacking this issue, but wouldn't this mean that something like this should work? @francislavoie

services:
  example1:
    networks:
      - caddy
    labels:
      caddy: example.com
      caddy.reverse_proxy: "{{upstreams 80}}"
  example2:
    networks:
      - caddy
    labels:
      caddy: example.com
      caddy.reverse_proxy: "/api {{upstreams 8080}}"
networks:
  caddy:
    external: true

This should proxy all traffic to example.com/api to the example2 container, did I understand that correctly? Also, would this work if example1 would be in a different compose file than example2?

AlexPewMaster avatar Jul 19 '24 22:07 AlexPewMaster

Path matching is exact. /api only matches that, and not /api/foo. Use /api/* instead.

francislavoie avatar Jul 19 '24 23:07 francislavoie