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:
@@ -9,13 +9,17 @@ RUN apt-get update && apt-get install -y \
|
|||||||
gcc \
|
gcc \
|
||||||
postgresql-client \
|
postgresql-client \
|
||||||
libpq-dev \
|
libpq-dev \
|
||||||
|
curl \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Kopírovanie requirements
|
# Kopírovanie requirements
|
||||||
COPY requirements.txt .
|
COPY requirements.txt .
|
||||||
|
|
||||||
# Inštalácia Python dependencies
|
# Inštalácia Python dependencies do /opt/venv
|
||||||
RUN pip install --no-cache-dir --user -r requirements.txt
|
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
|
# Production stage
|
||||||
FROM python:3.11-slim
|
FROM python:3.11-slim
|
||||||
@@ -23,12 +27,13 @@ FROM python:3.11-slim
|
|||||||
# Nastavenie environment variables
|
# Nastavenie environment variables
|
||||||
ENV PYTHONUNBUFFERED=1 \
|
ENV PYTHONUNBUFFERED=1 \
|
||||||
PYTHONDONTWRITEBYTECODE=1 \
|
PYTHONDONTWRITEBYTECODE=1 \
|
||||||
PATH=/root/.local/bin:$PATH
|
PATH="/opt/venv/bin:$PATH"
|
||||||
|
|
||||||
# Inštalácia runtime dependencies
|
# Inštalácia runtime dependencies
|
||||||
RUN apt-get update && apt-get install -y \
|
RUN apt-get update && apt-get install -y \
|
||||||
postgresql-client \
|
postgresql-client \
|
||||||
libpq-dev \
|
libpq-dev \
|
||||||
|
curl \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# Vytvorenie non-root user pre bezpečnosť
|
# Vytvorenie non-root user pre bezpečnosť
|
||||||
@@ -37,8 +42,8 @@ RUN useradd -m -u 1000 appuser
|
|||||||
# Nastavenie working directory
|
# Nastavenie working directory
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Kopírovanie Python dependencies z builder stage
|
# Kopírovanie Python virtual environment z builder stage
|
||||||
COPY --from=builder /root/.local /root/.local
|
COPY --from=builder /opt/venv /opt/venv
|
||||||
|
|
||||||
# Kopírovanie aplikačných súborov
|
# Kopírovanie aplikačných súborov
|
||||||
COPY --chown=appuser:appuser . .
|
COPY --chown=appuser:appuser . .
|
||||||
@@ -56,9 +61,10 @@ EXPOSE 8000
|
|||||||
|
|
||||||
# Healthcheck
|
# Healthcheck
|
||||||
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
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
|
# Spustenie aplikácie
|
||||||
WORKDIR /app/admin-backend
|
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"]
|
||||||
|
|||||||
Reference in New Issue
Block a user