Patryk Krawczyk
Patryk Krawczyk

Reputation: 1368

Docker and Nginx proxy_pass between containers

I'm trying to run configuration with Docker, Nginx, Gunicorn and Django.

Currently I successfully managed to run my container with Gunicorn and Django app using this command:

docker run --publish 8003:8000 user/app:latest

Now when I connect to localhost:8003 I see my application running.

At this point I would like to set up my Nginx in container to point to this app whenever I browse to localhost/app

My Nginx.conf file looks like this:

...
http {
    server {
        listen 80;

        location /app {
        proxy_pass http://127.0.0.1:8003;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }
}
...

I run it with:

docker run --publish 80:80 user/nginx:latest

This does not work and I can't wrap my head around this, thanks for any ideas how to solve this problem!

Upvotes: 0

Views: 991

Answers (1)

boyvinall
boyvinall

Reputation: 1907

The localhost IP 127.0.0.1 inside your nginx only refers internally to the nginx container. There are a couple of solutions to this, either:

  1. The simple thing is to run your nginx container in "host-mode networking" mode. At this point, 127.0.0.1 actually refers to your container host and it should all be good. See the docs, but basically just adding --network="host" should work. The downside with this simplicity is that it's slightly less secure.

  2. Alternatively, you can use "linked" containers, see the docs where you want the --link option. This way, from inside the nginx container you can use DNS resolution to access a different container, so you'd update your proxy_pass to the linked name. As a side note, doing this from docker-compose makes things considerably easier.

Upvotes: 1

Related Questions