访问Docker上的API文档

发布于 2025-02-07 04:41:59 字数 772 浏览 1 评论 0原文

我有一个FastApi,当我与Uvicorn一起运行时,我可以通过Localhost/Docs打开其API文档。

当我在Docker上有这个Fastapi时,我将看不到API文档。 我需要为API文档添加一个额外的Docker容器吗?

这是我的Docker组成文件:

version: "3.7"
services:   
    web:
        build: ui
        ports:
          - 80:80
        depends_on:
          - api

    api:
        build: app
        environment:
          - PORT=80
        ports:
          - 8020:80

    

我用docker-compose-Build运行它。

我尝试了localhost/docs和localhost/swagger/index.html,但我看不到Docker上的API文档。

这是我的fastapi docker文件:

FROM python:3.9

COPY requirements.txt /app/


RUN pip install -r /app/requirements.txt
COPY ./ /app
WORKDIR /app
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8020"]

I have a fastapi and when I run it with uvicorn, I am able to open its api documentation through localhost/docs.

When I have this fastapi on docker, I am not able to see the API documentation.
Do I need to add an extra docker container for the API documentation?

Here is my docker compose file:

version: "3.7"
services:   
    web:
        build: ui
        ports:
          - 80:80
        depends_on:
          - api

    api:
        build: app
        environment:
          - PORT=80
        ports:
          - 8020:80

    

and I run it with docker-compose up --build.

I tried localhost/docs and localhost/swagger/index.html and I wasn't able to see the API documentation on docker.

Here is my fastapi docker file:

FROM python:3.9

COPY requirements.txt /app/


RUN pip install -r /app/requirements.txt
COPY ./ /app
WORKDIR /app
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8020"]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2025-02-14 04:41:59

我认为问题是不匹配端口。

尝试

ports:
   - 8020:8020

然后您可以在
http://0.0.0.0.0.0.8020/docs

I think the problem is mismatching ports.

try

ports:
   - 8020:8020

then you can see docs at
http://0.0.0.0:8020/docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文