Set Djangos ALLOWED_HOSTS dynamically
Problem
When using the IDL in a Portainer environment (see orchestration!317 (merged)), the service is requested by the Traefik reverse proxy by its IP address, not by its Docker service name. As the IP addresses change depending on the order of the startup of the containers, the only solution is to add a range of IP addresses manually, as Django doesn't support netmask-style notation for subnets.
Solution
On Stack Overflow there is a simple solution to add all of the currently assigned IP addresses of the host to ALLOWED_HOSTS
:
>> from socket import gethostname, gethostbyname, gethostbyname_ex
>>> ALLOWED_HOSTS = [ gethostname(), ] + list(set(gethostbyname_ex(gethostname())[2]))
>>> print(ALLOWED_HOSTS)
['f07bb0b4c22a', '172.24.0.3', '172.23.0.5', '172.21.0.2']
In this case, 172.23.0.5
is the address which is used by the reverse proxy to access the IDL service.