lucaslorentz / caddy-docker-proxy

Caddy as a reverse proxy for Docker
MIT License
2.86k stars 168 forks source link

Template to get container name includes leading slash #648

Open bjeanes opened 2 months ago

bjeanes commented 2 months ago

The README gives the following example to dynamically get the container name:

caddy.respond: /info "{{index .Names 0}}"
↓
respond /info "mycontainer"

Despite the example, it actually appears to correspond to respond /info "/mycontainer"

I found I had to use the following template string to get the container name, but I am not sure how one would do this outside of docker compose:

{{ index .Labels "com.docker.compose.service" }}
lucaslorentz commented 1 month ago

Yeah. All container names have a leading slash in docker. I didn't know that until now. That example in the readme is wrong.

Screenshot 2024-08-10 at 10 04 57

You can try to use some go template function to trim the leading slash. We could even add back sprig https://github.com/Masterminds/sprig to have a lot of functions available.

bjeanes commented 1 month ago

You can try to use some go template function to trim the leading slash.

I tried to find a function to do this, but I am not very familiar with Go and so couldn't confirm that there was a builtin to do this (but did also find sprig).

In any case, within docker compose, as I am, I have the workaround I posted above.