Repository navigation
Description
I'm going to write down issue, working-but-terrible solution and would like to ask more experienced people around here for better solution.
Issue
Linux binaries shipped with NuGet package are mostly unusable unless user have exactly the same environment as the one package was built in. Even if you install all the dependencies, you are most likely to end up with slightly different versions of your libraries and OpenCVSharpExtern won't work.
So, unless you are using exactly the same Ubuntu with exactly the same versions of packages as on CI server, it won't work out of the box.
Working but problematic solution
Local environment
- Build OpenCV or install version 4.3.0 (if your package repos have newer versions).
- Follow build instructions from
README.mdand buildOpenCvSharpExtern. - Rename it to
libOpenCvSharp(without .so) and put file into/usr/libor make sureldfinds it some other way.
Debug with ldd or just read exception (it will mention what dependency is missing) if you encounter problems.
Docker
Things become even more fun here. If you are hosting ASP .NET Core app inside docker linux container, the only way I found is to build OpenCvSharpExtern in the container similar to production and then extract compilation result along with all dependencies.
- Dockerize your app as usual, use
mcr.microsoft.com/dotnet/core/aspnet:3.1-bionicas runtime image base. - Run your container with something that won't constantly break because of OpenCV exceptions (so container doesn't stop).
docker exec container_name bashto get into the container.- Run all the neccessary
apt-get install ..., build OpenCV 4.3.0, build OpenCvSharp and make sure both are installed into the container system (present somewhere in/usr/local/share/opencvsharp4/or elsewhere). There are CI scripts in this repo that should help withapt-getpart. - Copy all these dependencies into the host system (using mounted volume, for example). I had to copy
/usr/local,/usr/liband/lib/x86_64-linux-gnuwhich turned out to be 1.2GiB. - Remove this experimental container (optional, probably done by
docker-composeautomatically). - Setup your
Dockerfileto copy these files from host onto runtime container after build. - Also something like
ENV LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/share/opencv4/lib/:/usr/local/lib/:/lib/x86_64-linux-gnu/"will probably be required. - Enjoy this process again when upgrade of OpenCV needed (or runtime upgraded, etc).
Questions
- Is it possible to build statically linked
libOpenCvSharpExtern.soto embed all the dependent libs into it? It looks like it was successfully done for Windows, so I wonder if it is possible on Linux. - Is there a better solution for docker problem? Would you just automate
compiling OpenCV in production environmentsteps on your CI server or there is better approach?
I'm fairly noob in either docker and magic of C++ compilation, sorry if I'm missing something obvious.
Hope this helps someone or attracts answers with better solutions :)
Activity
ArXen42 commentedon Jul 15, 2020
A bit better solution
@shimat Thanks for the dockerfile you mentioned in #953. I modified this file to create base image that builds environment for running apps that use
OpenCVSharpwith all necessary dependencies. Modified dockerfile uses Ubuntu 20 dotnet SDK as base image.It can be added to
docker-compose.ymland then used asFROM opencvsharp_basein dockerfiles, or pushed to custom docker registry, etc.Dockerized .NET project does not need any dependency aside from
OpenCVSharp4, native binaries will be built inside docker.However, I'm yet to separate build environment from runtime here. To do so, one will need to copy OpenCV build output and libOpenCvSharp.so (these are currently installed directly into the container system using
make install) and install all the necessary packages (runtime should have same versions of dependencies ad build environment).Guess that will be the next step but for now I don't mind a bit bloated container :)
Dockerfile itself:
Example
docker-compose.yml:Example
Dockerfile:stale commentedon Jan 11, 2021
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
alanee1996 commentedon Jan 25, 2021
I'm using mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic as runtimes image base (docker run -d -t image_id) then i execute docker exec container_name bash. The i follow the sample dockerfile step to install the depedencies and cmake. Unfortunately i face some error when build opencv
alanee1996 commentedon Jan 25, 2021
i also found the opencvsharp repo have dockerfile, i build from the dockerfile then will run into the error like this:
When i build from the opencvsharp repo's dockerfile, i make this "shimat/ubuntu18-dotnetcore3.1-opencv4.5.0:20201030" as runtime image base.
alanee1996 commentedon Jan 25, 2021
now have another error is like this:
stale commentedon Jul 24, 2021
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.