Skip to content

Commit 9e876d9

Browse files
committed
Adjust docker to official UV docs
1 parent ee02406 commit 9e876d9

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

Dockerfile.optimized

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@
44
# checkov:skip=CKV_DOCKER_3: "The Dockerfile uses the official AWS Lambda Python base image (public.ecr.aws/lambda/python:3.12-arm64), which already configures the appropriate non-root user for Lambda execution"
55
# checkov:skip=CKV_DOCKER_2: "The Dockerfile.optimized is specifically designed for AWS Lambda container images, which don't use Docker HEALTHCHECK instructions."
66

7+
# Use specific version to avoid network issues
8+
FROM ghcr.io/astral-sh/uv:0.9.6 AS uv
79

10+
# Builder stage - bundle dependencies into Lambda task root
811
FROM public.ecr.aws/lambda/python:3.12-arm64 AS builder
912

10-
# Copy uv from official distroless image
11-
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
13+
# Enable bytecode compilation to improve cold-start performance
14+
ENV UV_COMPILE_BYTECODE=1
15+
16+
# Disable installer metadata to create a deterministic layer
17+
ENV UV_NO_INSTALLER_METADATA=1
18+
19+
# Enable copy mode to support bind mount caching
20+
ENV UV_LINK_MODE=copy
1221

1322
# Build argument for function path
1423
ARG FUNCTION_PATH
@@ -22,26 +31,25 @@ COPY lib/idp_common_pkg /tmp/idp_common_pkg
2231
COPY ${FUNCTION_PATH}/requirements.txt* /build/
2332

2433
# Install all dependencies including idp_common_pkg in one step
25-
RUN --mount=type=cache,target=/root/.cache/uv \
26-
if [ -f /build/requirements.txt ]; then \
27-
sed 's|^\.\./\.\.\(/\.\.\)\?/lib/idp_common_pkg|/tmp/idp_common_pkg|' /build/requirements.txt > /tmp/requirements.txt && \
28-
uv pip install --python python3.12 --target /opt/python -r /tmp/requirements.txt && \
29-
rm /tmp/requirements.txt; \
30-
fi && \
31-
rm -rf /tmp/idp_common_pkg
34+
# Using mount from uv stage instead of COPY to avoid layer bloat
35+
RUN --mount=from=uv,source=/uv,target=/bin/uv \
36+
--mount=type=cache,target=/root/.cache/uv \
37+
if [ -f /build/requirements.txt ]; then \
38+
sed 's|^\.\./\.\.\(/\.\.\)\?/lib/idp_common_pkg|/tmp/idp_common_pkg|' /build/requirements.txt > /tmp/requirements.txt && \
39+
uv pip install --python python3.12 --target "${LAMBDA_TASK_ROOT}" -r /tmp/requirements.txt && \
40+
rm /tmp/requirements.txt; \
41+
fi && \
42+
rm -rf /tmp/idp_common_pkg
3243

3344
# Final stage - minimal runtime
3445
FROM public.ecr.aws/lambda/python:3.12-arm64
3546

36-
# Copy only the installed packages
37-
COPY --from=builder /opt/python /opt/python
47+
# Copy the runtime dependencies from the builder stage
48+
COPY --from=builder ${LAMBDA_TASK_ROOT} ${LAMBDA_TASK_ROOT}
3849

3950
# Copy function code
4051
ARG FUNCTION_PATH
4152
COPY ${FUNCTION_PATH}/*.py ${LAMBDA_TASK_ROOT}/
4253

43-
# Set Python path
44-
ENV PYTHONPATH=/opt/python:${LAMBDA_TASK_ROOT}
45-
4654
# Set handler
47-
CMD ["index.handler"]
55+
CMD ["index.handler"]

0 commit comments

Comments
 (0)