title: Including Shared C Library (.SO) in a .NET Core Docker Image
body: <p>I am trying to use a C# library in my .net Core 5.0 project, which is a C# wrapper for a <a href="https://github.com/rpi-ws281x/rpi-ws281x-csharp" rel="nofollow noreferrer">C Assembly for ws281x</a> LEDs. The library requires the that the <a href="https://github.com/rpi-ws281x/rpi-ws281x-csharp#installation" rel="nofollow noreferrer">Shared C library (ws2811.so)</a> be built before running the framework. I have successfully built and installed the C shared library into /usr/lib for my raspberry pi, and have tested it to confirm it works.</p>
  <p>The issue lies in that within my Dockerfile, I don't know how/where to properly install this shared C library. My current dockerfile is as follows:</p>
  <pre><code># https://hub.docker.com/_/microsoft-dotnet
  FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
  WORKDIR /source
  COPY ./ /source
  
  # installs NodeJS and NPM
  RUN apt-get update -yq &amp;&amp; apt-get upgrade -yq &amp;&amp; apt-get install -yq curl git nano
  RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - &amp;&amp; apt-get install -yq nodejs build-essential
  
  RUN npm install -g npm
  RUN npm --version
  
  # installing c wrapper for ws2811 -- Here is where I need HELP
  RUN apt-get update &amp;&amp; apt-get install -y python
  RUN apt-get install -y python-setuptools python-dev build-essential python-pip
  
  RUN pip install scons &amp;&amp; git clone https://github.com/jgarff/rpi_ws281x.git \
           &amp;&amp; cd rpi_ws281x &amp;&amp; scons &amp;&amp; gcc -shared -o ws2811.so *.o
  
  # copy csproj and restore as distinct layers
  COPY *.sln .
  COPY control/ *.csproj ./control/
  RUN dotnet restore -r linux-arm
  
  # copy everything else and build app
  COPY control/. ./control/
  WORKDIR /source/control
  RUN dotnet publish -c release -o /app -r linux-arm --self-contained false --no-restore
  
  # final stage/image
  FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim-arm32v7
  WORKDIR /app
  COPY --from=build /app ./
  ENTRYPOINT [&quot;./control&quot;]
  </code></pre>
  <p>As it is now, the dockerfile builds correctly and the app launches. However, when I try to use a method to invoke the ws2811.so library through P/Invoke, I get the following error:</p>
  <blockquote>
  <p><strong>System.DllNotFoundException:</strong> Unable to load shared library 'ws2811.so' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libws2811.so: cannot open shared object file: No such file or directory</p>
  </blockquote>
  <p>Which is telling me that P/Invoke is unsuccessful in finding the ws2811.so library. Where should I put this ws2811.so library so that my C# code can access it? I've tried placing it with the source code, I've played around with installing it in docker, but am really at a standstill and don't know where to go from here.</p>
  <p>For a bit more information, the C# code to invoke this C-shared library is as follows. It is essentially the same thing as done in the example on the ws281x GitHub (as linked earlier).</p>
  <pre class="lang-cs prettyprint-override"><code>public void setRgb()
          {
              Console.WriteLine(&quot;R: &quot; + r + &quot;\tG: &quot; + g + &quot;\tB: &quot; + b);
  
              var settings = Settings.CreateDefaultSettings(false);
              var controller = settings.AddController(88, Pin.Gpio18, StripType.WS2812_STRIP, 255, false);
  
              Color color = new Color();
              color = Color.FromArgb(r, g, b);
  
              using (var rpi = new WS281x(settings)) // EXCEPTION OCCURS HERE (System.DllNotFound)
              {
                  rpi.SetLedCount(88);
                  rpi.SetAll(color);
              }
          }
  </code></pre>
  <p>Any help, guidance, or resources are extremely appreciated! Thanks so much :)</p>