menu

Questions & Answers

FastAPI served through NGINX with gunicorn and docker compose

I have a FastAPI API that I want to serve using gunicorn, nginx and docker compose.

I manage to make the FastApi and Gunicorn work with docker compose, now I add nginx. But I cannot manage to make it work. When I do curl http://localhost:80 I get this messsage: If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

So this is my docker compose file:

version: '3.8'

services:
  web:
    build:
       dockerfile: Dockerfile.prod
       context: .
    command: gunicorn main:app --bind 0.0.0.0:8000 --worker-class uvicorn.workers.UvicornWorker
    expose:
      - 8000
    env_file:
      - ./.env.prod
  nginx:
   build:
      dockerfile: Dockerfile.prod
      context: ./nginx
   ports:
    - 1337:80
   depends_on:
    - web

On this one, if I set ports to 80:80 I get an error when the image is composed: Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use, which I don't know why.

If I put [some random number]:80 (e.g. 1337:80) then the docker build works, but I get the If you see this page, the nginx web server is successfully installed but... error message state before. I think 1337 is not where nginx is listening, and that's why.

This is my nginx conf file:

upstream platic_service {
    server web:8000;
    }

server {

    listen 80;

     location / {
        proxy_pass http://platic_service;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }

I tried to change it to listen to 8080 but does not work.

What am I doing wrong?

Comments:
2023-01-18 00:05:12
Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use will likely be cuased by somethng already running on port 80 on your server. some servers come with a webserver already built in and started like apache on the host where your trying to run the docker compose command so you cant map the local port80 to the container
2023-01-18 00:05:12
But i understand that the first number in ports is the internal port, ie the one that is inside the container. Inside that container I just have nginx.
Answers(0) :