runtime: Missing package libldap-2.4.so.2 at runtime:5.0-buster-slim

Steps to reproduce the issue

  1. Create a .NET 5.0 Console app
  2. Run:
dotnet new nugetconfig
dotnet nuget add source -n dotnet5 https://dnceng.pkgs.visualstudio.com/public/_packaging/dotnet5/nuget/v3/index.json
  1. Get night build from System.DirectoryServices.Protocols, version 5.0.0-preview.6.20272.2
  2. Add the following code:
var credential = new NetworkCredential("<username>", "<password>");
var connection = new LdapConnection(new LdapDirectoryIdentifier("<ip(13.0.0.0)>", 389, false, false), credential);
connection.Bind();
  1. Run into container runtime:5.0-buster-slim Reference Dockerfile
FROM mcr.microsoft.com/dotnet/core/runtime:5.0-buster-slim AS base
RUN apt-get update && apt-get install -y libldap-2.4-2
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:5.0-buster AS build
WORKDIR /src
COPY ["TesteLdap/TesteLdap.csproj", "TesteLdap/"]
COPY ["nuget.config", "/src"]
RUN dotnet restore "TesteLdap/TesteLdap.csproj" --configfile "nuget.config"
COPY . .
WORKDIR "/src/TesteLdap"
RUN dotnet build "TesteLdap.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TesteLdap.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TesteLdap.dll"]

Expected behavior

Run without bugs

Actual behavior

We got this error at logs: Unable to load shared library 'libldap-2.4.so.2' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibldap-2.4.so.2: cannot open shared object file: No such file or directory

Additional information (e.g. issue happens only occasionally)

The next version of System.DirectoryServices.Protocols has a dependency of libldap-2.4. related to:

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 41 (17 by maintainers)

Most upvoted comments

@istretyakov - You would need to install the appropriate LDAP library in your Dockerfile. Examples provided below.

Debian/Ubuntu:

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        libldap \
    && rm -rf /var/lib/apt/lists/*

Alpine:

RUN apk add --no-cache \
        libldap

I’m sorry, i didn’t catch what is solution of this problem. For example, i have .NET 5.0 in Docker. And how can i use System.DirectoryServices.Protocols for connecting to Active Directory? What and how need i install? I understood that some libs there aren’t in .NET Docker images, but how to get round the problem?

This issue is still getting significant activity. I propose we update the error message, like the following:

Unable to load shared library 'libldap-2.4.so.2' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibldap-2.4.so.2: cannot open shared object file: No such file or directory

See https://aka.ms/dotnet/ldap-dependency to learn more.

From a container standpoint, I think think we made the best choice, to not install the ldap component. However, we need to do more to make this dependency problem self-diagnosable.

Also, this document doesn’t make any mention of a dangling dependency: https://docs.microsoft.com/dotnet/api/system.directoryservices.protocols.ldapconnection.

Related, this dependency likely isn’t constant across all distros. We should capture that somewhere, possibly at https://github.com/dotnet/core/blob/main/release-notes/6.0/linux-packages.md.

/cc @joperezr

So I finally did get this to work with the following docker file

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

RUN apt-get update \
    && apt-get install -y --no-install-recommends libldap-2.4-2 \
    && apt-get install -y --no-install-recommends libldap-common \
    && rm -rf /var/lib/apt/lists/* 
COPY ["Services/Identity/Identity.API/Certificate/ca_belcorpdc1.crt", "/usr/local/share/ca-certificates/"]
RUN chmod 644 /usr/local/share/ca-certificates/ca_belcorpdc1.crt && update-ca-certificates
COPY ["Services/Identity/Identity.API/Certificate/ldap.conf", "/etc/ldap/"] 

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Services/Identity/Identity.API/Identity.API.csproj", "Services/Identity/Identity.API/"]
RUN dotnet restore "Services/Identity/Identity.API/Identity.API.csproj"
COPY . .
WORKDIR "/src/Services/Identity/Identity.API/"
RUN dotnet build "Identity.API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Identity.API.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Identity.API.dll"]

I was still having issues with my connection not working with the cert so I add the libldap-common package and copied over a ldap.conf file that set the “TLS_REQCERT never”, after that I was able to query the AD and authenticate users.

@adithyasai - Your Dockerfile is installing the package in a Dockerfile stage that’s not being used when running your app. It’s being installed in the build stage which is only used to build your project. That stage is not used when actually running the app.

Here’s the suggested update:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        libldap-2.4-2 \
    && rm -rf /var/lib/apt/lists/*

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Ldaptest1/Ldaptest1.csproj", "Ldaptest1/"]
RUN dotnet restore "Ldaptest1/Ldaptest1.csproj"
COPY . .
WORKDIR "/src/Ldaptest1"
RUN dotnet build "Ldaptest1.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Ldaptest1.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Ldaptest1.dll"]

cc @joperezr - Just an FYI that we’ve decided to not include the libldap package in our Docker images.