Skip to content

Commit 3f617cf

Browse files
committed
Add setuptools fastapi dockerfile
1 parent b19969e commit 3f617cf

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

.github/workflows/testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ jobs:
408408
- name: Test with pytest
409409
working-directory: ${{ env.WORKING_DIR }}
410410
if: matrix.project_type == 'application'
411-
run: pytest
411+
run: python -m pytest
412412
pixi-linting:
413413
strategy:
414414
fail-fast: false

.github/workflows/testing_fastapi.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,83 @@ jobs:
196196
working-directory: ${{ env.WORKING_DIR }}
197197
if: matrix.project_type == 'application'
198198
run: poetry run pytest
199+
test-setuptools-fastapi-project:
200+
name: test-fastapi-setuptools-setup-fastapi
201+
strategy:
202+
fail-fast: false
203+
runs-on: ubuntu-latest
204+
steps:
205+
- uses: actions/checkout@v5
206+
- name: install Rust
207+
uses: dtolnay/rust-toolchain@stable
208+
- name: Cache Rust dependencies
209+
uses: Swatinem/[email protected]
210+
- name: Install sqlx-cli
211+
run: cargo install sqlx-cli --no-default-features -F native-tls -F postgres
212+
- name: Set up Python
213+
uses: actions/setup-python@v6
214+
with:
215+
python-version: "${{ env.MIN_PYTHON_VERSION }}"
216+
- name: Build package
217+
run: cargo build --release -F fastapi
218+
- name: Run creation
219+
run: ./scripts/ci_run_fastapi.sh "fastapi" 4
220+
shell: bash
221+
- name: Install Dependencies
222+
working-directory: ${{ env.WORKING_DIR }}
223+
run: |
224+
python -m pip install -U pip
225+
python -m pip install -r requirements-dev.txt -r requirements.txt
226+
- name: Pre-commit check
227+
working-directory: ${{ env.WORKING_DIR }}
228+
run: |
229+
pre-commit install
230+
git add .
231+
pre-commit run --all-files
232+
- name: make .env
233+
working-directory: ${{ env.WORKING_DIR }}
234+
run: touch .env
235+
- name: Build and start Docker containers
236+
working-directory: ${{ env.WORKING_DIR }}
237+
run: docker compose up -d
238+
- name: Test with pytest
239+
working-directory: ${{ env.WORKING_DIR }}
240+
run: python -m pytest -n auto
241+
test-setuptools-non-fastapi-project:
242+
name: test-fastapi-setuptools-setup-non-fastapi
243+
strategy:
244+
fail-fast: false
245+
matrix:
246+
project_type: ["application", "lib"]
247+
os: [ubuntu-latest, windows-latest]
248+
runs-on: ${{ matrix.os }}
249+
steps:
250+
- uses: actions/checkout@v5
251+
- name: install Rust
252+
uses: dtolnay/rust-toolchain@stable
253+
- name: Cache Rust dependencies
254+
uses: Swatinem/[email protected]
255+
- name: Set up Python
256+
uses: actions/setup-python@v6
257+
with:
258+
python-version: "${{ env.MIN_PYTHON_VERSION }}"
259+
- name: Build package
260+
run: cargo build --release -F fastapi
261+
- name: Run creation
262+
run: ./scripts/ci_run_fastapi.sh ${{ matrix.project_type }} 4
263+
shell: bash
264+
- name: Install Dependencies
265+
working-directory: ${{ env.WORKING_DIR }}
266+
run: |
267+
python -m pip install -U pip
268+
python -m pip install -r requirements-dev.txt -r requirements.txt
269+
- name: Pre-commit check
270+
working-directory: ${{ env.WORKING_DIR }}
271+
run: |
272+
pre-commit install
273+
git add .
274+
pre-commit run --all-files
275+
- name: Test with pytest
276+
working-directory: ${{ env.WORKING_DIR }}
277+
if: matrix.project_type == 'application'
278+
run: python -m pytest

src/fastapi/docker_files.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,75 @@ EXPOSE 8000
532532
533533
USER appuser
534534
535+
ENTRYPOINT ["./entrypoint.sh"]
536+
"#
537+
),
538+
ProjectManager::Setuptools => format!(
539+
r#"# syntax=docker/dockerfile:1
540+
541+
FROM ubuntu:24.04 AS builder
542+
543+
WORKDIR /app
544+
545+
ENV \
546+
PYTHONUNBUFFERED=true \
547+
PATH="/root/.local/bin:$PATH"
548+
549+
RUN : \
550+
&& apt-get update \
551+
&& apt-get install -y --no-install-recommends \
552+
software-properties-common \
553+
&& add-apt-repository ppa:deadsnakes/ppa \
554+
&& apt-get update \
555+
&& apt-get install -y --no-install-recommends \
556+
python{python_version} \
557+
python{python_version}-venv \
558+
&& apt-get clean \
559+
&& rm -rf /var/lib/apt/lists/*
560+
561+
COPY . ./
562+
563+
RUN : \
564+
python{python_version} -m venv .venv \
565+
&& .venv/bin/python -m pip install -r requirements.txt
566+
567+
568+
# Build production stage
569+
FROM ubuntu:24.04 AS prod
570+
571+
ENV \
572+
PYTHONUNBUFFERED=true \
573+
PATH="/app/.venv/bin:$PATH" \
574+
PORT="8000"
575+
576+
RUN : \
577+
&& apt-get update \
578+
&& apt-get install -y --no-install-recommends \
579+
software-properties-common \
580+
&& add-apt-repository ppa:deadsnakes/ppa \
581+
&& apt-get update \
582+
&& apt-get install -y --no-install-recommends \
583+
python3.13 \
584+
&& apt-get clean \
585+
&& rm -rf /var/lib/apt/lists/*
586+
587+
RUN useradd appuser
588+
589+
WORKDIR /app
590+
591+
RUN chown appuser:appuser /app
592+
593+
COPY --from=builder /app/.venv /app/.venv
594+
COPY --from=builder /app/{source_dir} /app/{source_dir}
595+
COPY --from=builder /opt/uv/python /opt/uv/python
596+
COPY ./scripts/entrypoint.sh /app
597+
598+
RUN chmod +x /app/entrypoint.sh
599+
600+
EXPOSE 8000
601+
602+
USER appuser
603+
535604
ENTRYPOINT ["./entrypoint.sh"]
536605
"#
537606
),

src/fastapi/fastapi_installer.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ fn setuptools_fastapi_depencency_installer(project_info: &ProjectInfo) -> Result
131131
bail!("Failed to install FastAPI dependencies: {stderr}");
132132
}
133133

134+
let freeze = std::process::Command::new(".venv/bin/python")
135+
.args(["-m", "pip", "freeze"])
136+
.current_dir(project_info.base_dir())
137+
.output()?;
138+
139+
if !freeze.status.success() {
140+
let stderr = String::from_utf8_lossy(&freeze.stderr);
141+
bail!("Failed to get pip freeze output: {stderr}");
142+
}
143+
144+
let requirements_path = project_info.base_dir().join("requirements.txt");
145+
std::fs::write(requirements_path, freeze.stdout)?;
146+
134147
Ok(())
135148
}
136149

0 commit comments

Comments
 (0)