1

I have a React app that uses Flask as a back-end. I am trying to move the react app to be served from an Nginx server. Both the Nginx and the Flask server are inside docker containers. The static content works fine but when the site tries to proxy a call to the flask server the connection is refused.

I don't see any logs on the Flask server so I don't think the requests are getting into the Flask container. I've tried changing the proxy to hit a different port and got the same issue. Shouldn't the containers be allowed to communicate with each other or do I have to explicitly allow certain ports to be open? Do I need a WSGI server between them?

Nginx default.conf

server {

   listen        80;
   server_name   localhost;

   location / {
      index      index.html;
      root       /usr/share/nginx/html;
   }

   location /api/ {
      proxy_pass http://flask:5000;
      proxy_http_version 1.1;
      proxy_set_header Connection "";
      proxy_set_header Host "localhost";
   }

}

docker-compose.yml

version: "3"
services:
    nginx:
        image: nginx
        volumes:
            - /home/cookery/Cookery/dining/build:/usr/share/nginx/html
            - /home/cookery/Cookery/docker/docker.conf:/etc/nginx/conf.d/default.conf
        ports:
            - "8080:80"
    flask:
        build: ../kitchen
        ports:
            - "5000:5000"

This is the error I get on the request to the flask server

2020/01/15 21:18:08 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.26.0.1, server: localhost, request: "GET /api/recipes/all HTTP/1.1", upstream: "http://172.26.0.2:5000/api/recipes/all", host: "localhost:8080", referrer: "http://localhost:8080/"

1 Answers1

3

Sounds like Flask is listening on 127.0.0.1, which means "the local machine". Each container has its own 127.0.0.1, so nginx is connecting to its container 127.0.0.1, which is not the same as the one Flask is listening to.

The solution is to have Flask listen on 0.0.0.0.

See https://pythonspeed.com/articles/docker-connection-refused/ for more details.

Itamar Turner-Trauring
  • 3,430
  • 1
  • 13
  • 17