FastAPI + NGINX + Docker 连接被拒绝

huangapple go评论82阅读模式
英文:

FastAPI + NGINX + Docker connection refused

问题

我试图创建一个简单的Docker Compose,其中包括启动两个容器,一个是下面所示的应用容器:

  1. from fastapi import APIRouter
  2. router = APIRouter()
  3. @router.get("/")
  4. async def root():
  5. return {"message": "This is a Test"}
  1. from fastapi import FastAPI
  2. import uvicorn as uvicorn
  3. from routers import test
  4. app = FastAPI()
  5. app.include_router(test.router)
  6. if __name__ == "__main__":
  7. uvicorn.run(app, host="0.0.0.0", port=8000)

另一个容器是NGINX,其配置如下:

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. server {
  7. listen 80;
  8. location / {
  9. proxy_pass http://0.0.0.0:8000;
  10. }
  11. }
  12. }

我的Docker Compose 如下:

  1. version: "3.7"
  2. services:
  3. web:
  4. build: nginx
  5. ports:
  6. - "80:80"
  7. depends_on:
  8. - app
  9. container_name: tensor_nginx
  10. app:
  11. build: app
  12. ports:
  13. - "8000:8000"
  14. container_name: test

当我启动一切都正常,可以通过访问0.0.0.0:8000/docs连接到FastAPI创建的Swagger,并且端点返回了代码中定义的字符串。

但如果我尝试使用NGINX端口连接,它会显示连接失败的消息:

  1. [error] 30#30: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://0.0.0.0:8000/", host: "127.0.0.1"

我在配置中漏掉了什么?

英文:

I'm trying to create a simple docker-compose that would launch 2 containers one for the app as seen below:

  1. from fastapi import APIRouter
  2. router = APIRouter()
  3. @router.get("/")
  4. async def root():
  5. return {"message": "This is a Test"}
  1. from fastapi import FastAPI
  2. import uvicorn as uvicorn
  3. from routers import test
  4. app = FastAPI()
  5. app.include_router(test.router)
  6. if __name__ == "__main__":
  7. uvicorn.run(app, host="0.0.0.0", port=8000)

The other for NGINX with the configuration:

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. server {
  7. listen 80;
  8. location / {
  9. proxy_pass http://0.0.0.0:8000;
  10. }
  11. }
  12. }

My docker-compose is as followed:

  1. version: "3.7"
  2. services:
  3. web:
  4. build: nginx
  5. ports:
  6. - "80:80"
  7. depends_on:
  8. - app
  9. container_name: tensor_nginx
  10. app:
  11. build: app
  12. ports:
  13. - "8000:8000"
  14. container_name: test

When i fire up everything starts fine and I can connect to the Swagger created by FastAPI by going 0.0.0.0:8000/docs and the endpoint returns the string defined in the code.

But if I try to connect using nginx port it says connection failed.

  1. [error] 30#30: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://0.0.0.0:8000/", host: "127.0.0.1"

What am I missing in the configurations?

答案1

得分: 0

nginx配置中的proxy_pass http://0.0.0.0:8000;这行没有意义(因为nginx部署在容器内,地址0.0.0.0无法解析),在这里,您需要在最终设备或域名之前指定完整的地址。在您的情况下,您可以使用Docker Compose文件中的域名,例如:
proxy_pass http://app:8000;
或者手动设置主域名

  1. app:
  2. build: app
  3. hostname: my-fastapi-app
  4. ports:
  5. - "8000:8000"

  1. proxy_pass http://my-fastapi-app:8000;
英文:

The line proxy_pass http://0.0.0.0:8000; of nginx conf does not make sense (since nginx is deployed inside a container, address 0.0.0.0 cannot be resolved), here you need to specify the full address before the end device or domain name. In your case, you can use the domain name from the docker compose file such as
proxy_pass http://app:8000 or set domain main manually

  1. app:
  2. build: app
  3. hostname: my-fastapi-app
  4. ports:
  5. - "8000:8000"

and

  1. proxy_pass http://my-fastapi-app:8000;

huangapple
  • 本文由 发表于 2023年3月3日 18:49:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定