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
JavaScript
x
24
24
1
from fastapi import FastAPI, Request, Response
2
from fastapi.templating import Jinja2Templates
3
from fastapi.staticfiles import StaticFiles
4
5
from src.scraper import Scraper
6
7
app = FastAPI()
8
templates = Jinja2Templates(directory="src/templates")
9
app.mount("/static", StaticFiles(directory="src/static"), name="static")
10
11
scraper = Scraper()
12
scraper.scrapedata()
13
14
15
@app.get("/")
16
async def home(request: Request):
17
return templates.TemplateResponse("index.html", {"request": request, "items": scraper.scrapedata()})
18
19
20
# if __name__ == "__main__":
21
# import uvicorn
22
# uvicorn.run(app, host="0.0.0.0", port=8000)
23
24
Dockerfile
JavaScript
1
23
23
1
FROM python:3.9.2-buster
2
3
ENV PYTHONDONTWRITEBYTECODEBYDEFAULT=1
4
5
ENV PYTHONUNBUFFERED=1
6
7
ENV PYTHONPATH "${PYTHONPATH}:/usr/src/"
8
9
COPY requirements.txt .
10
RUN python -m pip install --upgrade pip
11
RUN python -m pip install -r requirements.txt
12
13
WORKDIR /website
14
COPY . /website
15
16
RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website
17
USER webuser
18
19
EXPOSE 8000
20
21
#this is the cause for endless running. Any other way to do this?
22
RUN python -m uvicorn src.main:app --host 0.0.0.0 --port 8000
23
Console output
JavaScript
1
25
25
1
[+] Building 15.2s (11/12)
2
[+] Building 974.8s (11/12)
3
=> [internal] load build definition from Dockerfile 0.1s
4
=> => transferring dockerfile: 512B 0.0s
5
=> [internal] load .dockerignore 0.0s
6
=> => transferring context: 34B 0.0s
7
=> [internal] load metadata for docker.io/library/python:3.9.2-buster 2.4s
8
=> [1/8] FROM docker.io/library/python:3.9.2-buster@sha256:56f1b4dbdebb3b6ec31126e256c0852d18e79909ed1df8b594e56 0.0s
9
=> [internal] load build context 0.2s
10
=> => transferring context: 135.50kB 0.2s
11
=> CACHED [2/8] COPY requirements.txt . 0.0s
12
=> CACHED [3/8] RUN python -m pip install --upgrade pip 0.0s
13
=> CACHED [4/8] RUN python -m pip install -r requirements.txt 0.0s
14
=> CACHED [5/8] WORKDIR /website 0.0s
15
=> [6/8] COPY . /website 0.3s
16
=> [7/8] RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website 5.5s
17
=> [8/8] RUN python -m uvicorn src.main:app --host 0.0.0.0 --port 8000 966.2s
18
=> => # 200
19
=> => # INFO: Started server process [7]
20
=> => # INFO: Waiting for application startup.
21
=> => # INFO: Application startup complete.
22
=> => # INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
23
24
25
Advertisement
Answer
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:
JavaScript
1
22
22
1
FROM python:3.9.2-buster
2
3
ENV PYTHONDONTWRITEBYTECODEBYDEFAULT=1
4
5
ENV PYTHONUNBUFFERED=1
6
7
ENV PYTHONPATH "${PYTHONPATH}:/usr/src/"
8
9
COPY requirements.txt .
10
RUN python -m pip install --upgrade pip
11
RUN python -m pip install -r requirements.txt
12
13
WORKDIR /website
14
COPY . /website
15
16
RUN adduser -u 5678 --disabled-password --gecos "" webuser && chown -R webuser /website
17
USER webuser
18
19
EXPOSE 8000
20
21
CMD ["python", "-m", "uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "8000"]
22