How to write a custom load balancer for docker

Hello everyone,

For the sake of learning more about Docker network, I want to try and write a custom load balancer. I’m thinking of creating a docker network using the default bridge driver and putting a load balancer inside a proxy container for that network. I will run one or more services with multiple replicas, and the services will route traffic to each container using round robin.

I suppose that the proxy should be aware of all other instances that exist in the same network and the services they are offering in order to do load balancing. These are my ideas of doing it:

  • For every container running in the network, send a request to the proxy to inform its availability status. I find this doable, but requires customizing all the containers (to send requests periodically).
  • Make the proxy query the host for network status: I noticed that the command docker network inspect gives details about the network configurations as well as the containers running inside the network, so I think I can send this information to the proxy as well.

My question is: Which approach is preferred? Is the second approach even feasible? If yes, is there any guide or documentation I should look into for this? I have a little idea of operating systems and networking so any help to put those things together is really appreciated.

Thanks everyone in advance.