Docker Build Stuck at dotnet restore for linux/amd64 Platform on macOS: How to Fix?
Asked Answered
S

2

0

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
Stomacher answered 18/7 at 21:42 Comment(0)
A
1

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/.

Aconcagua answered 19/7 at 20:24 Comment(0)
S
0

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 .
Stomacher answered 19/7 at 22:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.