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=”/…


This content originally appeared on DEV Community and was authored by Mangabo Kolawole

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/venv
ENV PATH="/opt/venv/bin:$PATH"

Here's an example with two steps:

# first stage
FROM python:3.10-slim as builder

WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN pip install virtualenv

RUN virtualenv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

COPY requirements.txt .
RUN pip install -r requirements.txt

# another stage
FROM python:3.10-slim

COPY --from=builder /opt/venv /opt/venv

WORKDIR /app

ENV 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.


This content originally appeared on DEV Community and was authored by Mangabo Kolawole


Print Share Comment Cite Upload Translate Updates
APA

Mangabo Kolawole | Sciencx (2022-04-07T06:20:01+00:00) Optimize Docker Size Image with Python Environment. Retrieved from https://www.scien.cx/2022/04/07/optimize-docker-size-image-with-python-environment/

MLA
" » Optimize Docker Size Image with Python Environment." Mangabo Kolawole | Sciencx - Thursday April 7, 2022, https://www.scien.cx/2022/04/07/optimize-docker-size-image-with-python-environment/
HARVARD
Mangabo Kolawole | Sciencx Thursday April 7, 2022 » Optimize Docker Size Image with Python Environment., viewed ,<https://www.scien.cx/2022/04/07/optimize-docker-size-image-with-python-environment/>
VANCOUVER
Mangabo Kolawole | Sciencx - » Optimize Docker Size Image with Python Environment. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/07/optimize-docker-size-image-with-python-environment/
CHICAGO
" » Optimize Docker Size Image with Python Environment." Mangabo Kolawole | Sciencx - Accessed . https://www.scien.cx/2022/04/07/optimize-docker-size-image-with-python-environment/
IEEE
" » Optimize Docker Size Image with Python Environment." Mangabo Kolawole | Sciencx [Online]. Available: https://www.scien.cx/2022/04/07/optimize-docker-size-image-with-python-environment/. [Accessed: ]
rf:citation
» Optimize Docker Size Image with Python Environment | Mangabo Kolawole | Sciencx | https://www.scien.cx/2022/04/07/optimize-docker-size-image-with-python-environment/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.