[Answered ]-How to use nginx with docker as a reverse proxy

2đź‘Ť

You’ll need to utilize the IP of the bridge created by docker. There is a good article on Docker explaining this:
https://docs.docker.com/v1.6/articles/networking/

When Docker starts, it creates a virtual interface named docker0 on
the host machine. It randomly chooses an address and subnet from the
private range defined by RFC 1918 that are not in use on the host
machine, and assigns it to docker0.

If we take a look at the IP address assigned to docker0 (sudo ip addr show docker0) we could use this as the IP address to communicate with the host from within a docker container.

    upstream django {
 #  fail_timeout=0 means we always retry an upstream even if it failed
 #  to return a good HTTP response (in case the Unicorn master nukes a
 #  single worker for timing out).

  server IP_OF_DOCKER0:8000 fail_timeout=0;
}

I haven’t tested the above but I believe it should work. If not you may need to bind gunicorn to the docker0 IP as well.

This answer has some good insight into this process as well…
From inside of a Docker container, how do I connect to the localhost of the machine?

0đź‘Ť

A better approach might be “dokerize” the django application too, build a
network between the dokerized nginx and the dockerized django application
then expose the http port from the dockerized nginx to all interfaces.

Here is a good post about this, maybe you can take some hints from it 🙂

Leave a comment