Connection refused: when uwsgi and nginx in different containers


I am trying to setup two docker containers(yes separate without docker-compose): one with nginx and one with uwsgi with basic flask app.
I run containers in same network within docker
My nginx config for site added/linked to sites-enabled(everything else is default):

server {
    listen 80;

    location / {
        include uwsgi_params;

My uwsgi.ini

module = app:app

master = true
processes = 2

socket =

uwsgi entry point in docker looks like

.local/bin/uwsgi --ini uwsgi.ini

Containers run fine on their own – uwsgi receives request on 8080 and nginx receives expected requests. How ever when I try to access i get 502 status code and nginx logs error:

1 connect() failed (111: Connection refused) while connecting to
upstream, client:, server:, request: "GET /
HTTP/1.1", upstream: "uwsgi://", host: ""

By googling i find solution that rather use one container and some_socket.sock as file or use docker compose. Apparently problem with permissions, but I do not know how to solve them or diagnose.

I launch containers with these commands:

docker run --network app_network --name nginx --rm -p 80:80 my_nginx
docker run --network app_network --name flaskapp --rm -p 8080:8080 my_uwsgi



You can simply use the hostname of the docker container in the uwsgi_pass directive as both docker containers are on the same subnet.

location / {
        include uwsgi_params;
        uwsgi_pass flaskapp:8080;
    } isn’t the IP address of the server, it essentially tells the server to be hosted on every IP that the device has allocated.

To connect to it from nginx, you will need to use the IP address of the container instead.

You can find the IP address of the container running uWsgi with the following command:

docker inspect CONTAINER_ID

Where CONTAINER_ID is the ID of the container you started uwsgi in.

From here you can update the nginx config as follows:

uwsgi_pass IP_ADDRESS:8080;

Where IP_ADDRESS is the one you found from the command above

You can also set the ip address of the container when you start it with the following option

--ip <ip>

Be careful, however, to ensure that the IP address you set is in the same subnet as the standard IP’s assigned.

Answered By – HPringles

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published