lucaslorentz / caddy-docker-proxy

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

Allow empty services and shutdown containers to be registered #527

Closed acouvreur closed 1 year ago

acouvreur commented 1 year ago

This issue is a feature request to have a property for this project that could change its scanning behavior.

The arguments could be something like:

This feature is open so this issue: https://github.com/acouvreur/sablier/issues/170 could be working with Caddy and your project.

This allows auto shutdown of containers after a period of inactivity.

Currently, I assume that this project only scans for running containers and swarm services which have at least one replicas.

This is based on this:

https://github.com/lucaslorentz/caddy-docker-proxy/blob/82f17b9273c3a644cd3a9a935101d3b1f3b9d8b3/generator/generator.go#L120-L144

accforgithubtest commented 1 year ago

@lucaslorentz - this would be a great feature, as this prevents useful self-hosting tools like sablier and containernursery from being used along with caddy-docker-proxy.

Currently, for example, container nursery is able to stop a container after x mins of inactivity. Once the container is stopped however they do not receive any new requests to the service, to bring it back up, as caddy-docker-proxy doesnt forward the request to containernursery anymore.

Having this feature in caddy-docker-proxy means that for self-hosting a bunch of applications, it could help with minimizing utilisation / power consumption, and allow many more sparsely used services to be hosted on smaller machines - which would be great win !

lucaslorentz commented 1 year ago

I wonder if it was confirmed that we don't scan stopped containers or services without tasks, I'm not aware of anything in our code that filters them out. Do we need different calls or parameters to docker API to fetch them?

accforgithubtest commented 1 year ago

@lucaslorentz - I can confirm that the requests are not being forwarded once a container is stopped. Would you want me to share a docker compose setup that you can check it out with ?

lucaslorentz commented 1 year ago

I've confirmed it doesn't scan stopped containers as well. I guess the fix for that is just to replace

dockerClient.ContainerList(context.Background(), types.ContainerListOptions{})

With:

dockerClient.ContainerList(context.Background(), types.ContainerListOptions{All: true})

@accforgithubtest Have you confirmed we have the same problem with services?

accforgithubtest commented 1 year ago

@lucaslorentz - I am not a developer, but have been learning docker to help with setting up a few self hosted services.

I assume you are asking if the docker compose applications are under the services section. If thats your question, then yes I can confirm it doesnt scan stopped containers.

Everything I have in my docker compose is under services

version: "3"
services:
  pihole:
    ...
  caddy:
    ...
  glances:
    ...
lucaslorentz commented 1 year ago

Yes, what I mean by service is docker swarm services. docker compose up just creates containers. docker stack deploy is what creates swarm services.

I just tested it following the readme:

docker stack deploy -c examples/standalone.yaml caddy-docker-demo

Then scaling down all services:

docker service scale caddy-docker-demo_echo_0=0 caddy-docker-demo_whoami0=0 caddy-docker-demo_whoami1=0 caddy-docker-demo_whoami2=0 caddy-docker-demo_whoami3=0

It works for services, it still scans services and its labels. It generates reverse_proxy directives without upstreams, as expected.

accforgithubtest commented 1 year ago

docker compose up just creates containers. docker stack deploy is what creates swarm services.

Oh ok, thanks for that explanation.

I just use docker compose up, so only containers and hence I run into the issue you have mentioned above.

accforgithubtest commented 1 year ago

I can confirm with my test that this is now fixed ! Thanks for a very quick fix @lucaslorentz !