Skip to content

Commit 6f4e364

Browse files
committed
fix: update Dockerfiles to use consistent virtualenv path and caching for dev dependencies
1 parent 259e22d commit 6f4e364

4 files changed

Lines changed: 70 additions & 31 deletions

File tree

services/admin-backend/Dockerfile

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
FROM --platform=linux/amd64 python:3.13-bookworm AS build
22

33
ARG dev=0
4-
ENV POETRY_VIRTUALENVS_PATH=/app/services/admin-backend/.venv
5-
ENV POETRY_VERSION=2.1.3
4+
ENV POETRY_VIRTUALENVS_PATH=/opt/.venv \
5+
POETRY_VIRTUALENVS_CREATE=false \
6+
POETRY_VIRTUALENVS_IN_PROJECT=false \
7+
VIRTUAL_ENV=/opt/.venv \
8+
PATH=/opt/.venv/bin:$PATH \
9+
POETRY_VERSION=2.1.3
610

711
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
812
&& DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential --no-install-recommends make && \
@@ -19,7 +23,8 @@ COPY services/admin-backend/pyproject.toml services/admin-backend/poetry.lock ./
1923
RUN mkdir log && chmod 700 log
2024
RUN touch /app/services/admin-backend/log/logfile.log && chmod 600 /app/services/admin-backend/log/logfile.log
2125

22-
RUN poetry config virtualenvs.create false && \
26+
# Bust cache when dev flag changes
27+
RUN echo "CACHE_BUST_DEV=$dev" && poetry config virtualenvs.create false && \
2328
if [ "$dev" = "1" ]; then \
2429
poetry install --no-interaction --no-ansi --no-root --with dev; \
2530
else \
@@ -31,7 +36,7 @@ ARG dev=0
3136

3237
RUN adduser --disabled-password --gecos "" --uid 10001 nonroot
3338

34-
ENV POETRY_VIRTUALENVS_PATH=/app/services/admin-backend/.venv
39+
ENV POETRY_VIRTUALENVS_PATH=/opt/.venv
3540
COPY --from=build --chown=nonroot:nonroot ${POETRY_VIRTUALENVS_PATH} ${POETRY_VIRTUALENVS_PATH}
3641
COPY --from=build /usr/local/bin/ /usr/local/bin/
3742
COPY --from=build /usr/bin/make /usr/bin/make
@@ -44,6 +49,12 @@ WORKDIR /app/services/admin-backend
4449

4550
COPY --chown=nonroot:nonroot services/admin-backend .
4651

52+
# Ensure poetry reuses existing virtualenv when running as nonroot
53+
ENV POETRY_VIRTUALENVS_CREATE=false \
54+
POETRY_VIRTUALENVS_IN_PROJECT=false \
55+
VIRTUAL_ENV=/opt/.venv \
56+
PATH=/opt/.venv/bin:$PATH
57+
4758
# cleanup
4859
RUN apt-get clean autoclean
4960
RUN apt-get autoremove --yes
@@ -52,13 +63,12 @@ RUN if [ "$dev" = "0" ]; then \
5263
while read -r shell; do rm -f "$shell"; done < /etc/shells; \
5364
rm -rf /var/lib/{apt,dpkg,cache,log}/ \
5465
else \
55-
echo "POETRY_VIRTUALENVS_PATH=/app/services/admin-backend/.venv" >> /etc/environment;\
56-
export POETRY_VIRTUALENVS_PATH=/app/services/admin-backend/.venv;\
66+
echo "POETRY_VIRTUALENVS_PATH=/opt/.venv" >> /etc/environment;\
67+
export POETRY_VIRTUALENVS_PATH=/opt/.venv;\
5768
export PATH="${POETRY_VIRTUALENVS_PATH}/bin:$PATH";\
5869
fi
5970

6071

6172
USER nonroot
6273
COPY --from=build --chown=nonroot:nonroot /app/services/admin-backend/log /app/services/admin-backend/log
6374

