Configuring Nginx with Unicorn Flask within Docker

I’m having an issue configuring my nginx.conf for the container (server) container.

  • Without nginx: I can properly access the app through gunicorn.
  • With nginx: I get 502 Bad Gateway

enter image description here

My first question would be, should I even have nginx on the container(server) if I have ingress nginx?

My second question is, why is this configuration not working. Here is my docker file for container (server)


FROM python:3 as builder
WORKDIR '/app'
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
ENTRYPOINT [ "gunicorn", "-b", "", "run:app" ]

FROM nginx
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf


upstream flask_server {
    server localhost:8000;

server {
    listen 80;
    server_name localhost;

    location / {
        proxy_pass http://flask_server;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


# build
docker build -t test/server .
# run
docker run -p 80:80 test/server

I suppose my issue is the upstream localhost is not working. When I’m developing locally is there no way for me to test this container specifically through docker? Or do I have to test locally with docker-compose and put nginx in a separate container?

Source: Docker Questions