Skip to content

Commit 1257fc2

Browse files
committed
chore/security : Implement multi-stage build to support distroless final image
1 parent 6718d02 commit 1257fc2

1 file changed

Lines changed: 33 additions & 27 deletions

File tree

tools/chatbot/Dockerfile

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
1-
# Use an official Python runtime as a parent image
2-
FROM dhi.io/python:3.14
1+
# Builder stage - use standard Python image for building
2+
FROM python:3.13-slim AS builder
33

44
# Set environment variables
55
ENV PYTHONDONTWRITEBYTECODE=1 \
66
PYTHONUNBUFFERED=1 \
7-
UV_CACHE_DIR=/opt/uv-cache \
7+
UV_CACHE_DIR=/tmp/uv-cache \
88
UV_LINK_MODE=copy
99

10-
# Install system dependencies and uv
11-
RUN apt-get update && apt-get install -y \
12-
curl \
13-
ca-certificates \
14-
&& rm -rf /var/lib/apt/lists/* \
15-
&& curl -LsSf https://astral.sh/uv/install.sh | sh \
16-
&& mv /root/.local/bin/uv /usr/local/bin/uv \
17-
&& mv /root/.local/bin/uvx /usr/local/bin/uvx
10+
# Install uv
11+
RUN apt-get update && apt-get install -y curl ca-certificates && \
12+
curl -LsSf https://astral.sh/uv/install.sh | sh && \
13+
mv /root/.local/bin/uv /usr/local/bin/uv && \
14+
rm -rf /var/lib/apt/lists/*
1815

19-
# Create a non-root user and group
20-
RUN groupadd -r -g 1000 chatbot && useradd -r -u 1000 -g chatbot chatbot
21-
22-
# Set the working directory in the container
16+
# Set the working directory
2317
WORKDIR /app
2418

25-
# Create UV cache directory with proper permissions and set ownership of working directory
26-
RUN mkdir -p $UV_CACHE_DIR && \
27-
chown -R chatbot:chatbot $UV_CACHE_DIR && \
28-
chown -R chatbot:chatbot /app
19+
# Copy dependency files
20+
COPY pyproject.toml uv.lock ./
2921

30-
# Switch to the non-root user
31-
USER chatbot
22+
# Install dependencies
23+
RUN uv sync --frozen --no-dev
3224

33-
# Copy the current directory contents into the container at /app
34-
COPY --chown=chatbot:chatbot . .
25+
# Final stage - use hardened image
26+
FROM dhi.io/python:3.13
3527

36-
# Install dependencies and project in one step
37-
RUN uv sync --frozen --no-dev
28+
# Set environment variables
29+
ENV PYTHONDONTWRITEBYTECODE=1 \
30+
PYTHONUNBUFFERED=1
31+
32+
# Set the working directory
33+
WORKDIR /app
34+
35+
# Copy virtual environment from builder
36+
COPY --from=builder /app/.venv /app/.venv
37+
38+
# Copy application code
39+
COPY . .
40+
41+
# Run as non-root user
42+
USER 1000:1000
3843

39-
# Run the application (--no-sync prevents any dependency downloads at runtime)
40-
CMD ["uv", "run", "--no-sync", "python", "chatbot.py"]
44+
# Run the application using the virtual environment directly
45+
ENTRYPOINT ["/app/.venv/bin/python"]
46+
CMD ["chatbot.py"]

0 commit comments

Comments
 (0)