Skip to content

Not working in Docker: Unable to load shared library 'git2-106a5f2' or one of its dependencies #1798

Description

@litetex

Overview - The workflow

For details see below.

  • NET Core 3.1 project that uses LibGit2Sharp
  • It is build using dotnet publish -c Release -r linux-x64 /p:PublishSingleFile=true, which creates a standalone file and requires no dotnet locally installed
  • Then a Docker-Image (FROM mcr.microsoft.com/dotnet/core/runtime-deps:3.1-focal) is built with this file
  • When running the built image it crashes

However locally (on windows) it does not...

I also already checked out other issues, but found no workaround for my case:

Also tried to run / build the project not as self-contained file (without /p:PublishSingleFile=true and other modifications like FROM mcr.microsoft.com/dotnet/core/runtime:3.1-focal) but this also didn't work.

Reproduction steps

I created a PoC repo here: https://github.com/litetex/LibGit2SharpPoc

Dockerfile

https://github.com/litetex/LibGit2SharpPoc/blob/develop/build/Dockerfile

FROM mcr.microsoft.com/dotnet/core/runtime-deps:3.1-focal

COPY LibGit2SharpPoc LibGit2SharpPoc

RUN chmod +x LibGit2SharpPoc

ENTRYPOINT ["./LibGit2SharpPoc"]

Github workflow

https://github.com/litetex/LibGit2SharpPoc/blob/develop/.github/workflows/develop.yml

name: Develop CI

on:
  push:
    branches: develop

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
      
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.201
      
    - name: Install dependencies
      run: dotnet restore
      
    - name: Build
      run: dotnet build --configuration Release --no-restore
      
    - name: Test
      run: dotnet test --no-restore --verbosity normal
     
    - name: Publish
      working-directory: LibGit2SharpPoc
      run: dotnet publish -c Release -r linux-x64 /p:PublishSingleFile=true
        
    - name: Copy Dockerfile into build output
      run: cp build/Dockerfile LibGit2SharpPoc/bin/Release/netcoreapp3.1/linux-x64/publish/
          
    - name: Builder Dockerimage and publish to Registry
      # Let's call this Publish-... when it actually builds and publishes... 
      uses: elgohr/Publish-Docker-Github-Action@master
      with:
        name: litetex/libgit2sharppoc
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
        tags: "develop,develop-${{ github.sha }}"
        workdir: LibGit2SharpPoc/bin/Release/netcoreapp3.1/linux-x64/publish/

Project file

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>

    <RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>

    <!-- https://github.com/libgit2/libgit2sharp/issues/1754 -->
    <IncludeSymbolsInSingleFile>true</IncludeSymbolsInSingleFile>

    <!-- Emulate .NET Frameworks versioning behavior 'GenerateVersionFromPatternAndCurrentTime' (UTC based) -->
    <!-- https://github.com/dotnet/sdk/issues/8416#issuecomment-354095128 -->
    <Build>$([System.DateTime]::op_Subtraction($([System.DateTime]::get_UtcNow().get_Date()),$([System.DateTime]::new(2000,1,1))).get_TotalDays())</Build>
    <Revision>$([MSBuild]::Divide($([System.DateTime]::get_UtcNow().get_TimeOfDay().get_TotalSeconds()), 2).ToString('F0'))</Revision>
    <VersionPrefix>1.0.$(Build).$(Revision)</VersionPrefix>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="LibGit2Sharp" Version="0.26.2" />
  </ItemGroup>

</Project>

Code

using LibGit2Sharp;
using System;

namespace LibGit2SharpPoc
{
   static class Program
   {
      static void Main(string[] args)
      {
         Repository.Discover("");
      }
   }
}

Expected behavior

I expect the same behavior, as if I build it locally (dotnet publish -c Release -r win-x64 /p:PublishSingleFile=true) and execute the file:
No crash, exit successfully :)

Actual behavior

When I start the container with:

docker run litetex/libgit2sharppoc:develop

It crashes with:

