Skip to content

Instantly share code, notes, and snippets.

@peterroelants
Created February 12, 2021 09:58
Show Gist options
  • Select an option

  • Save peterroelants/23cd89ca4f8585e3a9d1cdf3d477c879 to your computer and use it in GitHub Desktop.

Select an option

Save peterroelants/23cd89ca4f8585e3a9d1cdf3d477c879 to your computer and use it in GitHub Desktop.
Reduce python container size by compiling with pip
# Install python packages with pip using compilation to reduce size
# - https://towardsdatascience.com/how-to-shrink-numpy-scipy-pandas-and-matplotlib-for-your-data-product-4ec8d7e86ee4
# - https://avilpage.com/2020/02/reduce-python-package-footprint.html
# - https://stackoverflow.com/a/22942120
# - https://blog.mapbox.com/aws-lambda-python-magic-e0f6a407ffc6
# `/usr/local/bin/python3-config --cflags` should be applied automatically, the
# following flags extend these.
RUN pip install --upgrade pip \
&& export CFLAGS="${CFLAGS} -g0 -Wl,--strip-all -Os -I/usr/include:/usr/local/include" \
&& export LDFLAGS="${LDFLAGS} -L/usr/lib:/usr/local/lib" \
&& pip install \
--no-cache-dir \
--trusted-host pypi.python.org \
--compile \
--global-option=build_ext \
--global-option="-j 8" \
--requirement /tmp/requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment