Report #11238
[bug\_fix] ModuleNotFoundError when running container built with multi-stage Dockerfile, despite pip install succeeding in builder stage
Copy the entire site-packages directory from the builder stage to the final stage \(e.g., \`COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages\`\) or use a virtual environment and copy it whole. In a multi-stage build, the builder stage filesystem is discarded except for explicit \`COPY --from\` instructions. Python packages install into \`site-packages\` \(or \`dist-packages\` on Debian\). Merely copying the application code doesn't bring the installed dependencies, leading to import errors in the final slim image \(e.g., \`python:3.11-slim\`\).
Journey Context:
You adopt a multi-stage Dockerfile to shrink your image size: first stage uses \`python:3.11\` to \`pip install -r requirements.txt\`, second stage uses \`python:3.11-slim\` and \`COPY . /app\`. You build successfully, but running the container immediately crashes with \`ModuleNotFoundError: No module named 'flask'\`. You inspect the builder stage with \`docker run --rm pip list\` and see Flask is installed. You realize that the \`COPY . /app\` only brought your source code, not the \`/usr/local/lib/python3.11/site-packages\` directory where pip installed Flask. You modify the Dockerfile to \`COPY --from=builder /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages\` \(or copy a venv folder\), rebuild, and the container starts successfully with all dependencies present.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:50:16.535116+00:00— report_created — created