0

Below are the important details:

Dockerfile for nginx build

FROM nginx:latest
EXPOSE 443
COPY nginx.conf /etc/nginx/nginx.conf

Nginx.conf

events {}
http {
  client_max_body_size 1000M;
  server {
    server_name _;
    location / {
      proxy_pass              http://127.0.0.1:8000/;
      proxy_set_header        Host $host;
    }

    listen 443 ssl;
    ssl_certificate cert/name.crt;
    ssl_certificate_key cert/name.key;
  }
}

Nginx docker command

docker run -dit -p 0.0.0.0:443:443 -v /etc/cert/:/etc/nginx/cert <MY NGINX CONTAINER> nginx -g 'daemon off;'

Docker command to start gunicorn server

docker run -dit -p 127.0.0.1:8000:8000 <My FASTAPI CONTAINER> gunicorn -w 3 -k uvicorn.workers.UvicornWorker -b 127.0.0.1:8000 server:app

Other details:

  • I expose port 8000 in my Fastapi container docker build
  • I run nginx docker command right before the gunicorn docker command
  • I am currently testing with python requests library and have turned verify=False for the SSL configuration

Edit: My issue related most directly to this post: From inside of a Docker container, how do I connect to the localhost of the machine?

Binding to 0.0.0.0:8000 for my gunicorn run and adding the tag --network="host" to my docker run nginx command solved my issue

bb197
  • 41
  • 4
  • Try running your `gunicorn` command with `-b 0.0.0.0:8000` instead. From the docker container's perspective, the port needs to be open outwards. – M.O. Nov 01 '22 at 18:33
  • Unfortunately, didn't work – bb197 Nov 01 '22 at 18:41
  • 127.0.0.1 is a special IP address, which in Docker typically means the current container (not another container or non-container processes on the host). I've linked this to two questions, one with a very similar setup using Docker Compose and a second describing the manual `docker network create`/`docker run --net` options you need running outside of Compose. – David Maze Nov 01 '22 at 18:54
  • Thank you for the links! I saw the docker compose question; it looks very similar to mine, except the resolution they came to is essentially the setup I gave to my question. The docker network question doesn't solve my core issue, but could be a workaround if I don't get this working. On the 127.0.0.1 issue -- that's helpful, thank you! I found the following question that seems related: https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach Would adding --network="host" in both my docker commands fix my issue? – bb197 Nov 01 '22 at 19:09

0 Answers0