Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to tell nginx to look up a container's IP dynamically so it can be restarted without needing to also restart nginx container? #3314

Closed
blah238 opened this issue Apr 12, 2016 · 4 comments

Comments

@blah238
Copy link

blah238 commented Apr 12, 2016

I am using Compose 1.7.0-rc2 and the V2 compose file format. No explicit network defined, just the default networking.

I have a backend container running Node.js and an Nginx container set up to proxy_pass requests to the backend container.

This all works fine except if I restart the backend container. Then it seems the backend's IP changes, but Nginx continues to use the old IP.

I have gone down the rabbit hole of trying to figure out how to get Nginx to resolve the IP dynamically, but the complexity skyrockets quickly, and nothing specific to V2 compose files has apparently been written.

Is there a simple and up-to-date way of accomplishing this apparently monumental task?

@dnephin
Copy link

dnephin commented Apr 13, 2016

I believe this is a known issue with nginx. It caches the DNS queries when it starts and never tries them again. This is why people have been developing alternative proxies/load balancers for use with containers. One example is https://github.com/containous/traefik

@blah238
Copy link
Author

blah238 commented Apr 13, 2016

Thanks for the tip @dnephin, it looks interesting. At first glance it doesn't seem to support serving static files though. I suppose there is probably something else to fill that gap as well though. I suppose I could just continue to use nginx for that.

@blah238
Copy link
Author

blah238 commented May 13, 2016

Well this is the simplest thing I could come up with. Not dynamic or scaleable in any way, I just set a static IP for the backend:

version: '2'
services:
  backend:
    command: node app.js
    networks:
      app_net:
        ipv4_address: 172.18.0.2
  nginx:
    build: ./nginx
    ports:
     - "80:80"
    networks:
      - app_net
networks:
  app_net:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.18.0.0/16
          gateway: 172.18.0.1

Now restarting the backend doesn't require restarting Nginx as well.

Hoping for a nicer pattern in the future!

@blah238 blah238 closed this as completed May 13, 2016
@regisb
Copy link

regisb commented Mar 12, 2020

If anyone later stumbles on this issue, as I did, trying to fix container restarts with docker-compose: as of v1.1.9, nginx now offers a valid argument to the resolver directive which can be used to cache the name resolution for a shorter time: http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver

This is not exactly "dynamic resolution" but it's close enough.

pingyen pushed a commit to pingyen/vexed.me that referenced this issue Jun 30, 2024
Restarting a Docker service can change the service's IP address. However, Nginx will not perform DNS resolution again, so using a static IP address can avoid this problem.

docker/compose#3314
pingyen added a commit to pingyen/vexed.me that referenced this issue Jul 4, 2024
Restarting a Docker service can change the service's IP address. However, Nginx will not perform DNS resolution again, so using a static IP address can avoid this problem.

docker/compose#3314
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants