-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
executable file
·49 lines (35 loc) · 1.48 KB
/
Copy pathDockerfile
File metadata and controls
executable file
·49 lines (35 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Stage 1: Build the Application
# We use python:3.12 as the base for building and installing dependencies.
FROM python:3.12 AS build
# Set the working directory inside the container
WORKDIR /usr/src/app
# Install system dependencies if needed
RUN apt-get update && apt-get install -y --no-install-recommends build-essential && rm -rf /var/lib/apt/lists/*
# Create a virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Copy requirements.txt if it exists (using wildcard to avoid build failure)
COPY requirements.tx[t] ./requirements.txt
# Install Python dependencies only if requirements.txt exists
RUN pip install --upgrade pip && if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
# Copy the rest of the application source code
COPY . .
# Stage 2: Create the Final Production Image
# We use python:3.12 as the runtime image with all the necessary tools.
FROM python:3.12
# Set the working directory
WORKDIR /usr/src/app
# Copy the virtual environment from the build stage
COPY --from=build /opt/venv /opt/venv
# Copy the application code
COPY --from=build /usr/src/app .
# Set the virtual environment as the active Python environment
ENV PATH="/opt/venv/bin:$PATH"
# Create a non-root user to run the application
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /usr/src/app
USER appuser
# Expose the port your app runs on
ENV PORT=8080
EXPOSE $PORT
# Define the command to start your application
CMD ["python", "app.py"]