Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 3, 2025

Previously, Dockerfiles were only generated for Python apps using UV. Non-UV Python apps had no automatic containerization support.

Changes

  • Split Dockerfile generation logic into GenerateUvDockerfile and GenerateFallbackDockerfile methods
  • Fallback Dockerfiles for non-UV apps:
    • Use same base image as UV workflow: python:{version}-slim-bookworm
    • Detect requirements.txt and run pip install --no-cache-dir -r requirements.txt when present
    • Single-stage build with non-root user
    • Support all entrypoint types (Script, Module, Executable)
  • Updated PythonVersionDetector to accept nullable VirtualEnvironment for version detection without venv

Example

// Python app without UV now generates Dockerfile automatically
var python = builder.AddPythonScript("api", "../python-api", "main.py");
// Publishes with generated Dockerfile that installs requirements.txt dependencies

Generated Dockerfile (with requirements.txt):

FROM python:3.12-slim-bookworm

RUN groupadd --system --gid 999 appuser && useradd --system --gid 999 --uid 999 --create-home appuser
WORKDIR /app

COPY requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

COPY --chown=appuser:appuser . /app
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

USER appuser
ENTRYPOINT ["python","main.py"]
Original prompt

This section details on the original issue you should resolve

<issue_title>Generate fallback Dockerfile for Python apps without UV support</issue_title>
<issue_description>Currently, the dotnet/aspire tooling only generates a Dockerfile for Python apps when UV is being used. There is no automatic Dockerfile generation as a fallback when UV is not present.

This issue tracks adding support for generating a default Dockerfile for all Python apps when UV is not being used and no Dockerfile exists. The generated Dockerfile should:

  • Detect if a requirements.txt is present and run pip install for dependencies if so.
  • Use the same runtime image as the UV workflow for consistency.
  • Apply this fallback logic for all Python apps, not just certain scenarios.

Reference: The current implementation at src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs only generates a Dockerfile when UV is used. This enhancement will ensure Python apps are supported even without UV and that dependencies are installed correctly via pip when requirements.txt is present.</issue_description>

Comments on the Issue (you are @copilot in this section)

@davidfowl For 13, we will provide a good error message.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits November 3, 2025 10:14
- Modified PythonAppResourceBuilderExtensions to generate Dockerfiles for all Python apps
- Split Dockerfile generation into two methods: GenerateUvDockerfile and GenerateFallbackDockerfile
- Fallback Dockerfiles detect requirements.txt and run pip install if present
- Uses same runtime image (python:X.Y-slim-bookworm) as UV workflow for consistency
- Updated PythonVersionDetector to accept nullable VirtualEnvironment parameter
- Added comprehensive tests for fallback Dockerfile generation

Co-authored-by: davidfowl <[email protected]>
Copilot AI changed the title [WIP] Generate fallback Dockerfile for Python apps without UV support Generate fallback Dockerfile for Python apps without UV Nov 3, 2025
Copilot AI requested a review from davidfowl November 3, 2025 10:22
@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2025

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 12627

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 12627"

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had the one comment. Beyond that, this looks good to me.

@davidfowl davidfowl marked this pull request as ready for review November 3, 2025 18:16
Copilot AI review requested due to automatic review settings November 3, 2025 18:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for generating fallback Dockerfiles for Python applications that don't use UV (the modern Python package installer). Previously, Dockerfiles were only generated for UV-based projects; now projects using traditional requirements.txt or no dependencies at all will also get appropriate Dockerfiles generated.

Key changes:

  • Refactored Dockerfile generation logic to support both UV and non-UV Python projects
  • Added a new GenerateFallbackDockerfile method for traditional Python projects using pip/requirements.txt
  • Updated version detection to handle cases where no virtual environment exists

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

File Description
src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs Refactored Dockerfile generation into separate methods (GenerateUvDockerfile and GenerateFallbackDockerfile) to support both UV and traditional Python projects
src/Aspire.Hosting.Python/PythonVersionDetector.cs Changed virtualEnvironment parameter to nullable and added null check to support version detection without a virtual environment
tests/Aspire.Hosting.Python.Tests/AddPythonAppTests.cs Added three new test methods to verify fallback Dockerfile generation for various scenarios (with/without requirements.txt, different entrypoint types)
tests/Aspire.Hosting.Python.Tests/Snapshots/*.verified.txt Added snapshot files for the new test cases showing expected Dockerfile content

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just had the one comment. Beyond that, this looks good to me.

@eerhardt eerhardt enabled auto-merge (squash) November 3, 2025 22:36
@eerhardt
Copy link
Member

eerhardt commented Nov 3, 2025

/backport to release/13.0

@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2025

Started backporting to release/13.0: https://github.com/dotnet/aspire/actions/runs/19051517713

@eerhardt eerhardt enabled auto-merge (squash) November 3, 2025 22:53
@eerhardt
Copy link
Member

eerhardt commented Nov 3, 2025

/backport to release/13.0

@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2025

Started backporting to release/13.0: https://github.com/dotnet/aspire/actions/runs/19051857972

@eerhardt eerhardt merged commit 0b9832d into main Nov 3, 2025
296 checks passed
@eerhardt eerhardt deleted the copilot/fix-9141961-696529789-efcdcafb-9024-40de-8c54-219d65c69792 branch November 3, 2025 23:14
@dotnet-policy-service dotnet-policy-service bot added this to the 13.1 milestone Nov 3, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Dec 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate fallback Dockerfile for Python apps without UV support

3 participants