How can I configure Nginx to dynamically proxy requests to Docker containers that change IP addresses?

Responsive Ad Header

Question

Grade: Education Subject: Support
How can I configure Nginx to dynamically proxy requests to Docker containers that change IP addresses?
Asked by:
102 Viewed 102 Answers

Answer (102)

Best Answer
(492)
For dynamic proxying, Nginx can be configured to use DNS resolution for upstream servers. Instead of hardcoding container IPs, you can use service names (e.g., `http://my-app-service:8080`) if using Docker's built-in DNS or a service discovery tool. Nginx will then resolve these names to the current IP addresses of the containers when it needs to forward requests. Tools like `docker-gen` can also be used to automatically regenerate Nginx configuration files when containers start or stop.