I am new to computer networks and Docker, and I'm trying to deploy and run a small FastAPI app using Docker. The app works successfully when I run it locally with Uvicorn, but it fails to run when I try to deploy it using Docker. Below is the error message. My OS is windows. I receive:
src/main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
Here is my Dockerfile
FROM python:3.10-slim
WORKDIR /code
COPY ./requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY ./src ./src
CMD ["uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "80", "--reload"]
Here are the commands I used in the terminal:
docker build -t fastapi-image .
docker run --name fastapi-container -p 80:80 fastapi-image
here is my docker log:
INFO: Will watch for changes in these directories: ['/code']
INFO: Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)
INFO: Started reloader process [1] using StatReload
INFO: Started server process [8]
INFO: Waiting for application startup.
INFO: Application startup complete.
my file structure:
requirements.txt
Dockerfile
src
|--main.py
requirements.txt
fastapi
uvicorn
By replicating the issue, can see the result at localhost:80
.
See also these posts to know more about the differences between localhost
and 0.0.0.0
: