Question

How to include OpenCV when building Flask app from Github using App Platform?

Posted August 26, 2021 158 views
PythonDigitalOcean App PlatformFlask

I have tried to build my Flask app from my Github repository using the App Platform UI.

I have managed to build the app, as indicated by the build logs, but I am unable to deploy the app, and the deployment log kept on throwing this particular error:

  • ImportError: libGL.so.1: cannot open shared object file: No such file or directory

I have tried changing the dependency from opencv-python to opencv-python-headless, but to no avail, and after some simple search online, I found that OpenCV is not supported by heroku-python-buildpack, which is the default buildpack used by App Platform to build the app.

I am wondering what are the steps which I can take, or files which I have to add to my Github repository, in order for App Platform to be able to compile OpenCV for use with my Flask app.

Any form of help will be appreciated.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

👋 @Occutracker

Great question, I think this would be a good candidate for using Dockerfile based builds.

I hacked together a sample flask app that uses opencv. Disclaimer I don’t have any Python/Flask experience so this might not be best practice.

flask-opencv-streaming sample

A Dockerfile based build will automatically be detected if the Dockerfile exists in the root of the project. My dockerfile looks like:

FROM ubuntu
RUN apt-get update -y --fix-missing --allow-releaseinfo-change
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y python3.6 python3-pip python3-pip python-dev build-essential libgl1-mesa-glx libsm6 libxext6 libglib2.0-0
RUN mkdir /app
WORKDIR /app
COPY . /app/

RUN pip3 install --upgrade pip
RUN pip3 install flask
RUN pip3 install -r requirements.txt
CMD ["python3", "app.py"]

and my DO app spec looks like:

name: flask-opencv-streaming
region: nyc
services:
- dockerfile_path: Dockerfile
  github:
    branch: master
    deploy_on_push: true
    repo: jonfriesen/flask-opencv-streaming
  http_port: 8080
  instance_count: 1
  instance_size_slug: basic-xs
  name: flask-opencv-streaming
  routes:
  - path: /
  source_dir: /
  • Hi @crashoverride,

    Thank you for the advice.

    I have tried running the app with the Dockerfile which you have used above, and even though Python 3.6 is indicated in the Dockerfile, the DigitalOcean App platform keeps on defaulting to Python 3.8, which is incompatible with my python packages.

    Is there anyway that I can force Docker to use only Python 3.6 (or any specific versions of Python 3.6.x for that matter)?

    Would appreciate any form of advice for this issue.

    • Hi @Occutracker

      Changing the base image (at the top of the Dockerfile) from From ubuntu to something like: FROM python:3.6-bullseye should pin you at v3.6 without having to do anything fancy.

      You may also be able to cut down the amount of apt-get installs that happen too!

      Hope this helps :)

      • Hi @crashoverride,

        Thank you for your advice once again.

        I have another question for you. I would like to install the python package “pythonnet”. However, I found that it is not possible to just install the package directly using pip install for linux-based systems.

        What command should I issue within the Dockerfile in order for the pythonnet package to be installed properly?

        Would appreciate your advice for this issue.

        • 👋 @Occutracker

          It seems that pythonnet needs access to .net, and in many cases mono will suffice. Bundling this into your dockerfile.

          This repository jonemo/pythonnet-docker looks like a good resource on getting Pythonnet running in Docker.

          Hope this helps!

          • Hi @crashoverride,

            Thank you for the great advice.

            I have created the following Dockerfile for my Flask app:

            FROM python:3.6.10-stretch
            RUN apt-get update -y
            RUN DEBIAN_FRONTEND=noninteractive apt-get install -y python-dev build-essential libgl1-mesa-glx libsm6 libxext6 libglib2.0-0 libglib2.0-dev
            
            RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF \
              && echo "deb http://download.mono-project.com/repo/debian stretch/snapshots/5.20 main" > /etc/apt/sources.list.d/mono-official.list \
              && apt-get update \
              && apt-get install -y clang \
              && apt-get install -y mono-devel=5.20\* \
              && rm -rf /var/lib/apt/lists/* /tmp/*
            
            RUN mkdir /app
            WORKDIR /app
            COPY . /app/
            
            RUN pip3 install --upgrade pip
            RUN pip3 install -U setuptools
            RUN pip3 install -U wheel
            RUN pip3 install -U pycparser
            RUN pip3 install pythonnet==2.4.0
            RUN pip3 install -r requirements.txt
            CMD ["python3", "app.py"]
            

            However, I keep on getting the following error:
            “gpg: keyserver receive failed: Address already in use”.

            I have tried looking for a solution, but nobody seems to have encountered this problem previously.

            Would appreciate it if you could provide some advice on this.