Home>

Trying to run a test project on Fastapi. on Ubuntu 20.04 C. Docker. . I use the docks.

DockerFile

From Python: 3.7
Run Pip Install Fastapi Uvicorn
Expose 8080.
Copy ./App /App
Cmd ["uvicorn", "app.main: app", "--host", "0.0.0.0", "--port", "8080"]

main.py

from typing import optional
From Fastapi Import Fastapi
App= Fastapi ()
@ app.get ("/")
DEF read_root ():
    Return {"Hello": "world"}
@ App.Get ("/Items /{Item_ID}")
Def Read_Item (Item_ID: INT, Q: Optional [StR]= None):
    Return {"Item_ID": Item_ID, "Q": Q}

Create Docker Image so Docker Build -t MyImage. . Then run in the container Docker Run -D --Name Mycontainer -P 80:80 MyImage . I get a mistake

docker: Error response from daemon: driver failed programming external connectivity on endpoint mycontainer (8c74a07c055929891d80994584452fde936a76d690ed60150d134838c0a50b59): Error starting userland proxy: listen tcp4 0.0.0.0:80: bind: address already in use.

If you change the port when the container is started on Docker Run -D --Name MyContainer -P 8000: 8000 MyImage . The container is created, but at the address 127.0.0.1:8080/docs. Page does not open what I do wrong? The container is running, but for some reason the port is not specified. And why the container immediately completes the container.

Container ID Image Command Created Status Ports Names
62740AB0782B MyImage "Uvicorn App.Main: AP ..." 11 Minutes Ago exited (1) 11 minutes Ago MyContainer

I do not understand much in deploying the server, but didn't the dokarterfile need to write the Uvicorn MAIP command: app?

Michael Tetelev2021-09-04 15:00:07

@Michaeltelev Well, in the docks it is written that it is not necessary. Yes, even if you write this nothing changes

Вадим2021-09-04 15:00:07

The 80th port is already busy, and it is impossible not to root it. When you use 8080, see the Docker Logs Container-ID container logs, there is probably an error message.

Roman Konoval2021-09-04 15:00:07

@Romankonoval Yes, writes Cannot Assign Requested Address Docker

Вадим2021-09-04 15:00:07

Try listening to Localhost, i.e. Specify "--host", "127.0.0.1" in the start parameters. Well, the error message should be written into the text of the question, and not in the comment, since this is part of the question.

Roman Konoval2021-09-04 15:00:07