Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
litetex opened this issue May 28, 2020 · 4 comments

Comments

@litetex
Copy link

litetex commented May 28, 2020

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
@litetex litetex 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 May 28, 2020
@bording
Copy link
Member

bording commented May 28, 2020

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.

@litetex
Copy link
Author

litetex commented May 29, 2020

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

@bording
Copy link
Member

bording commented May 29, 2020

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.

@litetex
Copy link
Author

litetex commented May 29, 2020

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.

@litetex litetex closed this as completed May 29, 2020
Viir added a commit to pine-vm/pine that referenced this issue Jan 1, 2021
Integrate new implementations from libgit2/libgit2sharp#1618

With the previous version, in deployments on Docker found the 'load from git' function in the Elm Editor did not work. The backend returned this error with the HTTP response:
```
Exception in volatile host:
System.AggregateException: One or more errors occurred. (The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an 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_clone(git_repository*& repo, String origin_url, FilePath workdir_path, GitCloneOptions& opts)
   at LibGit2Sharp.Core.Proxy.git_clone(String url, String workdir, GitCloneOptions& opts)
   at LibGit2Sharp.Repository.Clone(String sourceUrl, String workdirPath, CloneOptions options)
   at Kalmit.LoadFromGithub.LoadFromUrl(String sourceUrl) in /app/PersistentProcess/PersistentProcess.Common/LoadFromGithub.cs:line 109
   at Kalmit.LoadFromPath.LoadTreeFromPath(String path) in /app/PersistentProcess/PersistentProcess.Common/LoadFromPath.cs:line 11
[...]
```

See the discussion of the problem at libgit2/libgit2sharp#1798

Another workaround found in the discussion was switching to the bionic flavor of the .net docker image.
Viir added a commit to pine-vm/pine that referenced this issue Jan 1, 2021
Integrate new implementations from libgit2/libgit2sharp#1618

With the previous version, in deployments on Docker found the 'load from git' function in the Elm Editor did not work. The backend returned this error with the HTTP response:
```
Exception in volatile host:
System.AggregateException: One or more errors occurred. (The type initializer for 'LibGit2Sharp.Core.NativeMethods' threw an 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_clone(git_repository*& repo, String origin_url, FilePath workdir_path, GitCloneOptions& opts)
   at LibGit2Sharp.Core.Proxy.git_clone(String url, String workdir, GitCloneOptions& opts)
   at LibGit2Sharp.Repository.Clone(String sourceUrl, String workdirPath, CloneOptions options)
   at Kalmit.LoadFromGithub.LoadFromUrl(String sourceUrl) in /app/PersistentProcess/PersistentProcess.Common/LoadFromGithub.cs:line 109
   at Kalmit.LoadFromPath.LoadTreeFromPath(String path) in /app/PersistentProcess/PersistentProcess.Common/LoadFromPath.cs:line 11
[...]
```

See the discussion of the problem at libgit2/libgit2sharp#1798 and libgit2/libgit2sharp#1747

Another workaround found in the discussion was switching to the bionic flavor of the .net docker image.
ricardoboss added a commit to trenz-gmbh/trenz-docs-api that referenced this issue Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants