Docker + Python + Hatch: separate dependencies install

632 views Asked by At

I am building a Dockerfile, using Python, with hatch as a build manager. I want to install dependencies separately from compiling py => pyc files, so that when my code changes I can cache the dependencies (which can take a while to redownload and install).

I'm not sure how I can build the dependencies in hatch as a separate step. (Maybe I can't read the hatch docs, but it doesn't seem to mention it anywhere)

My dockerfile looks something like (simplified)

FROM python
RUN python -m pip install hatch
COPY . /
RUN hatch build

I'd like to change it to

FROM python
RUN python -m pip install hatch
COPY pyproject.toml / 
RUN hatch build-deps    # this step should be cached if a different file is changed
COPY . /
RUN hatch build         # this step shouldn't re-install deps again

Is this possible?

1

There are 1 answers

0
swertz On

You could use hatch to extract the requirements out of the pyproject.toml, install those using pip, and then install your project in a second step:

FROM python
RUN python -m pip install hatch
RUN mkdir /app
WORKDIR /app
COPY pyproject.toml /app

RUN hatch dep show requirements > /app/requirements.txt && \
    python -m pip install -r /app/requirements.txt

COPY . /app
RUN python -m pip install -e .

The disadvantage is that if any change is made to pyproject.toml that is not related to the dependencies, these will still be reinstalled the next time you create the container.