MistyK
MistyK

Reputation: 6222

Running .NET Core project in docker targeting x86 throws System.BadImageFormatException

I'm trying to build a simple 32 bit .NET Core Project. I found this: https://github.com/dotnet/core/issues/901 and it actually solves the issue if I build my project locally however it's not gonna work if I build the project using Dockerfile (docker container is linux).

Dockerfile (Default):

FROM microsoft/dotnet:2.2-runtime AS base
WORKDIR /app

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY TestDocker.csproj TestDocker/
RUN dotnet restore TestDocker/TestDocker.csproj
COPY . ./TestDocker
WORKDIR /src/TestDocker
RUN dotnet build TestDocker.csproj -c Release -o /app


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

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

CsProj (only PlatformTarget has been changed)

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

</Project>

Build finishes successfuly and when I try to run it using docker run I get

Unhandled Exception: System.BadImageFormatException: Could not load file or asse
mbly '/app/TestDocker.dll'. An attempt was made to load a program with an incorr
ect format.

If I print dotnet --info inside build then it clearly uses x64 ->

RID: debian.9-x64

I suspect that I need somehow to force x86 dotnet, is there a separate dotnet sdk 32 bit image which I can use?

Upvotes: 1

Views: 2049

Answers (1)

omajid
omajid

Reputation: 15203

The official Microsoft container images are tagged as x86_64. So you are probably out of luck. Worse, .NET Core doesn't even do releases for x86.

One option is to do everything yourself:

  1. Build .NET Core 2.2 for Debian for x86: https://github.com/liserdarts/dotnetcore-build-x86-Linux. You can use the debian 386 container image for this. Then save this somewhere.

  2. Build a debian container image, and install the .NET Core 2.2 SDK that you built in step 1 in it. You can see how Microsoft constructs their container images and adapt that as much as possible.

  3. Use this self-built container image instead of FROM microsoft/dotnet:2.2-sdk.

Upvotes: 1

Related Questions