tiangolo / uvicorn-gunicorn-fastapi-docker

Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python with performance auto-tuning.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to develop ?

yellow-sock opened this issue · comments

Hi,

I like to develop within the docker container so that I use the same environment that is used later in prod.

I run:
docker run -it --rm -v /c/Users/myuser/chat/api:/app -p 8090:8000 my-image-name:0.0.2 uvicorn main:app --reload

So I map using -v my local directory inside the container. When I change a file locally the reloader within docker detecs it and restarts the application. That works fine.
But if I try to connect to http://localhost:8090/ I get a "The connection was reset" message in the webbrowser (firefox).

Uvicorn logs that its running on http://127.0.0.1:8000 so -p 8090:8000 should work - I thought :)

my-image-name:0.0.2 is based on tiangolo/uvicorn-gunicorn-fastapi:python3.8-slim and basically only installs some python packages and then does a COPY /api /app
I develop on Windows 10 using WSL (not WSL2).

Does anyone else had this issue ?

Thanks!

Can you log in to your container and do curl 127.0.0.1:8000/docs? What's the response?

Hi rizkyarlin,

Running
docker run -it --rm -v /c/Users/myuser/chat/api:/app -p 8090:8000 my-image-name:0.0.2 uvicorn main:app --reload

and then in another console window:
docker exec -it b426a81143eb bash
apt-get install curl

then I get:

root@b426a81143eb:/app# curl 127.0.0.1:8000/docs

    <!DOCTYPE html>
    <html>
    <head>
    <link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css">
    <link rel="shortcut icon" href="https://fastapi.tiangolo.com/img/favicon.png">
    <title>Virgin Chat REST API - Swagger UI</title>
    </head>
    <body>
    <div id="swagger-ui">
    </div>
    <script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js"></script>
    <!-- `SwaggerUIBundle` is now available on the page -->
    <script>
    const ui = SwaggerUIBundle({
        url: '/openapi.json',
    oauth2RedirectUrl: window.location.origin + '/docs/oauth2-redirect',
        dom_id: '#swagger-ui',
        presets: [
        SwaggerUIBundle.presets.apis,
        SwaggerUIBundle.SwaggerUIStandalonePreset
        ],
        layout: "BaseLayout",
        deepLinking: true
    })
    </script>
    </body>
    </html>
    root@b426a81143eb:/app#

So the server seems to run fine.

One thing to note is that if I run http://localhost:8080/docs/ on my local pc in firefox I get an immediate "The connection was reset" message. If fast api is down it takes a while and then I get an "Unable to connect".

Thanks for your help.

can you do docker ps and send the screenshot?

Here is the output.

ubuntu@DESKTOP-IT5DHUK:/c/Users/guest2/$ docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                               NAMES
e9f5a34cc8ac        virgin-chat-api:0.0.5   "uvicorn main:app --…"   19 seconds ago      Up 18 seconds       0.0.0.0:8090->80/tcp                virgin-chat-api-container-0.0.5
f636fae7c0d6        mysql:5.7               "docker-entrypoint.s…"   3 weeks ago         Up 6 hours          0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-container-5.7

Can I ask you if you run
docker run -it --rm -p 8090:8000 tiangolo/uvicorn-gunicorn-fastapi:python3.8-slim uvicorn main:app on your computer can you connect to http://localhost:8090 ?

ah you don't run it as sudo... if you run sudo docker ps you can check the port mapping.

docker run -it --rm -p 8090:8000 tiangolo/uvicorn-gunicorn-fastapi:python3.8-slim uvicorn main:app on your computer can you connect to http://localhost:8090 ?

Yes

One more thing, try to run the uvicorn with --host option uvicorn main:app --host 0.0.0.0

BINGO .. that worked

I can connect to

docker run -it --rm -p 8090:8000 tiangolo/uvicorn-gunicorn-fastapi:python3.8-slim uvicorn main:app -
-host 0.0.0.0

But without "-host 0.0.0.0" it doesnt work. I am not sure what it does but it does work.

For others: I am using WSL (version 1) under Windows 10.

Thanks a lot for your help rizkyarlin.

the default allowed host for uvicorn (and other servers like gunicorn, live-server, etc) is 127.0.0.1 which means it can only be accessed from that host. But when we change it to 0.0.0.0, it means everyone can access it. Glad I can help!

Thanks for the help here @rizkyarlin ! 👏 🙇

If that solves the original problem, then you can close this issue @yellow-sock ✔️

Now closed .. thanks again