Alpine容器中指定linux-x64运行.NET测试时加载e_sqlite3失败求助
问题背景
在docker-dotnet-sdk:24.0.7-alpine3.18-net8.0-sdk容器环境下,执行dotnet test -c Release正常,但添加-r linux-x64参数后抛出错误:
System.DllNotFoundException : Unable to load shared library 'e_sqlite3' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable:
进一步报错显示:
/dotnet/Transport/PrijectName.Tests/bin/Release/net8.0/linux-x64/libe_sqlite3.so: fcntl64: symbol not found
已安装包:
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="9.0.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="9.0.4" />
尝试过SQLitePCLRaw.bundle_sqlite3、SQLitePCLRaw.bundle_e_sqlite3、SQLitePCLRaw.bundle_green均无效。
核心原因
Alpine使用musl libc而非glibc,而linux-x64 RID对应的SQLite库是针对glibc编译的,其中fcntl64是glibc特有的符号,musl中不存在,导致库加载失败。
解决方案
方案1:使用Alpine对应的RID(推荐)
将命令中的linux-x64替换为Alpine专属的linux-musl-x64 RID:
dotnet test -c Release -r linux-musl-x64
该RID会自动下载适配musl libc的SQLite库资产,无需额外配置。
方案2:安装musl兼容的SQLite包
在项目中添加针对musl的SQLitePCLRaw包,覆盖默认的glibc版本库:
<PackageReference Include="SQLitePCLRaw.lib.e_sqlite3.linux-musl-x64" Version="2.1.8" />
版本号可根据项目使用的Microsoft.Data.Sqlite版本匹配(建议与依赖的SQLitePCLRaw版本一致)。
方案3:安装系统级SQLite库
在Alpine容器中安装系统提供的musl兼容SQLite库,让程序优先加载系统库:
apk add sqlite-libs
此方法会替换程序包中的glibc版本库,解决符号缺失问题。
内容的提问来源于stack exchange,提问作者Birdalicious

