Skip to content

Commit e881d2b

Browse files
authored
Merge pull request #33 from mbsantiago/fix/docker-image
Improved docker file to use uv
2 parents 4cf8c62 + b87b97f commit e881d2b

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

Dockerfile

+54-39
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,79 @@
1-
# == Build User Guide
1+
# === STEP 1 === Build User Guide
22

3-
FROM python:3.12 as guide_builder
3+
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS guide_builder
44

55
RUN mkdir /guide
6-
WORKDIR /back/
7-
COPY back/docs /back/docs
8-
COPY back/guide_requirements.txt /back/guide_requirements.txt
9-
COPY back/mkdocs-guide.yml /back/mkdocs-guide.yml
10-
RUN pip install -r guide_requirements.txt
11-
RUN mkdocs build -f mkdocs-guide.yml -d /guide
126

13-
# == Build Front End
7+
WORKDIR /guide
148

15-
FROM node:latest as web_builder
9+
RUN --mount=type=cache,target=/root/.cache/uv \
10+
--mount=type=bind,source=back/uv.lock,target=uv.lock \
11+
--mount=type=bind,source=back/pyproject.toml,target=pyproject.toml \
12+
uv sync --frozen --no-install-project --dev
13+
14+
COPY back/docs /guide/docs
15+
COPY back/mkdocs-guide.yml /guide/mkdocs-guide.yml
16+
17+
RUN uv run mkdocs build -f mkdocs-guide.yml -d /guide/out/
18+
19+
# === STEP 2 === Build Front End
20+
21+
FROM node:latest AS frontend_builder
1622

1723
RUN mkdir /statics
1824

19-
WORKDIR /front/
25+
WORKDIR /front
2026

21-
COPY front/ /front/
27+
COPY front/ /front
2228

2329
RUN npm install
2430

2531
RUN npm run build
2632

27-
# == Run the web server
33+
# === STEP 3 === Build whombat
2834

29-
FROM python:3.12
35+
# Run the web server
36+
# Use a Python image with uv pre-installed
37+
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder
3038

31-
WORKDIR back/
39+
# Install the project into `/app`
40+
WORKDIR /app
3241

33-
# Install libsndfile1
34-
RUN apt-get update && apt-get install -y \
35-
libsndfile1 \
36-
build-essential \
37-
libffi-dev \
38-
python3-dev
42+
# Enable bytecode compilation
43+
ENV UV_COMPILE_BYTECODE=1
3944

40-
# Set the working directory to /code
41-
WORKDIR /code
45+
# Copy from the cache instead of linking since it's a mounted volume
46+
ENV UV_LINK_MODE=copy
4247

43-
# Copy the current directory contents into the container at /code
48+
# Install the project's dependencies using the lockfile and settings
49+
RUN --mount=type=cache,target=/root/.cache/uv \
50+
--mount=type=bind,source=back/uv.lock,target=uv.lock \
51+
--mount=type=bind,source=back/pyproject.toml,target=pyproject.toml \
52+
uv sync --frozen --no-install-project --no-dev
53+
54+
# Then, add the rest of the project source code and install it
55+
# Installing separately from its dependencies allows optimal layer caching
56+
ADD back /app
4457

4558
# Copy the guide
46-
COPY --from=guide_builder /guide/ /code/src/whombat/user_guide/
59+
COPY --from=guide_builder /guide/out/ /app/src/whombat/user_guide/
4760

4861
# Copy the statics
49-
COPY --from=web_builder /front/out/ /code/src/whombat/statics/
62+
COPY --from=frontend_builder /front/out/ /app/src/whombat/statics/
63+
64+
RUN --mount=type=cache,target=/root/.cache/uv \
65+
uv sync --frozen --no-dev
5066

51-
# Install the Python dependencies
52-
COPY back/requirements.txt /code/requirements.txt
53-
RUN pip install -r requirements.txt
67+
# === STEP 4 === Final Image
5468

55-
# Copy the source code
56-
COPY back/src /code/src
57-
COPY back/app.py /code/app.py
58-
COPY back/pyproject.toml /code/pyproject.toml
59-
COPY back/alembic.ini /code/alembic.ini
60-
COPY back/README.md /code/README.md
61-
COPY back/LICENSE /code/LICENSE
69+
# Then, use a final image without uv
70+
FROM python:3.12-slim-bookworm
6271

63-
# Install Whombat
64-
RUN pip install --no-deps .
72+
# Copy the application from the builder
73+
COPY --from=builder --chown=app:app /app /app
74+
75+
# Place executables in the environment at the front of the path
76+
ENV PATH="/app/.venv/bin:$PATH"
6577

6678
# Create a directory for audio files
6779
RUN mkdir /audio
@@ -84,5 +96,8 @@ ENV WHOMBAT_DOMAIN "localhost"
8496
# Expose the port for the web server
8597
EXPOSE 5000
8698

99+
# Reset the entrypoint, don't invoke `uv`
100+
ENTRYPOINT []
101+
87102
# Run the command to start the web server
88-
CMD ["python", "app.py"]
103+
CMD ["whombat"]

0 commit comments

Comments
 (0)