Dockerise ASP.NET Core app with Alpine image

1.2k Views Asked by At

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?

2

There are 2 best solutions below

1
Matt Thalman On BEST ANSWER

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.

1
Panagiotis Kanavos On

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>