I have problem with my FastAPI based app. Locally my application is working just fine, but when trying to run it with docker, then i can’t connect with it. Also tried on different machine with Ubuntu installed, and it is working just fine as it is.
Also tried with different container using flask application on port 8080, and it is working just fine.
.
└── root_directory/
├── venv/
├── .gitignore
├── database.py
├── docker-compose.yaml
├── Dockerfile
├── main.py
├── models.py
├── README.md
└── requirements.txt
main.py file
from fastapi import FastAPI
app = FastAPI()
@app.get("/hello")
def hello_world():
return "hello"
Dockerfile
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.11
ENV PYTHONBUFFERED True
WORKDIR /app
COPY . .
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
docker-compose.yaml
version: "3.8"
services:
api:
container_name: api
build:
context: .
command: uvicorn main:app --host 0.0.0.0 --port 8000 --reload
volumes:
- ./:/app
ports:
- "8000:8000"
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: example
POSTGRES_USER: host
POSTGRES_DB: movies_app
ports:
- '5432:5432'
Logs that containers are returning
api | INFO: Will watch for changes in these directories: ['/app']
api | INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
api | INFO: Started reloader process [1] using WatchFiles
db-1 |
db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db-1 |
db-1 | 2024-01-11 02:37:33.905 UTC [1] LOG: starting PostgreSQL 16.1 (Debian 16.1-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
db-1 | 2024-01-11 02:37:33.905 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db-1 | 2024-01-11 02:37:33.905 UTC [1] LOG: listening on IPv6 address "::", port 5432
db-1 | 2024-01-11 02:37:33.908 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1 | 2024-01-11 02:37:33.912 UTC [29] LOG: database system was shut down at 2024-01-11 02:37:25 UTC
db-1 | 2024-01-11 02:37:33.914 UTC [1] LOG: database system is ready to accept connections
api | INFO: Started server process [8]
api | INFO: Waiting for application startup.
api | INFO: Application startup complete.
As you can see there are not logs that shows that client tried to connect to api server
2
Answers
So if anyone have same issue, then i found solution that is working for me.
Original answer was found here
Solution step by step:
Get into terminal of your WSL instance that docker is using
Type
ip addr show eth0 | grep -oP '(?<=inets)d+(.d+){3}'
into the shell to get ip of your WSL instanceType
netsh interface portproxy add v4tov4 listenport=8000 listenaddress=0.0.0.0 connectport=8000 connectaddress=x.x.x.x
into your terminal, where connectaddress is your WSL ip from second step, and listenport, and connectport are equal to ports that you want to bind.Remember to open terminal as administrator
Does anyone have an idea why this issue occured only with fastapi container?
EDIT: What is more interesting, I checked now that it does work if we set up different port than 8000 for uvicorn server.
TRY:
Windows terminal: docker run -p 8080:8080 mountain(for me)
https://phpout.com/wp-content/uploads/2024/03/PteGY.png
Then
http://localhost:8080/