构建DockerFile时如何阻止Uvicorn Asgi Web服务器运行?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
CMD
而不是运行
在Dockerfile中启动Uvicorn Server。它将将命令的执行延迟到启动容器时。运行
在图像构建过程中运行命令。cmd
在容器启动过程中运行命令。您的Dockerfile将被重写如下:
Use
CMD
instead ofRUN
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: