From ca5f5e338391193e9500154b561b5fc82ee3cf46 Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Sun, 4 Jan 2026 00:58:24 +0100 Subject: [PATCH] refactor(environment): update virtualenv path to `/opt/venv` in Docker setup - Adjusted `docker-compose.dev.yml` to reflect the new venv location. - Modified entrypoint script and Dockerfile to reference `/opt/venv` for isolated dependencies. - Improved bind mount setup to prevent venv overwrites during development. --- backend/Dockerfile | 18 ++++++++++++------ backend/entrypoint.sh | 10 +++++----- docker-compose.dev.yml | 7 +------ 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/backend/Dockerfile b/backend/Dockerfile index 554865b..7188511 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -7,7 +7,10 @@ ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONPATH=/app \ UV_COMPILE_BYTECODE=1 \ UV_LINK_MODE=copy \ - UV_NO_CACHE=1 + UV_NO_CACHE=1 \ + UV_PROJECT_ENVIRONMENT=/opt/venv \ + VIRTUAL_ENV=/opt/venv \ + PATH="/opt/venv/bin:$PATH" # Install system dependencies and uv RUN apt-get update && \ @@ -20,7 +23,7 @@ RUN apt-get update && \ # Copy dependency files COPY pyproject.toml uv.lock ./ -# Install dependencies using uv (development mode with dev dependencies) +# Install dependencies using uv into /opt/venv (outside /app to survive bind mounts) RUN uv sync --extra dev --frozen # Copy application code @@ -45,7 +48,10 @@ ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONPATH=/app \ UV_COMPILE_BYTECODE=1 \ UV_LINK_MODE=copy \ - UV_NO_CACHE=1 + UV_NO_CACHE=1 \ + UV_PROJECT_ENVIRONMENT=/opt/venv \ + VIRTUAL_ENV=/opt/venv \ + PATH="/opt/venv/bin:$PATH" # Install system dependencies and uv RUN apt-get update && \ @@ -58,7 +64,7 @@ RUN apt-get update && \ # Copy dependency files COPY pyproject.toml uv.lock ./ -# Install only production dependencies using uv (no dev dependencies) +# Install only production dependencies using uv into /opt/venv RUN uv sync --frozen --no-dev # Copy application code @@ -67,7 +73,7 @@ COPY entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/entrypoint.sh # Set ownership to non-root user -RUN chown -R appuser:appuser /app +RUN chown -R appuser:appuser /app /opt/venv # Switch to non-root user USER appuser @@ -77,4 +83,4 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ CMD curl -f http://localhost:8000/health || exit 1 ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] -CMD ["uv", "run", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/backend/entrypoint.sh b/backend/entrypoint.sh index ba5b34b..a043d0a 100755 --- a/backend/entrypoint.sh +++ b/backend/entrypoint.sh @@ -1,11 +1,11 @@ #!/bin/bash set -e -# Ensure the project's virtualenv binaries are on PATH so commands like -# 'uvicorn' work even when not prefixed by 'uv run'. This matches how uv -# installs the env into /app/.venv in our containers. -if [ -d "/app/.venv/bin" ]; then - export PATH="/app/.venv/bin:$PATH" +# Ensure the virtualenv binaries are on PATH. Dependencies are installed +# to /opt/venv (not /app/.venv) to survive bind mounts in development. +if [ -d "/opt/venv/bin" ]; then + export PATH="/opt/venv/bin:$PATH" + export VIRTUAL_ENV="/opt/venv" fi # Only the backend service should run migrations and init_db diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index e30fdc7..dcaf812 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -40,8 +40,7 @@ services: volumes: - ./backend:/app - ./uploads:/app/uploads - # Exclude local .venv from bind mount to use container's .venv - - /app/.venv + # Note: venv is at /opt/venv (not /app/.venv) so bind mount doesn't affect it ports: - "8000:8000" env_file: @@ -76,7 +75,6 @@ services: target: development volumes: - ./backend:/app - - /app/.venv env_file: - .env environment: @@ -99,7 +97,6 @@ services: target: development volumes: - ./backend:/app - - /app/.venv env_file: - .env environment: @@ -122,7 +119,6 @@ services: target: development volumes: - ./backend:/app - - /app/.venv env_file: - .env environment: @@ -145,7 +141,6 @@ services: target: development volumes: - ./backend:/app - - /app/.venv env_file: - .env environment: