Built Objects from Docker Build Don't Carry Over #437
Replies: 1 comment 1 reply
-
hi! /ᐠ. ̫ .ᐟ\ฅ I've found similar problems when trying to integrate pytest-celery with uv. After too much time trying random stuff I've come to the conclusion that pytest-celery doesn't like virtual enviroments. Even if the image spins containers correctly in the cli, when trying with pytest it isn't able to find the venv. Maybe because the docker-py library uses the old legacy builder, I found problems with Buildkit when I tried using the docker files in this repo, but I don't know if that's the culprit. I tried making the django example work with venvs by simply creating one and installing everything there but it didn't work. My solution boiled down to exporting all the dependencies into a requirements.txt file and then installing that globally with pip. Here is the docker image I'm using for reference: FROM python:3.12-bookworm
# Set up uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
# Create a user to run the worker
RUN adduser --disabled-password --gecos "" test_user
# Install system dependencies
RUN apt-get update && apt-get install -y build-essential git
# Set arguments
ARG CELERY_LOG_LEVEL=INFO
ARG CELERY_WORKER_NAME=celery_dev_worker
ARG CELERY_WORKER_QUEUE=celery
ENV LOG_LEVEL=$CELERY_LOG_LEVEL
ENV WORKER_NAME=$CELERY_WORKER_NAME
ENV WORKER_QUEUE=$CELERY_WORKER_QUEUE
EXPOSE 5678
# Install packages
WORKDIR /src
# Copy dependency files before installing dependencies
COPY uv.lock pyproject.toml /src/
# Install dependencies
RUN uv export --no-hashes --output-file requirements.txt
RUN pip install --no-cache-dir --upgrade pip
RUN pip install -r ./requirements.txt
# Switch to the test_user
USER test_user
# Start the celery worker
CMD celery -A proj worker --loglevel=$LOG_LEVEL -n $WORKER_NAME@%h -Q $WORKER_QUEUE I used this with the django example and all tests passed. I only added the I don't know where the problem is, but this works ¯_(ツ)_/¯ It'd be great if an example with venvs was added to the project. For reference this is the dockerfile that I tried to use for venvs with the django example: FROM python:3.11-bookworm
# Create a user to run the worker
RUN adduser --disabled-password --gecos "" test_user
# Install system dependencies
RUN apt-get update && apt-get install -y build-essential git
# Set arguments
ARG CELERY_LOG_LEVEL=INFO
ARG CELERY_WORKER_NAME=celery_dev_worker
ARG CELERY_WORKER_QUEUE=celery
ENV LOG_LEVEL=$CELERY_LOG_LEVEL
ENV WORKER_NAME=$CELERY_WORKER_NAME
ENV WORKER_QUEUE=$CELERY_WORKER_QUEUE
EXPOSE 5678
# Install packages
WORKDIR /src
RUN python -m venv venv
ENV PATH="/src/venv/bin:$PATH"
COPY --chown=test_user:test_user requirements.txt .
RUN pip install --no-cache-dir --upgrade pip
RUN pip install -r ./requirements.txt
# Switch to the test_user
USER test_user
# Start the celery worker
CMD celery -A proj worker --loglevel=$LOG_LEVEL -n $WORKER_NAME@%h -Q $WORKER_QUEUE Which raises: |
Beta Was this translation helpful? Give feedback.
-
My aim is to use
pytest-celery
for my tests for a Django project. The issue that I am facing is that a folder I create in my worker's Dockerfile is no longer present when the worker runs.In my Dockerfile I use Poetry to install my dependencies to
/build
, away from/app
and/src
as those are overwritten later when the container is created.My goal is to then use this
/build
folder as a virtual environment for Celery to use. However, when I runpytest
it crashes, stating that Django is not installed. To try and solve this I explicitly stated that the/build/.venv/bin/celery
binary should be used, but this reveals that this path is completely absent from the image.Is there something I'm missing about the way
pytest-celery
creates images? If I build the worker image myself and runbash
on it the directory exists there.My worker creation script is also shown below.
...and the error I'm facing when running
pytest
.I also see here that it's using
python3.10
instead ofpython3.12
which is built into the Dockerimage?Any help is appreciated, thanks.
Beta Was this translation helpful? Give feedback.
All reactions