Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: optimize dockerfile #532

Merged
merged 5 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ FROM python:${PYTHON_VERSION} as dev
# ARG is used here for temporary override without changing the original env.
ARG PYTHON_VERSION

# Config Python
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONHASHSEED=0
ENV PYTHONUNBUFFERED=1

# renovate: depName=debian_12/pipx
ARG PIPX_VERSION="1.1.0-1"
# renovate: depName=debian_12/sudo
Expand Down Expand Up @@ -56,14 +61,14 @@ RUN chown -R $USER_UID:$USER_GID $PIPX_HOME $PIPX_BIN_DIR

FROM dev as build

# Copy necessary files for build.
COPY pyproject.toml pdm.lock README.md /workspace/
COPY src/ /workspace/src

# Install dependencies and project into the local packages directory.
WORKDIR /workspace
ARG PDM_BUILD_SCM_VERSION=0.0.0
RUN mkdir __pypackages__ && PDM_BUILD_SCM_VERSION=${PDM_BUILD_SCM_VERSION} pdm sync --prod --no-editable
RUN --mount=source=README.md,target=README.md \
--mount=source=pdm.lock,target=pdm.lock \
--mount=source=pyproject.toml,target=pyproject.toml \
--mount=source=src,target=src \
msclock marked this conversation as resolved.
Show resolved Hide resolved
mkdir __pypackages__ && PDM_BUILD_SCM_VERSION=${PDM_BUILD_SCM_VERSION} pdm sync --prod --no-editable

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

# Config Python
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONHASHSEED=0
ENV PYTHONUNBUFFERED=1

# Retrieve packages from build stage.
ENV PYTHONPATH=/workspace/pkgs
COPY --from=build /workspace/__pypackages__/${PYTHON_VERSION}/lib /workspace/pkgs
Expand Down
3 changes: 1 addition & 2 deletions .devcontainer/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
*
.*
!/README.md
!/pdm.lock
!/pdm.toml
!/pyproject.toml
!/README.md
!/src/
3 changes: 1 addition & 2 deletions template/.devcontainer/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
*
.*
!/README.md
!/pdm.lock
!/pdm.toml
!/pyproject.toml
!/README.md
!/src/
20 changes: 15 additions & 5 deletions template/.devcontainer/Dockerfile.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ FROM python:${PYTHON_VERSION} as dev
# ARG is used here for temporary override without changing the original env.
ARG PYTHON_VERSION

# Config Python
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONHASHSEED=0
ENV PYTHONUNBUFFERED=1

# renovate: depName=debian_12/pipx
ARG PIPX_VERSION="1.1.0-1"
# renovate: depName=debian_12/sudo
Expand Down Expand Up @@ -56,14 +61,14 @@ RUN chown -R $USER_UID:$USER_GID $PIPX_HOME $PIPX_BIN_DIR

FROM dev as build

# Copy necessary files for build.
COPY pyproject.toml pdm.lock README.md /workspace/
COPY src/ /workspace/src

# Install dependencies and project into the local packages directory.
WORKDIR /workspace
ARG PDM_BUILD_SCM_VERSION=0.0.0
RUN mkdir __pypackages__ && PDM_BUILD_SCM_VERSION=${PDM_BUILD_SCM_VERSION} pdm sync --prod --no-editable
RUN --mount=source=README.md,target=README.md \
--mount=source=pdm.lock,target=pdm.lock \
--mount=source=pyproject.toml,target=pyproject.toml \
--mount=source=src,target=src \
mkdir __pypackages__ && PDM_BUILD_SCM_VERSION=${PDM_BUILD_SCM_VERSION} pdm sync --prod --no-editable

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

# Config Python
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONHASHSEED=0
ENV PYTHONUNBUFFERED=1

# Retrieve packages from build stage.
ENV PYTHONPATH=/workspace/pkgs
COPY --from=build /workspace/__pypackages__/${PYTHON_VERSION}/lib /workspace/pkgs
Expand Down
Loading