Skip to content

Commit 70e6b9f

Browse files
authored
Update Dockerfile for faster building (#936)
* Update Dockerfile for faster building * Fix build error * Copy file for maturin * Fix build error * Update snapshots * Fix command * Update snapshots * Remove extra trailing slash * Update snapshots
1 parent 55fde23 commit 70e6b9f

5 files changed

+41
-24
lines changed

src/fastapi/docker_files.rs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,13 @@ RUN sh /uv-installer.sh && rm /uv-installer.sh
422422
423423
ENV PATH="/root/.local/bin:$PATH"
424424
425+
# Create virtual environment and download Python
426+
RUN uv venv -p {python_version}
427+
425428
COPY . ./
426429
427430
RUN --mount=type=cache,target=/root/.cache/uv \
428-
uv venv -p {python_version} \
429-
&& uv sync --locked --no-dev --no-editable
431+
uv sync --locked --no-dev --no-editable
430432
431433
432434
# Build production stage
@@ -557,11 +559,12 @@ RUN : \
557559
&& apt-get clean \
558560
&& rm -rf /var/lib/apt/lists/*
559561
562+
# Create virtual environment
563+
RUN python{python_version} -m venv .venv
564+
560565
COPY . ./
561566
562-
RUN : \
563-
&& python{python_version} -m venv .venv \
564-
&& .venv/bin/python -m pip install -r requirements.txt
567+
RUN .venv/bin/python -m pip install -r requirements.txt
565568
566569
567570
# Build production stage
@@ -638,22 +641,24 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --de
638641
639642
ENV PATH="/root/.local/bin:/root/.cargo/bin:$PATH"
640643
641-
COPY Cargo.toml Cargo.lock ./
644+
# Create virtual environment and download Python
645+
RUN uv venv -p {python_version}
642646
643-
COPY pyproject.toml uv.lock ./
647+
COPY pyproject.toml Cargo.toml Cargo.lock README.md LICENSE ./
648+
COPY src/ ./src
649+
RUN mkdir {source_dir}
644650
645-
RUN --mount=type=cache,target=/root/.cache/uv \
646-
uv venv -p {python_version} \
647-
&& uv sync --locked --no-dev --no-install-project --no-editable
651+
RUN --mount=type=cache,target=/app/target/ \
652+
--mount=type=cache,target=/usr/local/cargo/git/db \
653+
--mount=type=cache,target=/usr/local/cargo/registry/ \
654+
uv tool run maturin develop -r
648655
649-
COPY . ./
656+
COPY uv.lock ./
650657
651658
RUN --mount=type=cache,target=/root/.cache/uv \
652-
--mount=type=cache,target=/app/target/ \
653-
--mount=type=cache,target=/usr/local/cargo/git/db \
654-
--mount=type=cache,target=/usr/local/cargo/registry/ \
655-
uv sync --locked --no-dev --no-editable \
656-
&& uv tool run maturin develop -r
659+
uv sync --locked --no-dev --no-install-project
660+
661+
COPY . /app
657662
658663
659664
# Build production stage
@@ -723,13 +728,25 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --de
723728
724729
ENV PATH="/root/.local/bin:/root/.cargo/bin:$PATH"
725730
731+
# Create virtual environment
732+
RUN python{python_version} -m venv .venv
733+
734+
COPY pyproject.toml Cargo.toml Cargo.lock README.md LICENSE ./
735+
COPY src/ ./src
736+
RUN mkdir {source_dir}
737+
738+
RUN --mount=type=cache,target=/app/target/ \
739+
--mount=type=cache,target=/usr/local/cargo/git/db \
740+
--mount=type=cache,target=/usr/local/cargo/registry/ \
741+
uv tool run maturin develop -r
742+
726743
COPY requirements.txt ./
727744
728-
RUN : \
729-
&& python{python_version} -m venv .venv \
730-
&& .venv/bin/python -m pip install -r requirements.txt
745+
RUN --mount=type=cache,target=/root/.cache/uv \
746+
.venv/bin/python -m pip install -r requirements.txt
747+
748+
COPY . /app
731749
732-
COPY . ./
733750
734751
RUN --mount=type=cache,target=/usr/local/cargo/git/db \
735752
--mount=type=cache,target=/usr/local/cargo/registry/ \

src/fastapi/snapshots/python_project__fastapi__docker_files__tests__save_dockerfile_maturin_setuptools.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/fastapi/docker_files.rs
33
expression: content
44
---
5-
"# syntax=docker/dockerfile:1\n\nFROM ubuntu:24.04 AS builder\n\nWORKDIR /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n UV_PYTHON_INSTALL_DIR=/opt/uv/python \\\n UV_LINK_MODE=copy\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n build-essential \\\n curl \\\n ca-certificates \\\n libssl-dev \\\n pkg-config \\\n software-properties-common \\\n && add-apt-repository ppa:deadsnakes/ppa \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n python3.11 \\\n python3.11-venv \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\n# Install uv\nADD https://astral.sh/uv/install.sh /uv-installer.sh\n\nRUN sh /uv-installer.sh && rm /uv-installer.sh\n\n# Install rust\nRUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable --profile minimal\n\nENV PATH=\"/root/.local/bin:/root/.cargo/bin:$PATH\"\n\nCOPY requirements.txt ./\n\nRUN : \\\n && python3.11 -m venv .venv \\\n && .venv/bin/python -m pip install -r requirements.txt\n\nCOPY . ./\n\nRUN --mount=type=cache,target=/usr/local/cargo/git/db \\\n --mount=type=cache,target=/usr/local/cargo/registry/ \\\n .venv/bin/python -m pip install -r requirements.txt \\\n && uv tool run maturin develop -r\n\n\n# Build production stage\nFROM ubuntu:24.04 AS prod\n\nRUN useradd appuser\n\nWORKDIR /app\n\nRUN chown appuser:appuser /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/app/.venv/bin:$PATH\" \\\n PORT=\"8000\"\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n software-properties-common \\\n && add-apt-repository ppa:deadsnakes/ppa \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n python3.11 \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\nCOPY --from=builder /app/.venv /app/.venv\nCOPY --from=builder /app/my_project /app/my_project\nCOPY ./scripts/entrypoint.sh /app\n\nRUN chmod +x /app/entrypoint.sh\n\nEXPOSE 8000\n\nUSER appuser\n\nENTRYPOINT [\"./entrypoint.sh\"]\n"
5+
"# syntax=docker/dockerfile:1\n\nFROM ubuntu:24.04 AS builder\n\nWORKDIR /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n UV_PYTHON_INSTALL_DIR=/opt/uv/python \\\n UV_LINK_MODE=copy\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n build-essential \\\n curl \\\n ca-certificates \\\n libssl-dev \\\n pkg-config \\\n software-properties-common \\\n && add-apt-repository ppa:deadsnakes/ppa \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n python3.11 \\\n python3.11-venv \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\n# Install uv\nADD https://astral.sh/uv/install.sh /uv-installer.sh\n\nRUN sh /uv-installer.sh && rm /uv-installer.sh\n\n# Install rust\nRUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable --profile minimal\n\nENV PATH=\"/root/.local/bin:/root/.cargo/bin:$PATH\"\n\n# Create virtual environment\nRUN python3.11 -m venv .venv\n\nCOPY pyproject.toml Cargo.toml Cargo.lock README.md LICENSE ./\nCOPY src/ ./src\nRUN mkdir my_project\n\nRUN --mount=type=cache,target=/app/target/ \\\n --mount=type=cache,target=/usr/local/cargo/git/db \\\n --mount=type=cache,target=/usr/local/cargo/registry/ \\\n uv tool run maturin develop -r\n\nCOPY requirements.txt ./\n\nRUN --mount=type=cache,target=/root/.cache/uv \\\n .venv/bin/python -m pip install -r requirements.txt\n\nCOPY . /app\n\n\nRUN --mount=type=cache,target=/usr/local/cargo/git/db \\\n --mount=type=cache,target=/usr/local/cargo/registry/ \\\n .venv/bin/python -m pip install -r requirements.txt \\\n && uv tool run maturin develop -r\n\n\n# Build production stage\nFROM ubuntu:24.04 AS prod\n\nRUN useradd appuser\n\nWORKDIR /app\n\nRUN chown appuser:appuser /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/app/.venv/bin:$PATH\" \\\n PORT=\"8000\"\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n software-properties-common \\\n && add-apt-repository ppa:deadsnakes/ppa \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n python3.11 \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\nCOPY --from=builder /app/.venv /app/.venv\nCOPY --from=builder /app/my_project /app/my_project\nCOPY ./scripts/entrypoint.sh /app\n\nRUN chmod +x /app/entrypoint.sh\n\nEXPOSE 8000\n\nUSER appuser\n\nENTRYPOINT [\"./entrypoint.sh\"]\n"

src/fastapi/snapshots/python_project__fastapi__docker_files__tests__save_dockerfile_maturin_uv.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/fastapi/docker_files.rs
33
expression: content
44
---
5-
"# syntax=docker/dockerfile:1\n\nFROM ubuntu:24.04 AS builder\n\nWORKDIR /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n UV_PYTHON_INSTALL_DIR=/opt/uv/python \\\n UV_LINK_MODE=copy\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n build-essential \\\n curl \\\n ca-certificates \\\n libssl-dev \\\n pkg-config \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\n# Install uv\nADD https://astral.sh/uv/install.sh /uv-installer.sh\n\nRUN sh /uv-installer.sh && rm /uv-installer.sh\n\n# Install rust\nRUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable --profile minimal\n\nENV PATH=\"/root/.local/bin:/root/.cargo/bin:$PATH\"\n\nCOPY Cargo.toml Cargo.lock ./\n\nCOPY pyproject.toml uv.lock ./\n\nRUN --mount=type=cache,target=/root/.cache/uv \\\n uv venv -p 3.11 \\\n && uv sync --locked --no-dev --no-install-project --no-editable\n\nCOPY . ./\n\nRUN --mount=type=cache,target=/root/.cache/uv \\\n --mount=type=cache,target=/app/target/ \\\n --mount=type=cache,target=/usr/local/cargo/git/db \\\n --mount=type=cache,target=/usr/local/cargo/registry/ \\\n uv sync --locked --no-dev --no-editable \\\n && uv tool run maturin develop -r\n\n\n# Build production stage\nFROM ubuntu:24.04 AS prod\n\nRUN useradd appuser\n\nWORKDIR /app\n\nRUN chown appuser:appuser /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/app/.venv/bin:$PATH\" \\\n PORT=\"8000\"\n\nCOPY --from=builder /app/.venv /app/.venv\nCOPY --from=builder /app/my_project /app/my_project\nCOPY --from=builder /opt/uv/python /opt/uv/python\nCOPY ./scripts/entrypoint.sh /app\n\nRUN chmod +x /app/entrypoint.sh\n\nEXPOSE 8000\n\nUSER appuser\n\nENTRYPOINT [\"./entrypoint.sh\"]\n"
5+
"# syntax=docker/dockerfile:1\n\nFROM ubuntu:24.04 AS builder\n\nWORKDIR /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n UV_PYTHON_INSTALL_DIR=/opt/uv/python \\\n UV_LINK_MODE=copy\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n build-essential \\\n curl \\\n ca-certificates \\\n libssl-dev \\\n pkg-config \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\n# Install uv\nADD https://astral.sh/uv/install.sh /uv-installer.sh\n\nRUN sh /uv-installer.sh && rm /uv-installer.sh\n\n# Install rust\nRUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable --profile minimal\n\nENV PATH=\"/root/.local/bin:/root/.cargo/bin:$PATH\"\n\n# Create virtual environment and download Python\nRUN uv venv -p 3.11\n\nCOPY pyproject.toml Cargo.toml Cargo.lock README.md LICENSE ./\nCOPY src/ ./src\nRUN mkdir my_project\n\nRUN --mount=type=cache,target=/app/target/ \\\n --mount=type=cache,target=/usr/local/cargo/git/db \\\n --mount=type=cache,target=/usr/local/cargo/registry/ \\\n uv tool run maturin develop -r\n\nCOPY uv.lock ./\n\nRUN --mount=type=cache,target=/root/.cache/uv \\\n uv sync --locked --no-dev --no-install-project\n\nCOPY . /app\n\n\n# Build production stage\nFROM ubuntu:24.04 AS prod\n\nRUN useradd appuser\n\nWORKDIR /app\n\nRUN chown appuser:appuser /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/app/.venv/bin:$PATH\" \\\n PORT=\"8000\"\n\nCOPY --from=builder /app/.venv /app/.venv\nCOPY --from=builder /app/my_project /app/my_project\nCOPY --from=builder /opt/uv/python /opt/uv/python\nCOPY ./scripts/entrypoint.sh /app\n\nRUN chmod +x /app/entrypoint.sh\n\nEXPOSE 8000\n\nUSER appuser\n\nENTRYPOINT [\"./entrypoint.sh\"]\n"

src/fastapi/snapshots/python_project__fastapi__docker_files__tests__save_dockerfile_setuptools.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/fastapi/docker_files.rs
33
expression: content
44
---
5-
"# syntax=docker/dockerfile:1\n\nFROM ubuntu:24.04 AS builder\n\nWORKDIR /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/root/.local/bin:$PATH\"\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n software-properties-common \\\n && add-apt-repository ppa:deadsnakes/ppa \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n python3.11 \\\n python3.11-venv \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\nCOPY . ./\n\nRUN : \\\n && python3.11 -m venv .venv \\\n && .venv/bin/python -m pip install -r requirements.txt\n\n\n# Build production stage\nFROM ubuntu:24.04 AS prod\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/app/.venv/bin:$PATH\" \\\n PORT=\"8000\"\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n software-properties-common \\\n && add-apt-repository ppa:deadsnakes/ppa \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n python3.11 \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\nRUN useradd appuser\n\nWORKDIR /app\n\nRUN chown appuser:appuser /app\n\nCOPY --from=builder /app/.venv /app/.venv\nCOPY --from=builder /app/my_project /app/my_project\nCOPY ./scripts/entrypoint.sh /app\n\nRUN chmod +x /app/entrypoint.sh\n\nEXPOSE 8000\n\nUSER appuser\n\nENTRYPOINT [\"./entrypoint.sh\"]\n"
5+
"# syntax=docker/dockerfile:1\n\nFROM ubuntu:24.04 AS builder\n\nWORKDIR /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/root/.local/bin:$PATH\"\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n software-properties-common \\\n && add-apt-repository ppa:deadsnakes/ppa \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n python3.11 \\\n python3.11-venv \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\n# Create virtual environment\nRUN python3.11 -m venv .venv\n\nCOPY . ./\n\nRUN .venv/bin/python -m pip install -r requirements.txt\n\n\n# Build production stage\nFROM ubuntu:24.04 AS prod\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/app/.venv/bin:$PATH\" \\\n PORT=\"8000\"\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n software-properties-common \\\n && add-apt-repository ppa:deadsnakes/ppa \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n python3.11 \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\nRUN useradd appuser\n\nWORKDIR /app\n\nRUN chown appuser:appuser /app\n\nCOPY --from=builder /app/.venv /app/.venv\nCOPY --from=builder /app/my_project /app/my_project\nCOPY ./scripts/entrypoint.sh /app\n\nRUN chmod +x /app/entrypoint.sh\n\nEXPOSE 8000\n\nUSER appuser\n\nENTRYPOINT [\"./entrypoint.sh\"]\n"

src/fastapi/snapshots/python_project__fastapi__docker_files__tests__save_dockerfile_uv.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: src/fastapi/docker_files.rs
33
expression: content
44
---
5-
"# syntax=docker/dockerfile:1\n\nFROM ubuntu:24.04 AS builder\n\nWORKDIR /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n UV_PYTHON_INSTALL_DIR=/opt/uv/python \\\n UV_LINK_MODE=copy\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n curl \\\n ca-certificates \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\n# Install uv\nADD https://astral.sh/uv/install.sh /uv-installer.sh\n\nRUN sh /uv-installer.sh && rm /uv-installer.sh\n\nENV PATH=\"/root/.local/bin:$PATH\"\n\nCOPY . ./\n\nRUN --mount=type=cache,target=/root/.cache/uv \\\n uv venv -p 3.11 \\\n && uv sync --locked --no-dev --no-editable\n\n\n# Build production stage\nFROM ubuntu:24.04 AS prod\n\nRUN useradd appuser\n\nWORKDIR /app\n\nRUN chown appuser:appuser /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/app/.venv/bin:$PATH\" \\\n PORT=\"8000\"\n\nCOPY --from=builder /app/.venv /app/.venv\nCOPY --from=builder /app/my_project /app/my_project\nCOPY --from=builder /opt/uv/python /opt/uv/python\nCOPY ./scripts/entrypoint.sh /app\n\nRUN chmod +x /app/entrypoint.sh\n\nEXPOSE 8000\n\nUSER appuser\n\nENTRYPOINT [\"./entrypoint.sh\"]\n"
5+
"# syntax=docker/dockerfile:1\n\nFROM ubuntu:24.04 AS builder\n\nWORKDIR /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n UV_PYTHON_INSTALL_DIR=/opt/uv/python \\\n UV_LINK_MODE=copy\n\nRUN : \\\n && apt-get update \\\n && apt-get install -y --no-install-recommends \\\n curl \\\n ca-certificates \\\n && apt-get clean \\\n && rm -rf /var/lib/apt/lists/*\n\n# Install uv\nADD https://astral.sh/uv/install.sh /uv-installer.sh\n\nRUN sh /uv-installer.sh && rm /uv-installer.sh\n\nENV PATH=\"/root/.local/bin:$PATH\"\n\n# Create virtual environment and download Python\nRUN uv venv -p 3.11\n\nCOPY . ./\n\nRUN --mount=type=cache,target=/root/.cache/uv \\\n uv sync --locked --no-dev --no-editable\n\n\n# Build production stage\nFROM ubuntu:24.04 AS prod\n\nRUN useradd appuser\n\nWORKDIR /app\n\nRUN chown appuser:appuser /app\n\nENV \\\n PYTHONUNBUFFERED=true \\\n PATH=\"/app/.venv/bin:$PATH\" \\\n PORT=\"8000\"\n\nCOPY --from=builder /app/.venv /app/.venv\nCOPY --from=builder /app/my_project /app/my_project\nCOPY --from=builder /opt/uv/python /opt/uv/python\nCOPY ./scripts/entrypoint.sh /app\n\nRUN chmod +x /app/entrypoint.sh\n\nEXPOSE 8000\n\nUSER appuser\n\nENTRYPOINT [\"./entrypoint.sh\"]\n"

0 commit comments

Comments
 (0)