1+ # Use Python 3.11 slim base image
2+ FROM python:3.11-slim
3+
4+ # Set environment variables
5+ ENV PYTHONUNBUFFERED=1 \
6+ # Add the local user's bin directory to PATH
7+ PATH="/home/appuser/.local/bin:$PATH"
8+
9+ # Create a non-root user and group
10+ RUN groupadd --gid 1000 appuser && \
11+ useradd --uid 1000 --gid appuser --create-home appuser
12+
13+ # Set working directory
14+ WORKDIR /opt/emon_tools
15+
16+ # Install build dependencies if needed (e.g., for any compiled packages)
17+ RUN apt-get update && apt-get install -y --no-install-recommends \
18+ gcc \
19+ dos2unix \
20+ default-mysql-client \
21+ python3-dev \
22+ default-libmysqlclient-dev \
23+ build-essential \
24+ pkg-config \
25+ && rm -rf /var/lib/apt/lists/*
26+
27+ # Copy requirements and install them as the non-root user
28+ COPY --chown=appuser:appuser requirements-docker-dev.txt .
29+
30+ # Create the datas and static directories for storing data
31+ RUN mkdir -p /opt/emon_tools/datas /opt/emon_tools/static /opt/emon_tools/backend \
32+ && chown -R appuser:appuser /opt/emon_tools/datas /opt/emon_tools/static /opt/emon_tools/backend
33+
34+ # Switch to the non-root user
35+ USER appuser
36+
37+ # Install Python dependencies
38+ # Use --no-cache-dir to avoid caching the packages in the image
39+ # Use --user to install packages in the user's home directory
40+ RUN pip install --no-cache-dir --user --upgrade pip && \
41+ pip install --no-cache-dir --user -r requirements-docker-dev.txt
42+
43+ # Copy the backend source code
44+ COPY --chown=appuser:appuser . ./backend
45+
46+ # Replace env file with the one for Docker
47+ COPY --chown=appuser:appuser .env.docker ./backend/.env
48+
49+ RUN dos2unix /opt/emon_tools/backend/scripts/docker_start.sh
50+ RUN dos2unix /opt/emon_tools/backend/scripts/pre_start.sh
51+ RUN dos2unix /opt/emon_tools/backend/scripts/wait_for_db.sh
52+
53+ # Ensure scripts have execution permission
54+ RUN chmod +x /opt/emon_tools/backend/scripts/docker_start.sh \
55+ && chmod +x /opt/emon_tools/backend/scripts/pre_start.sh \
56+ && chmod +x /opt/emon_tools/backend/scripts/wait_for_db.sh
57+
58+ # Set the PYTHONPATH to ensure the backend module is found
59+ ENV PYTHONPATH=/opt/emon_tools
60+
61+ # Expose FastAPI port (use 8000)
62+ EXPOSE 8000
63+
64+ # Use the entrypoint script as CMD, which will run migrations and then launch uvicorn
65+ CMD ["./backend/scripts/docker_start.sh" ]
0 commit comments