less than 1 minute read

Is your docker build step slow? Assuming you can install your code as a package, try installing your dependencies first and your code second.

If you install your code and dependencies together any change in the code will result in an invalidation of the cache. This means that Docker is going to do the dependency steps all over again.

Splitting them up fixes this, hopefully making your docker builds much faster by leveraging the cache.

WORKDIR "/opt"

# Copy *only* package information to prevent code changes from killing the cache
COPY poetry.lock /opt/poetry.lock
COPY pyproject.toml /opt/pyproject.toml

# Install the packages
RUN \
  pip install poetry && \
  poetry install --no-root

# Copy code 
COPY src/ /opt/src/

# Install module
RUN \
  pip install . --no-dependencies && \
  python -c "import ..."

Subscribe

Comments