Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 7, 2022 06:20 am GMT

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To