构建DockerFile时如何阻止Uvicorn Asgi Web服务器运行?

发布于 2025-02-09 06:06:40 字数 3634 浏览 2 评论 0原文

Docker构建运行不断,

我正在尝试构建Docker映像,但是我在构建Uvicorn Server构建时遇到了一个问题,这使其永远不会构建。

因此,我正在寻找构建/运行Docker图像的另一种方法。

UVICORN服务器

启动时运行

from fastapi import FastAPI, Request, Response
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles

from src.scraper import Scraper

app = FastAPI()
templates = Jinja2Templates(directory="src/templates")
app.mount("/static", StaticFiles(directory="src/static"), name="static")

scraper = Scraper()
scraper.scrapedata()


@app.get("/")
async def home(request: Request):
    return templates.TemplateResponse("index.html", {"request": request, "items": scraper.scrapedata()})


# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="0.0.0.0", port=8000)

必需

FROM python:3.9.2-buster

ENV PYTHONDONTWRITEBYTECODEBYDEFAULT=1

ENV PYTHONUNBUFFERED=1

ENV PYTHONPATH "${PYTHONPATH}:/usr/src/"

COPY requirements.txt .
RUN python -m pip install --upgrade pip
RUN python -m pip install -r requirements.txt

WORKDIR /website
COPY . /website

RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website
USER webuser

EXPOSE 8000

#this is the cause for endless running. Any other way to do this?
RUN python -m uvicorn src.main:app --host 0.0.0.0 --port 8000

Docker映像

[+] Building 15.2s (11/12)
[+] Building 974.8s (11/12)
 => [internal] load build definition from Dockerfile                                                               0.1s
 => => transferring dockerfile: 512B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 34B                                                                                   0.0s
 => [internal] load metadata for docker.io/library/python:3.9.2-buster                                             2.4s
 => [1/8] FROM docker.io/library/python:3.9.2-buster@sha256:56f1b4dbdebb3b6ec31126e256c0852d18e79909ed1df8b594e56  0.0s
 => [internal] load build context                                                                                  0.2s
 => => transferring context: 135.50kB                                                                              0.2s
 => CACHED [2/8] COPY requirements.txt .                                                                           0.0s
 => CACHED [3/8] RUN python -m pip install --upgrade pip                                                           0.0s
 => CACHED [4/8] RUN python -m pip install -r requirements.txt                                                     0.0s
 => CACHED [5/8] WORKDIR /website                                                                                  0.0s
 => [6/8] COPY . /website                                                                                          0.3s
 => [7/8] RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website                  5.5s
 => [8/8] RUN python -m uvicorn src.main:app --host 0.0.0.0 --port 8000                                          966.2s
 => => # 200
 => => # INFO:     Started server process [7]
 => => # INFO:     Waiting for application startup.
 => => # INFO:     Application startup complete.
 => => # INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)


Docker build running endlessly

I'm trying to build a docker image, but i'm running into an issue with uvicorn server running while building which causes it to never build.

so i'm looking for an alternative way of building/running the docker image.

Required the docker image should run the uvicorn server on startup

Haven't found any real solution from browsing SOF/Google

Code

main.py file

from fastapi import FastAPI, Request, Response
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles

from src.scraper import Scraper

app = FastAPI()
templates = Jinja2Templates(directory="src/templates")
app.mount("/static", StaticFiles(directory="src/static"), name="static")

scraper = Scraper()
scraper.scrapedata()


@app.get("/")
async def home(request: Request):
    return templates.TemplateResponse("index.html", {"request": request, "items": scraper.scrapedata()})


# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="0.0.0.0", port=8000)

Dockerfile

FROM python:3.9.2-buster

ENV PYTHONDONTWRITEBYTECODEBYDEFAULT=1

ENV PYTHONUNBUFFERED=1

ENV PYTHONPATH "${PYTHONPATH}:/usr/src/"

COPY requirements.txt .
RUN python -m pip install --upgrade pip
RUN python -m pip install -r requirements.txt

WORKDIR /website
COPY . /website

RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website
USER webuser

EXPOSE 8000

#this is the cause for endless running. Any other way to do this?
RUN python -m uvicorn src.main:app --host 0.0.0.0 --port 8000

Console output

[+] Building 15.2s (11/12)
[+] Building 974.8s (11/12)
 => [internal] load build definition from Dockerfile                                                               0.1s
 => => transferring dockerfile: 512B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 34B                                                                                   0.0s
 => [internal] load metadata for docker.io/library/python:3.9.2-buster                                             2.4s
 => [1/8] FROM docker.io/library/python:3.9.2-buster@sha256:56f1b4dbdebb3b6ec31126e256c0852d18e79909ed1df8b594e56  0.0s
 => [internal] load build context                                                                                  0.2s
 => => transferring context: 135.50kB                                                                              0.2s
 => CACHED [2/8] COPY requirements.txt .                                                                           0.0s
 => CACHED [3/8] RUN python -m pip install --upgrade pip                                                           0.0s
 => CACHED [4/8] RUN python -m pip install -r requirements.txt                                                     0.0s
 => CACHED [5/8] WORKDIR /website                                                                                  0.0s
 => [6/8] COPY . /website                                                                                          0.3s
 => [7/8] RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website                  5.5s
 => [8/8] RUN python -m uvicorn src.main:app --host 0.0.0.0 --port 8000                                          966.2s
 => => # 200
 => => # INFO:     Started server process [7]
 => => # INFO:     Waiting for application startup.
 => => # INFO:     Application startup complete.
 => => # INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)


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

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

发布评论

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

评论(1

伤感在游骋 2025-02-16 06:06:40

使用CMD而不是运行在Dockerfile中启动Uvicorn Server。它将将命令的执行延迟到启动容器时。

运行在图像构建过程中运行命令。

cmd在容器启动过程中运行命令。

您的Dockerfile将被重写如下:

FROM python:3.9.2-buster

ENV PYTHONDONTWRITEBYTECODEBYDEFAULT=1

ENV PYTHONUNBUFFERED=1

ENV PYTHONPATH "${PYTHONPATH}:/usr/src/"

COPY requirements.txt .
RUN python -m pip install --upgrade pip
RUN python -m pip install -r requirements.txt

WORKDIR /website
COPY . /website

RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website
USER webuser

EXPOSE 8000

CMD ["python", "-m", "uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]

Use CMD instead of RUN to launch the uvicorn server in the Dockerfile. It will retard the execution of the command to when the container is launched.

RUN runs commands during the image building.

CMD runs commands during container launching.

Your Dockerfile would be rewritten as follows:

FROM python:3.9.2-buster

ENV PYTHONDONTWRITEBYTECODEBYDEFAULT=1

ENV PYTHONUNBUFFERED=1

ENV PYTHONPATH "${PYTHONPATH}:/usr/src/"

COPY requirements.txt .
RUN python -m pip install --upgrade pip
RUN python -m pip install -r requirements.txt

WORKDIR /website
COPY . /website

RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website
USER webuser

EXPOSE 8000

CMD ["python", "-m", "uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文