|
1 | | -FROM python:3.11 |
| 1 | +# Install PDM and use it to export requirements.txt to ensure platform compatibility |
| 2 | +FROM python:3.11-slim as requirements-stage |
2 | 3 |
|
3 | | -ENV LANG=C.UTF-8 |
| 4 | +WORKDIR /tmp |
| 5 | + |
| 6 | +RUN pip install pdm |
| 7 | + |
| 8 | +# Copy the pyproject.toml and pdm.lock files to the /tmp directory |
| 9 | +# Because it uses ./pdm.lock* (ending with a *), it won't crash if that file is not available |
| 10 | +COPY ./pyproject.toml ./pdm.lock* /tmp/ |
| 11 | + |
| 12 | +# Generate requirements.txt |
| 13 | +RUN pdm export --prod --without-hashes -o requirements.txt |
| 14 | + |
| 15 | +# Build the final image |
| 16 | +FROM python:3.11-slim |
| 17 | + |
| 18 | +# ENV LANG=C.UTF-8 # Defined in the base image |
4 | 19 | ENV LC_ALL=C.UTF-8 |
5 | 20 | ENV LC_TIME=C.UTF-8 |
6 | 21 | ENV LISTEN_PORT=8000 |
7 | 22 | # Enable Python optimizations |
8 | 23 | ENV PYTHONOPTIMIZE=1 |
9 | 24 |
|
10 | | -# Create a directory where cached log entries will be stored |
11 | | -# VOLUME /app/log_archive |
12 | | - |
13 | 25 | WORKDIR /app |
14 | 26 |
|
15 | | -# COPY requirements.txt requirements.txt |
16 | | - |
17 | | -# RUN pip install --no-cache-dir --upgrade -r requirements.txt |
18 | | - |
19 | | -# COPY ./src/terminal_sync . |
20 | | - |
21 | | -RUN pip install pdm |
| 27 | +# Copy the requirements.txt file from the previous stage |
| 28 | +COPY --from=requirements-stage /tmp/requirements.txt /app/requirements.txt |
22 | 29 |
|
23 | | -COPY . . |
| 30 | +# Upgrade pip and install / upgrade required packages |
| 31 | +RUN pip install --no-cache-dir --upgrade pip && pip install --no-cache-dir --upgrade -r requirements.txt |
24 | 32 |
|
25 | | -RUN pdm install --prod |
| 33 | +# Copy the application code into the container |
| 34 | +COPY ./src/terminal_sync ./terminal_sync |
26 | 35 |
|
27 | 36 | EXPOSE $LISTEN_PORT/tcp |
28 | 37 |
|
29 | 38 | # FastAPI has a built-in /docs endpoint that we can use to check whether the server is running properly |
30 | 39 | HEALTHCHECK CMD curl --fail http://localhost:$LISTEN_PORT/docs || exit 1 |
31 | 40 |
|
| 41 | +# Run terminal_sync as a module |
32 | 42 | # IMPORTANT: This must listen on 0.0.0.0 or else the application will not be accessible outside of the container |
33 | | -CMD pdm run uvicorn terminal_sync.api:app --host 0.0.0.0 --port $LISTEN_PORT |
34 | | -# CMD ["pdm", "run", "uvicorn", "terminal_sync.api:app", "--host", "0.0.0.0", "--port", $LISTEN_PORT] |
| 43 | +CMD python3 -m terminal_sync --host 0.0.0.0 --port $LISTEN_PORT |
0 commit comments