Skip to content

Commit

Permalink
Improve docker build: use apt installed numpy, opencv - it is more ef…
Browse files Browse the repository at this point in the history
…fective & cross platform way (for arm's)
  • Loading branch information
yoori committed Nov 23, 2024
1 parent e3f7290 commit d828e44
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
18 changes: 5 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ ENV CHROME_DISABLE_GPU=${CHROME_DISABLE_GPU}
ENV DEBUG=false
ENV VERBOSE=false
ENV FORKS=
ENV PYTHONPATH=$PYTHONPATH:/usr/lib/python3/dist-packages/
#< trick for use apt installed packages on package installation with using pip.

# Copy dummy packages
COPY --from=builder ${PACKAGES_DIR} ${PACKAGES_DIR}
Expand Down Expand Up @@ -113,27 +115,17 @@ RUN echo '%sudo ALL=(ALL:ALL) NOPASSWD:ALL' >/etc/sudoers.d/nopasswd \
WORKDIR /app

# for armv7l install additional packages for build python modules (no binary distribution for some python packages).
RUN apt-get update && apt install -y --no-install-recommends python3-opencv && ( \
BUILD_ARCH="$(arch)" ; \
if [ "$BUILD_ARCH" = "armv7l" ] ; then \
apt install -y --no-install-recommends cmake build-essential libssl-dev ; \
fi ; \
)
RUN apt-get update && apt install -y --no-install-recommends python3-opencv python3-numpy

RUN echo "Install python package for arch: $(arch)"

COPY . flare_bypasser
RUN pip install --prefer-binary flare_bypasser/
RUN ADDITIONAL_PYTHONPATH="$PYTHONPATH" pip install --prefer-binary flare_bypasser/

COPY rootfs /

# Cleanup environment - decrease image size.
RUN pip cache purge && apt clean && ( \
BUILD_ARCH="$(arch)" ; \
if [ "$BUILD_ARCH" = "armv7l" ] ; then \
apt autoremove -y cmake build-essential libssl-dev ; \
fi ; \
)
RUN pip cache purge && apt clean

USER ${UID}

Expand Down
21 changes: 15 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import sys
import os
import importlib
import distutils.core


# Trick for avoid installation of non pip installed packages (apt), available by ADDITIONAL_PYTHONPATH
def is_installed(pkgname):
try:
import cv2 # noqa
return True
m = importlib.import_module(pkgname)
return m is not None
except Exception:
return False
pass
return False


if "ADDITIONAL_PYTHONPATH" in os.environ:
add_path = os.environ["ADDITIONAL_PYTHONPATH"]
sys.path += add_path.split(':')

install_requires = [
'asyncio',
'uuid',
Expand All @@ -29,8 +38,8 @@ def is_installed(pkgname):
'gunicorn ; platform_system != "Windows"',
]

if not is_installed('cv2'):
# can be installed as opencv-python or opencv-contrib-python
install_requires += ['opencv-python']
for package_import_name, package in [('numpy', 'numpy'), ('cv2', 'opencv-python')]:
if not is_installed(package_import_name):
install_requires += [package]

distutils.core.setup(install_requires=install_requires)

0 comments on commit d828e44

Please sign in to comment.