Skip to content

Commit aaed930

Browse files
committed
Optimize docker file
1 parent 4b951c4 commit aaed930

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
**/node_modules
1+
**/node_modules
2+
**/dist

Dockerfile

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ RUN ["pip", "install", "pipenv"]
88
RUN ["sh", "-c", "pipenv requirements --dev > requirements.txt"]
99
RUN ["rm", "Pipfile", "Pipfile.lock"]
1010

11-
FROM python:3.12 as build-server
11+
FROM python:3.12 as build-server-deps
1212

1313
# Path: /app
1414
WORKDIR /app
@@ -19,26 +19,43 @@ RUN ["python3", "-m", "venv", "/venv"]
1919
ENV PATH="/venv/bin:$PATH"
2020
RUN ["python3", "-m", "pip", "install", "-r", "requirements.txt"]
2121

22+
FROM node:20 as build-frontend-deps
23+
24+
# Path: /tmp/
25+
WORKDIR /tmp/
26+
27+
COPY ./frontend/package.json frontend/package.json
28+
WORKDIR /tmp/frontend
29+
RUN ["npm", "install"]
30+
31+
FROM python:3.12 as build-server-openapi
32+
33+
WORKDIR /app
34+
35+
COPY --from=build-server-deps /venv /venv
36+
2237
COPY src/main.py src/main.py
2338
COPY dump_openapi.py .
2439
ENV FASTAPI_OPENAPI_OUTPUT="/tmp/openapi.json"
2540
RUN ["mkdir", "-p", "frontend/dist"]
41+
42+
ENV PATH="/venv/bin:$PATH"
2643
RUN ["python3", "dump_openapi.py"]
2744

2845
FROM node:20 as build-frontend
2946

30-
# Path: /tmp/
31-
WORKDIR /tmp/
47+
COPY --from=build-server-openapi /tmp/openapi.json /tmp/openapi.json
48+
COPY ./frontend /tmp/frontend
49+
COPY --from=build-frontend-deps /tmp/frontend/node_modules /tmp/frontend/node_modules
3250

33-
COPY ./frontend frontend
3451
WORKDIR /tmp/frontend
35-
RUN ["npm", "install"]
36-
COPY --from=build-server /tmp/openapi.json /tmp/openapi.json
52+
3753
RUN ["npx", "openapi-typescript", "/tmp/openapi.json", "-o", "src/api/schema.d.ts"]
3854
RUN ["npm", "run", "build"]
3955

4056
FROM python:3.12-slim as production
41-
COPY --from=build-server /venv /venv
57+
58+
COPY --from=build-server-deps /venv /venv
4259
COPY src/main.py .
4360
COPY --from=build-frontend /tmp/frontend/dist ./frontend/dist
4461
ENV PATH="/venv/bin:$PATH"

0 commit comments

Comments
 (0)