Search code examples
c#linuxdocker.net-core.net-standard

Run .Net Core application with .Net Standard library in Linux Docker


I have a .Net Core 3.1.200 that reference another code that is written in .Net Standard 1.3 (shared with .Net Framework apps).

When I try to push image to a docker it fails on all sorts of memory faults. Image was running perfectly till the reference was added. Here is my docker file:

FROM mcr.microsoft.com/dotnet/core/runtime:3.1

COPY bin/publish/ /app-docker-image

WORKDIR /app-docker-image

ENTRYPOINT ["dotnet", "app.dll"]

Is there a way to make it work on a Linux docker?

Thanks!


Solution

  • Quick Answer:

    Reference the project in your custom .net app. Transfer the app code (along with .NET standard lib dependency inside docker) and build the new image

    Explanation:

    Your current approach isn't the recommended way of creating container images. You don't copy a pre-built code to the runtime image.

    Rather than copying publish folder from host to container, you should transfer the code to the SDK container image.

    With the code inside docker run dotnet CLI commands to build/publish the code.

    The transfer the published folder from SDK container to runtime container. Your docker file should look like below:

    #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/core/runtime:3.1-buster-slim AS base WORKDIR /app

    FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
    WORKDIR /src
    COPY ["ConsoleApp1/ConsoleApp1.csproj", "ConsoleApp1/"]
    RUN dotnet restore "ConsoleApp1/ConsoleApp1.csproj"
    COPY . .
    WORKDIR "/src/ConsoleApp1"
    RUN dotnet build "ConsoleApp1.csproj" -c Release -o /app/build
    
    FROM build AS publish
    RUN dotnet publish "ConsoleApp1.csproj" -c Release -o /app/publish
    
    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "ConsoleApp1.dll"]