Skip to content

Commit 4dab956

Browse files
committed
Update Dockerfile and docker-compose.yaml
Signed-off-by: Federico Busetti <[email protected]>
1 parent 73e0bb0 commit 4dab956

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

Dockerfile

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,78 +10,76 @@ RUN chown nonroot:nonroot /app
1010
# Creating a separate directory for venvs allows to easily
1111
# copy them from the builder and to mount the application
1212
# for local development
13-
RUN mkdir /poetryvenvs && chown nonroot:nonroot /poetryvenvs
13+
RUN mkdir /venv && chown nonroot:nonroot /venv
14+
ENV PATH="/venv/bin:$PATH"
1415

1516
# Install necessary runtime libraries (e.g. libmysql)
1617
RUN apt-get update \
1718
&& apt-get install -y --no-install-recommends \
1819
make \
1920
&& rm -rf /var/lib/apt/lists/*
2021

21-
# Update pip and install poetry
22-
RUN pip install --no-cache-dir -U pip
23-
RUN pip install --no-cache-dir -U poetry
24-
25-
# We run everything by poetry run from now on, so that PATH will be handled
26-
# for binaries installed in virtual environments
27-
ENTRYPOINT ["poetry", "run"]
28-
2922
FROM base AS base_builder
23+
ENV UV_PROJECT_ENVIRONMENT=/venv
24+
# Enable bytecode compilation
25+
ENV UV_COMPILE_BYTECODE=1
26+
3027
# Install build system requirements (gcc, library headers, etc.)
3128
# for compiled Python requirements like psycopg2
3229
RUN apt-get update \
3330
&& apt-get install -y --no-install-recommends \
3431
build-essential gcc git \
3532
&& rm -rf /var/lib/apt/lists/*
3633

34+
COPY --from=ghcr.io/astral-sh/uv:0.5.13 /uv /uvx /bin/
35+
3736
# From here we shouldn't need anymore a root user
3837
# Switch to nonroot and config poetry
3938
USER nonroot
40-
RUN poetry config virtualenvs.path /poetryvenvs
4139

4240
COPY --chown=nonroot:nonroot pyproject.toml .
43-
COPY --chown=nonroot:nonroot poetry.lock .
41+
COPY --chown=nonroot:nonroot uv.lock .
4442
COPY --chown=nonroot:nonroot Makefile .
4543

46-
# Test image, contains all files and dependencies
44+
# Dev image, contains all files and dependencies
4745
FROM base_builder AS dev
4846
COPY --chown=nonroot:nonroot . .
49-
RUN make dev-dependencies
47+
RUN --mount=type=cache,target=~/.cache/uv \
48+
make dev-dependencies
49+
5050
# Note that opentelemetry doesn't play well together with uvicorn reloader
5151
# when signals are propagated, we disable it in dev image default CMD
5252
CMD ["uvicorn", "http_app:create_app", "--host", "0.0.0.0", "--port", "8000", "--factory", "--reload"]
5353

5454
# Installs requirements to run production dramatiq application
5555
FROM base_builder AS dramatiq_builder
56-
RUN poetry install --no-root
56+
RUN --mount=type=cache,target=~/.cache/uv \
57+
uv sync --no-dev --no-install-project --frozen --no-editable
5758

5859
# Installs requirements to run production http application
5960
FROM base_builder AS http_builder
60-
RUN poetry install --no-root --with http
61+
RUN --mount=type=cache,target=~/.cache/uv \
62+
uv sync --no-dev --group http --no-install-project --frozen --no-editable
6163

62-
# Copy the shared python packages
64+
# Create the base app with the common python packages
6365
FROM base AS base_app
6466
USER nonroot
65-
RUN poetry config virtualenvs.path /poetryvenvs
66-
COPY --chown=nonroot:nonroot pyproject.toml .
67-
COPY --chown=nonroot:nonroot poetry.lock .
6867
COPY --chown=nonroot:nonroot src/alembic ./alembic
6968
COPY --chown=nonroot:nonroot src/domains ./domains
7069
COPY --chown=nonroot:nonroot src/gateways ./gateways
7170
COPY --chown=nonroot:nonroot src/common ./common
7271
COPY --chown=nonroot:nonroot src/alembic.ini .
73-
COPY --chown=nonroot:nonroot Makefile .
7472

7573
# Copy the http python package and requirements from relevant builder
7674
FROM base_app AS http_app
77-
COPY --from=http_builder /poetryvenvs /poetryvenvs
75+
COPY --from=http_builder /venv /venv
7876
COPY --chown=nonroot:nonroot src/http_app ./http_app
7977
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
8078
CMD ["opentelemetry-instrument", "uvicorn", "http_app:create_app", "--host", "0.0.0.0", "--port", "8000", "--factory"]
8179

8280
# Copy the dramatiq python package and requirements from relevant builder
8381
FROM base_app AS dramatiq_app
84-
COPY --from=dramatiq_builder /poetryvenvs /poetryvenvs
82+
COPY --from=dramatiq_builder /venv /venv
8583
COPY --chown=nonroot:nonroot src/dramatiq_worker ./dramatiq_worker
8684
# Run CMD using array syntax, so it's uses `exec` and runs as PID1
8785
# TODO: Review processes/threads

docker-compose.yaml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
services:
2-
32
otel-collector:
43
image: otel/opentelemetry-collector-contrib
54
volumes:
@@ -13,11 +12,9 @@ services:
1312
dockerfile: Dockerfile
1413
context: .
1514
target: dramatiq_app
15+
env_file: local.env
1616
environment:
17-
WATCHFILES_FORCE_POLLING: true
1817
OTEL_SERVICE_NAME: "bootstrap-fastapi-dramatiq-worker"
19-
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector:4317"
20-
DRAMATIQ__REDIS_URL: "redis://redis:6379/0"
2118
working_dir: "/app/src"
2219
volumes:
2320
- '.:/app'
@@ -40,10 +37,9 @@ services:
4037
dockerfile: Dockerfile
4138
context: .
4239
target: dev
40+
env_file: local.env
4341
environment:
44-
WATCHFILES_FORCE_POLLING: true
4542
OTEL_SERVICE_NAME: "bootstrap-fastapi-dev"
46-
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector:4317"
4743
ports:
4844
- '8000:8000'
4945
working_dir: "/app/src"
@@ -72,9 +68,9 @@ services:
7268
depends_on:
7369
- redis
7470
- otel-collector
71+
env_file: local.env
7572
environment:
7673
OTEL_SERVICE_NAME: "bootstrap-fastapi-http"
77-
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector:4317"
7874
ports:
7975
- '8001:8000'
8076
volumes:

local.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DRAMATIQ__REDIS_URL: "redis://redis:6379/0"
2+
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector:4317"

0 commit comments

Comments
 (0)