64-
ENV PATH="${POETRY_VIRTUALENVS_PATH}/bin:${PATH}"

services/document-extractor/Dockerfile

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
FROM --platform=linux/amd64 python:3.13-bookworm AS build
22

33
ARG dev=0
4-
ENV POETRY_VIRTUALENVS_PATH=/app/services/document-extractor/.venv
5-
ENV POETRY_VERSION=2.1.3
4+
ENV POETRY_VIRTUALENVS_PATH=/opt/.venv \
5+
POETRY_VIRTUALENVS_CREATE=false \
6+
POETRY_VIRTUALENVS_IN_PROJECT=false \
7+
VIRTUAL_ENV=/opt/.venv \
8+
PATH=/opt/.venv/bin:$PATH \
9+
POETRY_VERSION=2.1.3
610

711
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
812
&& DEBIAN_FRONTEND=noninteractive apt-get install -y build-essential --no-install-recommends make \
@@ -23,7 +27,8 @@ COPY services/document-extractor/pyproject.toml services/document-extractor/poet
2327
RUN mkdir log && chmod 700 log
2428
RUN touch /app/services/document-extractor/log/logfile.log && chmod 600 /app/services/document-extractor/log/logfile.log
2529

26-
RUN poetry config virtualenvs.create false &&\
30+
# Cache bust with dev arg so dev dependencies install when dev=1
31+
RUN echo "CACHE_BUST_DEV=$dev" && poetry config virtualenvs.create false &&\
2732
if [ "$dev" = "1" ]; then \
2833
poetry install --no-interaction --no-ansi --no-root --with dev; \
2934
else \
@@ -35,7 +40,7 @@ ARG dev=0
3540

3641
RUN adduser --disabled-password --gecos "" --uid 10001 nonroot
3742

38-
ENV POETRY_VIRTUALENVS_PATH=/app/services/document-extractor/.venv
43+
ENV POETRY_VIRTUALENVS_PATH=/opt/.venv
3944
COPY --from=build --chown=nonroot:nonroot ${POETRY_VIRTUALENVS_PATH} ${POETRY_VIRTUALENVS_PATH}
4045
COPY --from=build /usr/local/bin/ /usr/local/bin/
4146
COPY --from=build /usr/bin/ /usr/bin/
@@ -50,6 +55,12 @@ WORKDIR /app/services/document-extractor
5055
COPY --chown=nonroot:nonroot services/document-extractor .
5156

5257

58+
# Ensure poetry reuses existing virtualenv when running as nonroot
59+
ENV POETRY_VIRTUALENVS_CREATE=false \
60+
POETRY_VIRTUALENVS_IN_PROJECT=false \
61+
VIRTUAL_ENV=/opt/.venv \
62+
PATH=/opt/.venv/bin:$PATH
63+
5364
# cleanup
5465
RUN apt-get clean autoclean
5566
RUN apt-get autoremove --yes
@@ -58,13 +69,11 @@ RUN if [ "$dev" = "0" ]; then \
5869
while read -r shell; do rm -f "$shell"; done < /etc/shells; \
5970
rm -rf /var/lib/{apt,dpkg,cache,log}/ \
6071
else \
61-
echo "POETRY_VIRTUALENVS_PATH=/app/services/document-extractor/.venv" >> /etc/environment;\
62-
export POETRY_VIRTUALENVS_PATH=/app/services/document-extractor/.venv;\
72+
echo "POETRY_VIRTUALENVS_PATH=/opt/.venv" >> /etc/environment;\
73+
export POETRY_VIRTUALENVS_PATH=/opt/.venv;\
6374
export PATH="${POETRY_VIRTUALENVS_PATH}/bin:$PATH";\
6475
fi
6576

6677

6778
USER nonroot
6879
COPY --from=build --chown=nonroot:nonroot /app/services/document-extractor/log /app/services/document-extractor/log
69-
70-
ENV PATH="${POETRY_VIRTUALENVS_PATH}/bin:${PATH}"

