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
This commit is contained in:
richardtekula
2025-11-11 16:35:57 +01:00
parent feb583b66c
commit 6d0ee923e2

View File

@@ -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"]