File size: 2,536 Bytes
2e968f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a543e3b
2e968f0
 
a543e3b
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Stage 1: Build the dependencies
FROM python:3.12-bullseye AS builder

# Install required system packages
RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    build-essential \
    cmake \
    libopenblas-dev \
    libomp-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Set the working directory to /app
WORKDIR /app

# Copy requirements and install dependencies
COPY requirements.txt /app/

# Install Python dependencies and torchmcubes
RUN pip install --upgrade pip setuptools wheel \
    && pip install -r requirements.txt \
    && pip install git+https://github.com/tatsy/torchmcubes.git@3aef8afa5f21b113afc4f4ea148baee850cbd472 \
    && rm -rf ~/.cache/pip

# Copy the application files
COPY . /app

# Configure Git to treat the directory as safe before switching to the final stage
RUN git config --global --add safe.directory /app

# Stage 2: Final image
FROM python:3.12-slim-bullseye

# Set up a new user named "user"
RUN useradd user

# Set the home environment variable and PATH
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH

# Set the working directory to the user's home directory
WORKDIR $HOME/app

# Copy the application files and installed packages from the builder stage
COPY --from=builder /app $HOME/app
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# Change ownership of the app directory to the user
RUN chown -R user:user $HOME/app

# Install git in the final stage
RUN apt-get update && apt-get install -y --no-install-recommends git \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*
    
# Expose secrets at build time and store them in a file
RUN --mount=type=secret,id=AWS_ACCESS_KEY_ID,mode=0444,required=true \
 git config --global --add safe.directory $HOME/app && \
 git init && \
 git remote add sec1 $(cat /run/secrets/AWS_ACCESS_KEY_ID)
 
RUN --mount=type=secret,id=AWS_SECRET_ACCESS_KEY,mode=0444,required=true \
 git config --global --add safe.directory $HOME/app && \
 git init && \
 git remote add sec2 $(cat /run/secrets/AWS_SECRET_ACCESS_KEY)
 
RUN --mount=type=secret,id=AWS_DEFAULT_REGION,mode=0444,required=true \
 git config --global --add safe.directory $HOME/app && \
 git init && \
 git remote add sec3 $(cat /run/secrets/AWS_DEFAULT_REGION)
 
# Switch to the "user" user
USER user

EXPOSE 7860

# Set the entry point to run the FastAPI application
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]