skip to Main Content

I am trying to build a dotnet docker image for linux/amd64 architecture on my Apple Silicon. It works for other architectures but it get stuck at "RUN dotnet restore" when building for linux/amd64.

Tried both following commands:

docker build --platform linux/amd64 -t my-hub/solid:auth-api .  
docker buildx build --platform linux/amd64 -t my-hub/solid:auth-api .

My Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /app

COPY *.csproj ./
ENV DOTNET_NUGET_SIGNATURE_VERIFICATION=false
RUN dotnet restore --verbosity detailed

COPY . .
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
COPY --from=build /app/out ./

EXPOSE 5241
ENTRYPOINT ["dotnet", "AuthAPI.dll"]

Console Output:

 => [internal] load build definition from Dockerfile                                                                            0.0s
 => => transferring dockerfile: 396B                                                                                            0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:8.0                                                            0.1s
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:8.0                                                               0.1s
 => [internal] load .dockerignore                                                                                               0.0s
 => => transferring context: 2B                                                                                                 0.0s
 => [build 1/6] FROM mcr.microsoft.com/dotnet/sdk:8.0@sha256:35792ea4ad1db051981f62b313f1be3b46b1f45cadbaa3c288cd0d3056eefb83   0.0s
 => [runtime 1/3] FROM mcr.microsoft.com/dotnet/aspnet:8.0@sha256:6c4df091e4e531bb93bdbfe7e7f0998e7ced344f54426b7e874116a3dc32  0.0s
 => [internal] load build context                                                                                               0.0s
 => => transferring context: 45.74kB                                                                                            0.0s
 => CACHED [runtime 2/3] WORKDIR /app                                                                                           0.0s
 => CACHED [build 2/6] WORKDIR /app                                                                                             0.0s
 => CACHED [build 3/6] COPY *.csproj ./                                                                                         0.0s
 => CANCELED [build 4/6] RUN dotnet restore --verbosity detailed                                                              243.7s # this line takes forever

2

Answers


  1. Chosen as BEST ANSWER

    Matt Thalman's solution worked except I've just added TARGETARCH as an ARG to my Dockerfile. Here is the final Dockerfile:

    FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build
    ARG TARGETARCH
    WORKDIR /app
    
    COPY *.csproj ./
    ENV DOTNET_NUGET_SIGNATURE_VERIFICATION=false
    RUN dotnet restore -a $TARGETARCH
    
    COPY . .
    RUN dotnet publish -c Release -o out -a $TARGETARCH
    
    FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
    WORKDIR /app
    COPY --from=build /app/out ./
    
    EXPOSE 5241
    ENTRYPOINT ["dotnet", "AuthAPI.dll"]
    

    To get a build you can just use the --platform argument in docker build. To build an image for linux/amd64 just use the following build command:

    docker build --platform linux/amd64 .
    

  2. It hangs because .NET doesn’t support running in emulation with QEMU, which is what is used by Docker Desktop in this case since you’re targeting an amd64 architecture which differs from the host machine.

    Try this Dockerfile instead:

    FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build
    WORKDIR /app
    
    COPY *.csproj ./
    ENV DOTNET_NUGET_SIGNATURE_VERIFICATION=false
    RUN dotnet restore --verbosity detailed -a $TARGETARCH
    
    COPY . .
    RUN dotnet publish -c Release -o out -a $TARGETARCH
    
    FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
    WORKDIR /app
    COPY --from=build /app/out ./
    
    EXPOSE 5241
    ENTRYPOINT ["dotnet", "AuthAPI.dll"]
    

    The only changes I made here are the use of --platform=$BUILDPLATFORM in the FROM instruction and -a $TARGETARCH in the restore and publish commands.

    This uses an arm64 version of the sdk image to build the app but then uses the amd64 version of the aspnet image to create your final image. This pattern is explained in detail in https://devblogs.microsoft.com/dotnet/improving-multiplatform-container-support/.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search