Getting access to FastAPI (uvicorn) running in a docker container with Elastic Beanstalk

0

I've been trying the whole day to properly set up my application, but alas without success. I've built a FastAPI app that is currently running without problems on DigitalOcean and getting deployed from gitlab. After trying to set everything up for a move to AWS, I'm stuck. I managed to have gitlab build and deploy a container to ECR and get that container up and running in Elastic Beanstalk. I can see in the docker logs that the app properly loads. The problem is that nginx just returns a 502 bad gateway error and I can't seem to figure out how to configure it.

I've now updated my custom_proxy file to make it work and I'm seeing if I'm able to get EBS to use it instead of the default one. Here's the file:

events {}

http {
  upstream uvicorn {
    server 172.17.0.2:5000;
  }

  server {
    listen 80;

    location / {
      proxy_pass http://uvicorn;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
    }
  }
}

Using the internal IP of the docker container was part of the breakthrough.

  • Are you using the Docker platform branch or ECS platform branch?

  • Hi John, I'm using the Docker platform branch.

asked a year ago1637 views
2 Answers
1

Sounds like maybe this might help? https://dev.to/devasservice/the-cheap-way-how-to-use-docker-to-deploy-your-fastapi-kmn Specifically this part:

upstream app server, contains the definition of our API endpoint, as you can see if references the url ‘api:8080’ (api is our other docker instance name from the docker-compose file, this allows cross communication between instances)

You could try changing server 172.17.0.2:5000; to server api:5000; or whatever that container is called.

answered a year ago
  • I was using docker instead of api before and that should have worked too. I think the important part from the link you shared is adding a line to copy the nginx.conf file within the Dockerfile. I'll try that for the next "release".

  • Glad to hear...please let me know if that fixes it.

1

So, for now I have given up on setting a specific configuration for nginx inside the instance. Instead, I changed the uvicorn command to run the server listening on port 80. That gets picked up by the default nginx config of AWS and now my app is open to the world!

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions