0

I want my dockerised ASP.NET Core 7 app based on Alpine (not ASP.NET Core Runtime).

MyApp.csproj contains:

<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>linux-musl-x64</RuntimeIdentifier>

Dockerfile contains:

FROM alpine

But when I start a container, I get:

Error relocating /dist/MyApp: __cxa_pure_virtual: symbol not found
Error relocating /dist/MyApp: __cxa_pure_virtual: symbol not found
Error relocating /dist/MyApp: __cxa_pure_virtual: symbol not found
Error relocating /dist/MyApp: _ZTVN10__cxxabiv121__vmi_class_type_infoE: symbol not found
Error relocating /dist/MyApp: _ZTVN10__cxxabiv121__vmi_class_type_infoE: symbol not found
Error relocating /dist/MyApp: _ZTVN10__cxxabiv121__vmi_class_type_infoE: symbol not found

It works with the normal image (i.e. FROM mcr.microsoft.com/dotnet/aspnet).

The docs imply that with the correct RID (linux-musl-x64), this should work. But obviously it's missing dependencies.

How do I fix this?

lonix
  • 14,255
  • 23
  • 85
  • 176
  • Alphine is a minimal image, I doubt it has .NET runtime installed... Probably need to install it. Or just use an official aspnet alpine base image – ventsyv Aug 29 '23 at 14:12
  • @ventsyv Thanks. If I understand the docs correctly, since I'm generating a single-file self-contained binary, that shouldn't be necessary; and targetting the musl RID means it *should* contain the necessary dependencies. If I add those APKs then I think all I'll be doing is upgrading the alpine image to a mcr.microsoft.com/dotnet/aspnet image. Please correct me if I'm wrong? – lonix Aug 29 '23 at 14:15
  • Oh, I see. Have you tried setting IncludeNativeLibrariesForSelfExtract ? The documentation says only managed DLLs are bundled, maybe you are missing a native library? – ventsyv Aug 29 '23 at 14:24
  • Thanks I didn't see that one! I tried it and it doesn't solve the issue though. – lonix Aug 29 '23 at 15:42
  • You need an Alpine image with the .NET runtime dependencies, not just an empty Alpine image. Check the tags in the [dotnet-runtime-deps](https://hub.docker.com/_/microsoft-dotnet-runtime-deps/) image. One of them will be Alpine – Panagiotis Kanavos Aug 29 '23 at 17:46

2 Answers2

1

You can find the list of Alpine packages that .NET requires at https://github.com/dotnet/dotnet-docker/blob/231835f7f8b1effdebbddea9521e34fa305e7459/src/runtime-deps/6.0/alpine3.18/amd64/Dockerfile#L11-L20. For .NET 6/7, those packages are the following:

  • ca-certificates
  • krb5-libs
  • libgcc
  • libintl
  • libssl3
  • libstdc++
  • zlib

Make sure those packages are installed. These packages are necessary even when publishing as a self-contained app as the self-contained app only embeds the .NET runtime, not the native dependencies.

Matt Thalman
  • 3,540
  • 17
  • 26
  • Thanks, straight from the horse's mouth! :-) I used the `runtime-deps` image and it worked. I did not set `IncludeNativeLibrariesForSelfExtract` so I assume it's unnecessary in this case? – lonix Aug 30 '23 at 01:18
1

Try with runtime-deps:7.0-alpine instead, ie:

FROM runtime-deps:7.0-alpine

or

FROM runtime-deps:6.0-alpine

The .NET Runtime Dependencies image

contains the native dependencies needed by .NET. It does not include .NET. It is for self-contained applications.

Alpine is a pretty slim distribution and doesn't contain many of the dependencies found on other distributions, like libgcc.

Publish Directly to a Container

.NET 7 added the ability to publish directly to a container. The linked docs show how this is done:

  1. Add the Microsoft.NET.Build.Containers package to the project
  2. Set the image name with the ContainerImageName property
  3. Publish with dotnet publish --os linux --arch x64 /t:PublishContainer -c Release

For self-published applications, the runtime image is used by default. It's possible to change this using the ContainerBaseImage property in csproj :

<PropertyGroup>
    <ContainerBaseImage>mcr.microsoft.com/dotnet/runtime-deps:7.0-alpine</ContainerBaseImage>
</PropertyGroup>
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Thanks! I wish I could mark both as correct. The section "Publish Directly to a Container" was a revelation - I'm using ci/cd but for local dev builds that is going to be very useful. Thank you. – lonix Aug 30 '23 at 01:19