因此,我有一个包含两个容器的 Docker Compose 构建。在 Windows 上运行它。
- 一个容器包含一个 postgres 数据库,它的端口映射到主机,一切正常。
- 另一个包含 FastAPI 服务器,也有一个映射端口。但我无法从主机访问它。
我确信,该服务器运行良好,我看到日志,我可以进入容器并使用curl检查它:
# curl 127.0.0.1:8000
{"detail":"Not Found"}
这是FastAPI的默认答案,所以服务器很好。
这是我的 docker-compose.yaml
version: '3.8'
services:
api_service:
build: ./api_service/
volumes:
- ./api_service/app:/app/app
ports:
- 5003:8000
depends_on:
- api_db
env_file:
- config.env
- api_db_config.env
api_db:
image: postgres
ports: # for debug purpuses
- 5005:5432
volumes:
- ./api_db/postgres_data:/var/lib/postgresql/data/
- ./api_db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d/
env_file:
- api_db_config.env
我想可能是主机端口被占用,所以我用 TCPView 检查了端口。
看来,由于某种原因,端口无法接受连接,但我不知道为什么以及如何修复它。任何帮助将不胜感激。
So, I have a Docker Compose build with two containers. Run it on Windows.
- One container contains a postgres DB, it has it's port mapped to the host machine and everything works properly.
- Another one contains FastAPI server and also has a mapped port. But I can't reach it from the host machine.
I know for sure, that server is running fine, I see logs, I can go into the container and check it with curl:
# curl 127.0.0.1:8000
{"detail":"Not Found"}
That's a default answear from FastAPI, so server is fine.
Here is my docker-compose.yaml
version: '3.8'
services:
api_service:
build: ./api_service/
volumes:
- ./api_service/app:/app/app
ports:
- 5003:8000
depends_on:
- api_db
env_file:
- config.env
- api_db_config.env
api_db:
image: postgres
ports: # for debug purpuses
- 5005:5432
volumes:
- ./api_db/postgres_data:/var/lib/postgresql/data/
- ./api_db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d/
env_file:
- api_db_config.env
I thought that maybe, a host port is occupied, so I checked ports with TCPView.
Here I can see both host ports occupied by three process each, that's OK. But when I try to reach backend (5003) from browser, I see this:
It seems, that for some reason, port can not accept connection, but I have no clue why and how can I fix it. Any help would be apprecietad.
发布评论
评论(1)
我还没有找到这种行为的确切原因,但我已经设法解决它。在我的 run.sh 脚本中,我启动了 uvicorn,因为
我通过添加密钥
--host 0.0.0.0
使其工作I haven't found an exact reason for such behaviour, but I've managed to fix it. In my run.sh script i started uvicorn as
I made it work by adding key
--host 0.0.0.0