Skip to content

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

Closed
@litetex

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

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