Skip to content

Commit 6cfd977

Browse files
committed
refactor: enhance Dockerfile build flow
- Re-arranged steps order to install requirements before copying backend for better caching. - Introduced a healthcheck command to monitor the application status. - Set environment variables and documented the exposed port for better container management.
1 parent bc68c59 commit 6cfd977

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

Dockerfile

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
1+
# Frontend build stage
12
FROM node:20-slim as frontend-builder
23
WORKDIR /app/frontend
4+
5+
# Copy package files first to leverage layer caching
36
COPY src/frontend/package.json src/frontend/yarn.lock ./
47
RUN yarn install --frozen-lockfile
8+
9+
# Copy all frontend files
510
COPY src/frontend/ ./
11+
12+
# Build the frontend
613
RUN yarn build
714

15+
# Backend stage
816
FROM python:3.11-slim
917
WORKDIR /app
10-
COPY src/backend /app
18+
19+
# Copy requirements first to leverage layer caching
20+
COPY src/backend/requirements.txt .
1121
RUN pip install --no-cache-dir -r requirements.txt
22+
23+
# Install curl for healthcheck
24+
RUN apt-get update && apt-get install -y --no-install-recommends curl && \
25+
apt-get clean && \
26+
rm -rf /var/lib/apt/lists/*
27+
28+
# Copy backend files
29+
COPY src/backend .
30+
31+
# Copy built frontend from the frontend-builder stage
1232
COPY --from=frontend-builder /app/frontend/dist /app/frontend/dist
1333

14-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
34+
# Add healthcheck
35+
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
36+
CMD curl -f http://localhost:8000/ || exit 1
37+
38+
# Set environment variables
39+
ENV PYTHONUNBUFFERED=1
40+
41+
# Document the port number the container will expose
42+
EXPOSE 8000
43+
44+
# Run the application
45+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

0 commit comments

Comments
 (0)