Building Docker image with Python can be well quite heavy.
For a multistage build for example, instead of building wheels at each, you can specify a path for the python environment once it's initialized at the first stage of the build.
Make sure you have created the virtual environment tho.👀
RUN python -m venv /opt/venv
Here's an example with two steps:
# first stage
FROM python:3.10-slim as builder
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install virtualenv
RUN virtualenv /opt/venv
COPY requirements.txt .
RUN pip install -r requirements.txt
# another stage
COPY --from=builder /opt/venv /opt/venv
In conclusion, here are the steps again 🚀:
- Create the virtual environment in the builder image
- Copy the virtual environment to the final image
Article posted using bloggu.io. Try it for free.