An Interest In:
Web News this Week
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
April 7, 2022 06:20 am GMT
Original Link: https://dev.to/koladev/optimize-docker-size-image-with-python-environment-df9
Optimize Docker Size Image with Python Environment
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.
ENV PATH="/opt/venv/bin:$PATH"
Make sure you have created the virtual environment tho.
RUN python -m venv /opt/venvENV PATH="/opt/venv/bin:$PATH"
Here's an example with two steps:
# first stageFROM python:3.10-slim as builderWORKDIR /appENV PYTHONDONTWRITEBYTECODE 1ENV PYTHONUNBUFFERED 1RUN pip install virtualenvRUN virtualenv /opt/venvENV PATH="/opt/venv/bin:$PATH"COPY requirements.txt .RUN pip install -r requirements.txt# another stageFROM python:3.10-slimCOPY --from=builder /opt/venv /opt/venvWORKDIR /appENV PATH="/opt/venv/bin:$PATH"
Summary
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.
Original Link: https://dev.to/koladev/optimize-docker-size-image-with-python-environment-df9
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To