services/mcp-server/Dockerfile

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
FROM --platform=linux/amd64 python:3.13.7-bookworm AS build
22

33
ARG dev=0
4-
ENV POETRY_VIRTUALENVS_PATH=/app/services/mcp-server/.venv
5-
ENV POETRY_VERSION=2.1.3
4+
ENV POETRY_VIRTUALENVS_PATH=/opt/.venv \
5+
POETRY_VIRTUALENVS_CREATE=false \
6+
POETRY_VIRTUALENVS_IN_PROJECT=false \
7+
VIRTUAL_ENV=/opt/.venv \
8+
PATH=/opt/.venv/bin:$PATH \
9+
POETRY_VERSION=2.1.3
610

711
WORKDIR /app
812

@@ -19,7 +23,8 @@ COPY services/mcp-server/pyproject.toml services/mcp-server/poetry.lock ./
1923
RUN mkdir log && chmod 700 log
2024
RUN touch /app/services/mcp-server/log/logfile.log && chmod 600 /app/services/mcp-server/log/logfile.log
2125

22-
RUN poetry config virtualenvs.create false &&\
26+
# Cache bust with dev arg so dev dependencies install when dev=1
27+
RUN echo "CACHE_BUST_DEV=$dev" && poetry config virtualenvs.create false &&\
2328
if [ "$dev" = "1" ]; then \
2429
poetry install --no-interaction --no-ansi --no-root --with dev; \
2530
else \
@@ -31,7 +36,8 @@ ARG dev=0
3136

3237
RUN adduser --disabled-password --gecos "" --uid 10001 nonroot
3338

34-
ENV POETRY_VIRTUALENVS_PATH=/app/services/mcp-server/.venv
39+
ENV POETRY_VIRTUALENVS_PATH=/opt/.venv
40+
3541
COPY --from=build --chown=nonroot:nonroot ${POETRY_VIRTUALENVS_PATH} ${POETRY_VIRTUALENVS_PATH}
3642
COPY --from=build /usr/local/bin/ /usr/local/bin/
3743
COPY --from=build /usr/bin/make /usr/bin/make
@@ -42,6 +48,13 @@ COPY --chown=nonroot:nonroot services/mcp-server/src ./src
4248
COPY --chown=nonroot:nonroot services/mcp-server/tests ./tests
4349
COPY --chown=nonroot:nonroot services/mcp-server/pyproject.toml services/mcp-server/poetry.lock ./
4450
COPY --chown=nonroot:nonroot services/mcp-server/Makefile ./
51+
52+
# Ensure poetry reuses existing virtualenv when running as nonroot
53+
ENV POETRY_VIRTUALENVS_CREATE=false \
54+
POETRY_VIRTUALENVS_IN_PROJECT=false \
55+
VIRTUAL_ENV=/opt/.venv \
56+
PATH=/opt/.venv/bin:$PATH
57+
4558
# cleanup
4659
RUN apt-get clean autoclean
4760
RUN apt-get autoremove --yes
@@ -50,15 +63,13 @@ RUN if [ "$dev" = "0" ]; then \
5063
while read -r shell; do rm -f "$shell"; done < /etc/shells; \
5164
rm -rf /var/lib/{apt,dpkg,cache,log}/ \
5265
else \
53-
echo "POETRY_VIRTUALENVS_PATH=/app/services/mcp-server/.venv" >> /etc/environment;\
54-
export POETRY_VIRTUALENVS_PATH=/app/services/mcp-server/.venv;\
66+
echo "POETRY_VIRTUALENVS_PATH=/opt/.venv" >> /etc/environment;\
67+
export POETRY_VIRTUALENVS_PATH=/opt/.venv;\
5568
export PATH="${POETRY_VIRTUALENVS_PATH}/bin:$PATH";\
5669
fi
5770

5871

5972
USER nonroot
6073
COPY --from=build --chown=nonroot:nonroot /app/services/mcp-server/log /app/services/mcp-server/log
6174

