skip to Main Content

I am trying to run a simple FastAPI docker container. My only requirement is that I need the redis module.

Here is my Dockerfile

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8

COPY ./app /app

CMD pip install -r /app/infrastructure_req.txt

According to the log, the pip install was successful

Collecting redis==3.5.3
  Downloading redis-3.5.3-py2.py3-none-any.whl (72 kB)
Installing collected packages: redis
Successfully installed redis-3.5.3
WARNING: You are using pip version 20.0.2; however, version 20.2.3 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Requirement already satisfied: redis==3.5.3 in /usr/local/lib/python3.8/site-packages (from -r 
/app/infrastructure_req.txt (line 1)) (3.5.3)
WARNING: You are using pip version 20.0.2; however, version 20.2.3 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Requirement already satisfied: redis==3.5.3 in /usr/local/lib/python3.8/site-packages (from -r 
/app/infrastructure_req.txt (line 1)) (3.5.3)
WARNING: You are using pip version 20.0.2; however, version 20.2.3 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.

However, the docker container quits immediately after the CMD.

if I attempt to build the image without the pip install, I get this error instead

# Let the DB start

sleep 10;

# Run migrations

alembic upgrade head


[2020-10-01 19:08:24 +0000] [1] [INFO] Starting gunicorn 20.0.4

[2020-10-01 19:08:24 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)

[2020-10-01 19:08:24 +0000] [1] [INFO] Using worker: uvicorn.workers.UvicornWorker

[2020-10-01 19:08:24 +0000] [7] [INFO] Booting worker with pid: 7

{"loglevel": "info", "workers": 2, "bind": "0.0.0.0:80", "graceful_timeout": 120, "timeout": 120, 
"keepalive": 5, "errorlog": "-", "accesslog": "-", "workers_per_core": 1.0, "use_max_workers": null, 
"host": "0.0.0.0", "port": "80"}

[2020-10-01 19:08:25 +0000] [1] [INFO] Shutting down: Master

[2020-10-01 19:08:25 +0000] [1] [INFO] Reason: Worker failed to boot.

{"loglevel": "info", "workers": 2, "bind": "0.0.0.0:80", "graceful_timeout": 120, "timeout": 120,
"keepalive": 5, "errorlog": "-", "accesslog": "-", "workers_per_core": 1.0, "use_max_workers": null, 

"host": "0.0.0.0", "port": "80"}

I am unclear what to do to resolve this.

4

Answers


  1. Chosen as BEST ANSWER

    First off I want to thank everyone who replied. By hacking and doing a little inferencing I was able to solve my issue.

    As a little context for someone who comes by this post in the future. I am running Windows 10 and Docker for Desktop but I am creating Linux images. I am specifically using the tiangolo/uvicorn-gunicorn-fastapi because it is supposed to be tuned for production.

    Here is my Dockerfile

    FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8
    
    COPY ./app /app
    
    RUN pip install --upgrade pip
    RUN ls -agl
    RUN pip install -r requirements
    

    The second run command is not necessary but it made sure that my files and folders are being correctly copied. Thanks to those who responded regarding CMD vs RUN. That was (1) of my mistakes. Also thanks @The Fool who let me know files can be mounted. That was not my issue but this will be handy when I am developing locally on my laptop. If I set FastAPI to reload when it detects a new file this will make development a whole lot faster.

    Ultimately I had two problems but for whatever reason, the error message(s) was not showing up in the docker logs but instead just blew up gunicorn:

    Problem #1 My directory structure and files are as follows:

    app/
     |
     -- __init__.py
     -- main.py
     --log_settings/
        |
        |--__init__.py
        |--logging_config.py
        |--loggers.py
    

    Because of how my dev environment was setup, my main.py had this code

    from app.log_settings import loggers
    

    Which is incorrect and should be:

    from log_settings import loggers
    

    Problem #2: My code has dependency of psutils. For whatever reason, psutils was not being installed. I have no idea why. I used pip freeze and kept getting issues. I finally deleted the line and then just had entered psutils at the very bottom of the list of requirements

    This was a bear to debug because there was no really good error messages in the docker logs.

    I started by creating a simply hello world and then adding some of my code a little at time by copying and pasting over until it blew up again. Then I examined the issue, fixed and moved on to the next problem. Figuring out the psutils was not being installed and fixing that issue lead me to a good error message to fix the path problem.

    I have had issues with uvicorn and gunicorn in the past but this is the first time I did not get messages that were telling me what the hell was going on. I mean that is just a cryptic mess what was being shown.

    Thanks again for all those that responded


  2. Check your dockerfile. The last command you issue is the pip installation. The installation works and since following there is no other command, it simply shuts down.

    The dockerfile should look as follows

    FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8
    
    COPY ./app /app
    
    CMD pip install -r /app/infrastructure_req.txt
    
    CMD uvicorn --host=0.0.0.0 app.main:app # or wherever your FastAPI app is located and named
    
    Login or Signup to reply.
  3. Typically my Dockerfiles for FastAPI would have something like:

    RUN pip install -r /app/infrastructure_req.txt
    
    CMD uvicorn --host=0.0.0.0 app:app
    

    You can only have one CMD as the main entrypoint, so you need to install your pip modules using RUN

    Login or Signup to reply.
  4. This is how i use fastapi with docker.

    FROM python
    
    COPY ./requirements.txt /
    RUN pip install -r requirements.txt
    
    WORKDIR /app
    
    CMD ["uvicorn", "main:app", "--reload"]
    

    And then I start it with

    docker run --rm -d -ti --name fastapi -v $PWD/python-api/app:/app fastapi
    

    If you don’t want to mount your code you can copy it in the build instead.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search