From 6d0ee923e27f2064476bbe7d8cf40778672c0fd2 Mon Sep 17 00:00:00 2001 From: richardtekula Date: Tue, 11 Nov 2025 16:35:57 +0100 Subject: [PATCH] Fix: Dockerfile Python modules path and CMD syntax - Use virtual environment instead of --user install - Fix PATH to /opt/venv/bin (accessible by appuser) - Fix CMD syntax with shell form - Add curl for healthcheck - Proper init_db.py execution before uvicorn --- ebook_backend&admin_panel/Dockerfile | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/ebook_backend&admin_panel/Dockerfile b/ebook_backend&admin_panel/Dockerfile index f7283c7..ce7e3f5 100644 --- a/ebook_backend&admin_panel/Dockerfile +++ b/ebook_backend&admin_panel/Dockerfile @@ -9,13 +9,17 @@ RUN apt-get update && apt-get install -y \ gcc \ postgresql-client \ libpq-dev \ + curl \ && rm -rf /var/lib/apt/lists/* # Kopírovanie requirements COPY requirements.txt . -# Inštalácia Python dependencies -RUN pip install --no-cache-dir --user -r requirements.txt +# Inštalácia Python dependencies do /opt/venv +RUN python -m venv /opt/venv +ENV PATH="/opt/venv/bin:$PATH" +RUN pip install --no-cache-dir --upgrade pip && \ + pip install --no-cache-dir -r requirements.txt # Production stage FROM python:3.11-slim @@ -23,12 +27,13 @@ FROM python:3.11-slim # Nastavenie environment variables ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ - PATH=/root/.local/bin:$PATH + PATH="/opt/venv/bin:$PATH" # Inštalácia runtime dependencies RUN apt-get update && apt-get install -y \ postgresql-client \ libpq-dev \ + curl \ && rm -rf /var/lib/apt/lists/* # Vytvorenie non-root user pre bezpečnosť @@ -37,8 +42,8 @@ RUN useradd -m -u 1000 appuser # Nastavenie working directory WORKDIR /app -# Kopírovanie Python dependencies z builder stage -COPY --from=builder /root/.local /root/.local +# Kopírovanie Python virtual environment z builder stage +COPY --from=builder /opt/venv /opt/venv # Kopírovanie aplikačných súborov COPY --chown=appuser:appuser . . @@ -56,9 +61,10 @@ EXPOSE 8000 # Healthcheck HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \ - CMD python -c "import requests; requests.get('http://localhost:8000/health')" || exit 1 + CMD curl -f http://localhost:8000/health || exit 1 # Spustenie aplikácie WORKDIR /app/admin-backend -CMD ["python", "init_db.py"] && \ - ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"] + +# Použijeme shell form aby sme mohli spustiť init_db.py a potom uvicorn +CMD ["/bin/sh", "-c", "python init_db.py && uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4"]