Why isn't packages installed in my multi-stage Docker build using pip?

Why isn't packages installed in my multi-stage Docker build using pip?
python
Ethan Jackson

I'm working on a Python project with a multi-stage Docker build and running into an issue where pydantic (just example) isn't installed, even though pip is present and working in the final image.

Here's my project structure:

project-root/ ├── docker-compose.yml ├── vector_db_service/ │ ├── app/ │ │ └── __init__.py │ ├── Dockerfile │ ├── pyproject.toml │ ├── .env

docker-compose.yml:

services: vector_db_service: container_name: vector_db_service build: context: ./vector_db_service dockerfile: Dockerfile command: tail -f /dev/null env_file: - ./vector_db_service/.env

Dockerfile:

# Build stage FROM python:3.13-slim AS compile-image RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" RUN pip install --no-cache-dir --upgrade pip # Final image FROM python:3.13-slim COPY --from=compile-image /opt/venv /opt/venv WORKDIR /app ENV HOME=/app ENV PATH="/opt/venv/bin:$PATH" RUN addgroup --system app && adduser --system --group app COPY . . RUN chown -R app:app $HOME RUN chown -R app:app "/opt/venv/" USER app RUN pip install -e pydantic

The last line, RUN pip install -e pydantic, doesn't install anything. The build finishes successfully, but the package isn’t installed. I confirmed that pip is installed in the final image.

I’ve tried other variations like RUN pip install pydantic or RUN pip install -e ., but they didn't change the outcome.

My pyproject.toml does list pydantic as a dependency. Do I need to install from the project root, or am I missing something in the build process?

Any help would be greatly appreciated. Thank you in advance!

Answer

Try to copy project files which you need to
install

FROM python:3.13-slim AS compile-image RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" RUN pip install --no-cache-dir --upgrade pip setuptools wheel WORKDIR /app COPY pyproject.toml ./ COPY vector_db_service ./vector_db_service RUN pip install --no-cache-dir . FROM python:3.13-slim COPY --from=compile-image /opt/venv /opt/venv WORKDIR /app ENV HOME=/app ENV PATH="/opt/venv/bin:$PATH" RUN addgroup --system app && adduser --system --group app COPY . . RUN chown -R app:app $HOME RUN chown -R app:app "/opt/venv/" USER app

Related Articles