Skip to content

Commit 21db07d

Browse files
authored
chore: optimize dockerfile (#532)
1 parent c23da50 commit 21db07d

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

.devcontainer/Dockerfile

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax=docker/dockerfile:1
2+
13
ARG PYTHON_VERSION=3.12
24

35
########################################################################################
@@ -10,6 +12,11 @@ FROM python:${PYTHON_VERSION} as dev
1012
# ARG is used here for temporary override without changing the original env.
1113
ARG PYTHON_VERSION
1214

15+
# Config Python
16+
ENV PYTHONDONTWRITEBYTECODE=1
17+
ENV PYTHONHASHSEED=0
18+
ENV PYTHONUNBUFFERED=1
19+
1320
# renovate: depName=debian_12/pipx
1421
ARG PIPX_VERSION="1.1.0-1"
1522
# renovate: depName=debian_12/sudo
@@ -56,14 +63,14 @@ RUN chown -R $USER_UID:$USER_GID $PIPX_HOME $PIPX_BIN_DIR
5663

5764
FROM dev as build
5865

59-
# Copy necessary files for build.
60-
COPY pyproject.toml pdm.lock README.md /workspace/
61-
COPY src/ /workspace/src
62-
6366
# Install dependencies and project into the local packages directory.
6467
WORKDIR /workspace
6568
ARG PDM_BUILD_SCM_VERSION=0.0.0
66-
RUN mkdir __pypackages__ && PDM_BUILD_SCM_VERSION=${PDM_BUILD_SCM_VERSION} pdm sync --prod --no-editable
69+
RUN --mount=source=README.md,target=README.md \
70+
--mount=source=pdm.lock,target=pdm.lock \
71+
--mount=source=pyproject.toml,target=pyproject.toml \
72+
--mount=source=src,target=src \
73+
mkdir __pypackages__ && PDM_BUILD_SCM_VERSION=${PDM_BUILD_SCM_VERSION} pdm sync --prod --no-editable
6774

6875
########################################################################################
6976
# Prod image is used for deployment and distribution.
@@ -75,6 +82,11 @@ FROM python:${PYTHON_VERSION}-slim as prod
7582
# ARG is used here for temporary override without changing the original env.
7683
ARG PYTHON_VERSION
7784

85+
# Config Python
86+
ENV PYTHONDONTWRITEBYTECODE=1
87+
ENV PYTHONHASHSEED=0
88+
ENV PYTHONUNBUFFERED=1
89+
7890
# Retrieve packages from build stage.
7991
ENV PYTHONPATH=/workspace/pkgs
8092
COPY --from=build /workspace/__pypackages__/${PYTHON_VERSION}/lib /workspace/pkgs

.devcontainer/Dockerfile.dockerignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
*
22
.*
3+
!/README.md
34
!/pdm.lock
4-
!/pdm.toml
55
!/pyproject.toml
6-
!/README.md
76
!/src/
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
*
22
.*
3+
!/README.md
34
!/pdm.lock
4-
!/pdm.toml
55
!/pyproject.toml
6-
!/README.md
76
!/src/

template/.devcontainer/Dockerfile.jinja

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# syntax=docker/dockerfile:1
2+
13
ARG PYTHON_VERSION={{ default_py }}
24

35
########################################################################################
@@ -10,6 +12,11 @@ FROM python:${PYTHON_VERSION} as dev
1012
# ARG is used here for temporary override without changing the original env.
1113
ARG PYTHON_VERSION
1214

15+
# Config Python
16+
ENV PYTHONDONTWRITEBYTECODE=1
17+
ENV PYTHONHASHSEED=0
18+
ENV PYTHONUNBUFFERED=1
19+
1320
# renovate: depName=debian_12/pipx
1421
ARG PIPX_VERSION="1.1.0-1"
1522
# renovate: depName=debian_12/sudo
@@ -56,14 +63,14 @@ RUN chown -R $USER_UID:$USER_GID $PIPX_HOME $PIPX_BIN_DIR
5663

5764
FROM dev as build
5865

59-
# Copy necessary files for build.
60-
COPY pyproject.toml pdm.lock README.md /workspace/
61-
COPY src/ /workspace/src
62-
6366
# Install dependencies and project into the local packages directory.
6467
WORKDIR /workspace
6568
ARG PDM_BUILD_SCM_VERSION=0.0.0
66-
RUN mkdir __pypackages__ && PDM_BUILD_SCM_VERSION=${PDM_BUILD_SCM_VERSION} pdm sync --prod --no-editable
69+
RUN --mount=source=README.md,target=README.md \
70+
--mount=source=pdm.lock,target=pdm.lock \
71+
--mount=source=pyproject.toml,target=pyproject.toml \
72+
--mount=source=src,target=src \
73+
mkdir __pypackages__ && PDM_BUILD_SCM_VERSION=${PDM_BUILD_SCM_VERSION} pdm sync --prod --no-editable
6774

6875
########################################################################################
6976
# Prod image is used for deployment and distribution.
@@ -75,6 +82,11 @@ FROM python:${PYTHON_VERSION}-slim as prod
7582
# ARG is used here for temporary override without changing the original env.
7683
ARG PYTHON_VERSION
7784

85+
# Config Python
86+
ENV PYTHONDONTWRITEBYTECODE=1
87+
ENV PYTHONHASHSEED=0
88+
ENV PYTHONUNBUFFERED=1
89+
7890
# Retrieve packages from build stage.
7991
ENV PYTHONPATH=/workspace/pkgs
8092
COPY --from=build /workspace/__pypackages__/${PYTHON_VERSION}/lib /workspace/pkgs

0 commit comments

Comments
 (0)