62-
ENV PATH="${POETRY_VIRTUALENVS_PATH}/bin:${PATH}"
63-
6475
CMD [ "poetry", "run", "python", "src/main.py" ]

services/rag-backend/Dockerfile

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
FROM --platform=linux/amd64 python:3.13-bookworm AS build
22

33
ARG dev=0
4-
ENV POETRY_VIRTUALENVS_PATH=/app/services/rag-backend/.venv
5-
ENV POETRY_VERSION=2.1.3
4+
ENV POETRY_VIRTUALENVS_PATH=/opt/.venv \
5+
POETRY_VIRTUALENVS_CREATE=false \
6+
POETRY_VIRTUALENVS_IN_PROJECT=false \
7+
VIRTUAL_ENV=/opt/.venv \
8+
PATH=/opt/.venv/bin:$PATH \
9+
POETRY_VERSION=2.1.3
610

711
WORKDIR /app
812

@@ -21,7 +25,8 @@ COPY services/rag-backend/pyproject.toml services/rag-backend/poetry.lock ./
2125
RUN mkdir log && chmod 700 log
2226
RUN touch /app/services/rag-backend/log/logfile.log && chmod 600 /app/services/rag-backend/log/logfile.log
2327

24-
RUN poetry config virtualenvs.create false &&\
28+
# Cache bust with dev arg so dev dependencies install when dev=1
29+
RUN echo "CACHE_BUST_DEV=$dev" && poetry config virtualenvs.create false &&\
2530
if [ "$dev" = "1" ]; then \
2631
poetry install --no-interaction --no-ansi --no-root --with dev; \
2732
else \
@@ -33,7 +38,7 @@ ARG dev=0
3338

3439
RUN adduser --disabled-password --gecos "" --uid 10001 nonroot
3540

36-
ENV POETRY_VIRTUALENVS_PATH=/app/services/rag-backend/.venv
41+
ENV POETRY_VIRTUALENVS_PATH=/opt/.venv
3742
COPY --from=build --chown=nonroot:nonroot ${POETRY_VIRTUALENVS_PATH} ${POETRY_VIRTUALENVS_PATH}
3843
COPY --from=build /usr/local/bin/ /usr/local/bin/
3944
COPY --from=build /usr/bin/make /usr/bin/make
@@ -47,6 +52,12 @@ WORKDIR /app/services/rag-backend
4752
COPY --chown=nonroot:nonroot services/rag-backend .
4853

4954

55+
# Ensure poetry reuses existing virtualenv when running as nonroot
56+
ENV POETRY_VIRTUALENVS_CREATE=false \
57+
POETRY_VIRTUALENVS_IN_PROJECT=false \
58+
VIRTUAL_ENV=/opt/.venv \
59+
PATH=/opt/.venv/bin:$PATH
60+
5061
# cleanup
5162
RUN apt-get clean autoclean
5263
RUN apt-get autoremove --yes
@@ -55,13 +66,11 @@ RUN if [ "$dev" = "0" ]; then \
5566
while read -r shell; do rm -f "$shell"; done < /etc/shells; \
5667
rm -rf /var/lib/{apt,dpkg,cache,log}/ \
5768
else \
58-
echo "POETRY_VIRTUALENVS_PATH=/app/services/rag-backend/.venv" >> /etc/environment;\
59-
export POETRY_VIRTUALENVS_PATH=/app/services/rag-backend/.venv;\
69+
echo "POETRY_VIRTUALENVS_PATH=/opt/.venv" >> /etc/environment;\
70+
export POETRY_VIRTUALENVS_PATH=/opt/.venv;\
6071
export PATH="${POETRY_VIRTUALENVS_PATH}/bin:$PATH";\
6172
fi
6273

6374

6475
USER nonroot
6576
COPY --from=build --chown=nonroot:nonroot /app/services/rag-backend/log /app/services/rag-backend/log
66-
67-
ENV PATH="${POETRY_VIRTUALENVS_PATH}/bin:${PATH}"

0 commit comments

Comments
 (0)