Unhandled exception. System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'git2-106a5f2' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libgit2-106a5f2: cannot open shared object file: No such file or directory
   at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
   at LibGit2Sharp.Core.NativeMethods.InitializeNativeLibrary()
   at LibGit2Sharp.Core.NativeMethods..cctor()
   --- End of inner exception stack trace ---
   at LibGit2Sharp.Core.NativeMethods.git_repository_discover(GitBuf buf, FilePath start_path, Boolean across_fs, FilePath ceiling_dirs)
   at LibGit2Sharp.Core.Proxy.<>c__DisplayClass219_0.<git_repository_discover>b__0(GitBuf buf)
   at LibGit2Sharp.Core.Proxy.ConvertPath(Func`2 pathRetriever)
   at LibGit2Sharp.Core.Proxy.git_repository_discover(FilePath start_path)
   at LibGit2Sharp.Repository.Discover(String startingPath)
   at LibGit2SharpPoc.Program.Main(String[] args) in /home/runner/work/LibGit2SharpPoc/LibGit2SharpPoc/LibGit2SharpPoc/Program.cs:line 11

Version of LibGit2Sharp (release number or SHA1)

0.26.2 (latest at the moment)

Operating system(s) tested; .NET runtime tested

  • OS: Docker with mcr.microsoft.com/dotnet/core/runtime-deps:3.1-focal (ubuntu-20.04)
    • Checked alpine, but doesn't work either
  • Runtime: Self contained Net Core 3.1.201 app

Activity

  1. changed the title [-]Not working in Docker as self-contained file: Unable to load shared library 'git2-106a5f2' or one of its dependencies[/-] [+]Not working in Docker: Unable to load shared library 'git2-106a5f2' or one of its dependencies[/+] on May 28, 2020
  2. bording commented on May 28, 2020

    @bording
    Member

    This is a known issue. Ubuntu 20.04 isn't going to work right now.

    You'll need to an older version of Ubuntu, and when you publish, you likely need to use a more specific RID to ensure you're getting the correct native binary from the package.

  3. litetex commented on May 29, 2020

    @litetex
    Author

    This is a known issue. Ubuntu 20.04 isn't going to work right now.

    Can you link the issue please :)

    Using RID (runtime identifier) ubuntu.18.04-x64 and FROM mcr.microsoft.com/dotnet/core/runtime-deps:3.1-bionic works 👍
    Poc-Repo available here, Image available here

    However alpine with RID alpine-x64 and FROM mcr.microsoft.com/dotnet/core/runtime-deps:3.1-alpine fails:

    Unhandled exception. System.TypeInitializationException: The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an exception.
     ---> System.DllNotFoundException: Unable to load shared library 'git2-106a5f2' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: Error loading shared library libgit2-106a5f2: No such file or directory
       at LibGit2Sharp.Core.NativeMethods.git_libgit2_init()
       at LibGit2Sharp.Core.NativeMethods.InitializeNativeLibrary()
       at LibGit2Sharp.Core.NativeMethods..cctor()
       --- End of inner exception stack trace ---
       at LibGit2Sharp.Core.NativeMethods.git_repository_discover(GitBuf buf, FilePath start_path, Boolean across_fs, FilePath ceiling_dirs)
       at LibGit2Sharp.Core.Proxy.<>c__DisplayClass219_0.<git_repository_discover>b__0(GitBuf buf)
       at LibGit2Sharp.Core.Proxy.ConvertPath(Func`2 pathRetriever)
       at LibGit2Sharp.Core.Proxy.git_repository_discover(FilePath start_path)
       at LibGit2Sharp.Repository.Discover(String startingPath)
       at LibGit2SharpPoc.Program.Main(String[] args) in /home/runner/work/LibGit2SharpPoc/LibGit2SharpPoc/LibGit2SharpPoc/Program.cs:line 11
    

    Poc-Repo available here, Image available here

    For now I can work with ubuntu-18.04, but it would be very nice if this would work with alpine, because it's very small (which makes it really good for micro-services 😉):
    mcr.microsoft.com/dotnet/core/runtime-deps:3.1-alpine has only a size of <10MB, while
    mcr.microsoft.com/dotnet/core/runtime-deps:3.1-bionic (ubuntu-18.04) and mcr.microsoft.com/dotnet/core/runtime-deps:3.1-buster-slim (debian) have a size of >100MB

  4. bording commented on May 29, 2020

    @bording
    Member

    It's talked about in several issues on the repo.

    For the alpine image you're trying, you probably need to use the alpine.3.9-x64 RID. You've got to use the RID that will get you the "correct" native library from the LibGit2Sharp.NativeBinaries runtimes folder.

    Yes, this is all more complicated than I'd like for it to be. Ultimately, the fix is going to be getting #1618 finished, which is slowly happening.

  5. litetex commented on May 29, 2020

    @litetex
    Author

    Okay alpine.3.9-x64 works 👍

    Very nice. Thank you a lot for the help :)

    It would be nice if this would be documented somewhere, for now I used the bin\Debug\netcoreapp3.1\runtimes-folder
    grafik

    For now I'm closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions