Skip to content

Commit 14ecd5f

Browse files
committed
Update docker build and compose
1 parent 7f4e525 commit 14ecd5f

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

containers/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.venv

containers/Dockerfile

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
1-
# Install uv
2-
FROM python:3.12-slim
3-
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
1+
# First, build the application in the `/app` directory
2+
FROM ghcr.io/astral-sh/uv:bookworm-slim AS builder
3+
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy
44

5-
# Install git, gcc etc.
6-
RUN apt-get update && apt-get upgrade -y && apt-get install -y git build-essential
7-
8-
# Change the working directory to the `app` directory
9-
WORKDIR /app
5+
# Configure the Python directory so it is consistent
6+
ENV UV_PYTHON_INSTALL_DIR=/python
107

11-
# Enable bytecode compilation
12-
ENV UV_COMPILE_BYTECODE=1
8+
# Only use the managed Python version
9+
ENV UV_PYTHON_PREFERENCE=only-managed
1310

14-
# Copy from the cache instead of linking since it's a mounted volume
15-
ENV UV_LINK_MODE=copy
11+
# Install Python before the project for caching
12+
RUN uv python install 3.12
1613

17-
# Install dependencies
14+
WORKDIR /app
1815
RUN --mount=type=cache,target=/root/.cache/uv \
1916
--mount=type=bind,source=uv.lock,target=uv.lock \
2017
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
21-
uv sync -v --locked --no-install-project
18+
uv sync --locked --no-install-project --no-dev
19+
COPY . /app
20+
RUN --mount=type=cache,target=/root/.cache/uv \
21+
uv sync --locked --no-dev
2222

23-
# Copy contents into the image
24-
ADD . /app
23+
# Then, use a final image without uv
24+
FROM debian:bookworm-slim
2525

26-
# Sync the project
27-
RUN --mount=type=cache,target=/root/.cache/uv \
28-
uv sync --locked
26+
# Install git, gcc etc.
27+
RUN apt-get update && apt-get upgrade -y && apt-get install -y git build-essential
28+
29+
# Setup a non-root user
30+
RUN groupadd --system --gid 999 nonroot \
31+
&& useradd --system --gid 999 --uid 999 --create-home nonroot
32+
33+
# Copy the Python version
34+
COPY --from=builder --chown=python:python /python /python
35+
36+
# Copy the application from the builder
37+
COPY --from=builder --chown=nonroot:nonroot /app /app
38+
39+
# Place executables in the environment at the front of the path
40+
ENV PATH="/app/.venv/bin:$PATH"
41+
42+
# Use the non-root user to run our application
43+
USER nonroot
44+
45+
# Use `/app` as the working directory
46+
WORKDIR /app
2947

30-
# Run app.py
31-
CMD ["uv", "run", "run_app.py"]
48+
# Run the application
49+
CMD ["ml_peg", "app"]

containers/compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ services:
55
- 8050:8050
66
working_dir: /app
77
volumes:
8-
- ./:/app
8+
- ../ml_peg/app/data:/app/ml_peg/app/data

0 commit comments

Comments
 (0)