question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to deploy dockerized FastAPI application to ECS fargate

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

Dockerfile:
# Image from dockerhub
FROM python:3.8.1-slim 

ENV PYTHONUNBUFFERED 1 
# Expose the port 8000 in which our application runs
EXPOSE 8000 
# Make /app as a working directory in the container
WORKDIR /app 
# Copy requirements from host, to docker container in /app 
COPY ./requirements.txt .
# Copy everything from ./app directory to /app in the container
COPY ./app . 
# Install the dependencies
RUN pip install --no-deps -r requirements.txt
# Run the application in the port 8000
ENTRYPOINT [ "uvicorn" ]
CMD ["--host", "0.0.0.0", "--port", "8000", "app.main:app"]

docker-compose.yml
version: '3.8'

services:
  web:
    restart: always
    build:
      context: .
      dockerfile: ./Dockerfile
    volumes:
      - .:/app
    ports:
      - 8000:8000

Description

docker-compose and docker build both work locally on Docker Desktop. However, when I deploy to ECS/fargate, I get the following error.

### Cloudwatch logs for ECS/Fargate Task

Traceback (most recent call last):

File “/usr/local/bin/uvicorn”, line 8, in <module> sys.exit(main()) File “/usr/local/lib/python3.8/site-packages/click/core.py”, line 829, in call return self.main(*args, **kwargs) File “/usr/local/lib/python3.8/site-packages/click/core.py”, line 782, in main rv = self.invoke(ctx) File “/usr/local/lib/python3.8/site-packages/click/core.py”, line 1066, in invoke return ctx.invoke(self.callback, **ctx.params) File “/usr/local/lib/python3.8/site-packages/click/core.py”, line 610, in invoke return callback(*args, **kwargs) File “/usr/local/lib/python3.8/site-packages/uvicorn/main.py”, line 362, in main run(**kwargs) File “/usr/local/lib/python3.8/site-packages/uvicorn/main.py”, line 386, in run server.run() File “/usr/local/lib/python3.8/site-packages/uvicorn/server.py”, line 49, in run loop.run_until_complete(self.serve(sockets=sockets)) File “uvloop/loop.pyx”, line 1494, in uvloop.loop.Loop.run_until_complete File “/usr/local/lib/python3.8/site-packages/uvicorn/server.py”, line 56, in serve config.load() File “/usr/local/lib/python3.8/site-packages/uvicorn/config.py”, line 308, in load self.loaded_app = import_from_string(self.app) File “/usr/local/lib/python3.8/site-packages/uvicorn/importer.py”, line 23, in import_from_string raise exc from None File “/usr/local/lib/python3.8/site-packages/uvicorn/importer.py”, line 20, in import_from_string module = importlib.import_module(module_str) File “/usr/local/lib/python3.8/importlib/init.py”, line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File “<frozen importlib._bootstrap>”, line 1014, in _gcd_import File “<frozen importlib._bootstrap>”, line 991, in _find_and_load File “<frozen importlib._bootstrap>”, line 961, in _find_and_load_unlocked File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed File “<frozen importlib._bootstrap>”, line 1014, in _gcd_import File “<frozen importlib._bootstrap>”, line 991, in _find_and_load File “<frozen importlib._bootstrap>”, line 973, in _find_and_load_unlocked ModuleNotFoundError: No module named ‘uvicorn app’

Operating System

Linux

Operating System Details

docker

FastAPI Version

0.65.2

Python Version

3.8.1

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

1reaction
zavbalacommented, Apr 7, 2022

Hello @schrecka, were you able to deploy it? I have many issues too

0reactions
iamthen0isecommented, Sep 7, 2021

Hi @schrecka I am confused by the error text itself:

 ModuleNotFoundError: No module named 'uvicorn app'

Is it a good practice to use entrypoint command like this? Usually I do not use ENTRYPOINTfor binary files.

ENTRYPOINT [ "uvicorn" ]
CMD ["--host", "0.0.0.0", "--port", "8000", "app.main:app"]

Would it work if you will run application as it mentioned in FastAPI docs? The example provided for RaspberryPI, but mentioned CMD command is valid for any platform.

CMD ["uvicorn", "app.main:app",  "--host", "0.0.0.0", "--port", "8000"]

Sorry if i missed something

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deploy a dockerized FastAPI application to AWS
In this article, I am going to explain step-by-step from creating a simple application with FastAPI, dockerizing it, and deploying to AWS EC2....
Read more >
Deploy FastAPI on AWS Part 2: Fargate & ALB - Elias Brange
Read on to learn how to deploy a FastAPI application on AWS Fargate behind an Application Load Balancer using AWS CDK. Repository link....
Read more >
FastAPI in Containers - Docker
When deploying FastAPI applications a common approach is to build a Linux container image. It's normally done using Docker. You can then deploy...
Read more >
Python Fastapi Project Deployment using AWS Fargate
python# fastapi #aws#fargateHi EveryOne !! This video contains a Live project with Fastapi and poetry and deployment using Aws fargate using ...
Read more >
AWS ECS Service FastAPI Project is Spamming "GET / HTTP ...
... as your application running in Fargate is returning 404 Not Found for ... health check is failing and Fargate is shutting the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found