I have an M1 macbook running Docker Desktop 4.14.1.
I have created a dot net asp.net website project. When I build my image for arm64 everything works fine. When I try to build for amd64 the build gets hung up on the dotnet restore command.
I have tried building using both the normal docker build and also docker buildx build. The buildx build gives other errors that I can’t seem to resolve either, so I decided to post here and focus on the dotnet restore hanging up to see if there is a solution to that without using buildx. If there is not then I will start a new thread to ask about the buildx errors.
Here is my dockerfile:
#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/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["MyApp.Server/MyApp.Server.csproj", "MyApp.Server/"]
COPY ["MyApp/MyApp.csproj", "MyApp/"]
COPY ["MyApp.Api.User/MyApp.Api.User.csproj", "MyApp.Api.User/"]
COPY ["MyApp.Api.Models/MyApp.Api.Models.csproj", "MyApp.Api.Models/"]
COPY ["MyApp.Configuration/MyApp.Configuration.csproj", "MyApp.Configuration/"]
COPY ["MyApp.User.Services/MyApp.User.Services.csproj", "MyApp.User.Services/"]
COPY ["MyApp.Api.Data/MyApp.Api.Data.csproj", "MyApp.Api.Data/"]
COPY ["MyApp.User/MyApp.User.csproj", "MyApp.User/"]
RUN dotnet restore "MyApp.Server/MyApp.Server.csproj"
COPY . .
WORKDIR "/src/MyApp.Server"
RUN dotnet build "MyApp.Server.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApp.Server.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApp.Server.dll"]
Here is my command line I am using to build for amd64:
Since I think it should give you an error if “amd64” was invalid, I guess Docker converts it to “linux/amd64”, but you can still try with the longer platform name.
Although I think it is more likely that the process inside the build container cannot finish. Maybe because it cannot be emulated properly. Emulation is helpful, but not a perfect solution.
@hotshot10101 I’m seeing the same issue locally as well, doesn’t matter if i pass in through the CLI or add it to the FROM in the Dockerfile.
So far I’ve tried this on Docker Desktop version 4.14.1 (91661) and 4.15.0 (93002). Going to try some older versions. Did you manage to resolve it yet?
I have not resolved it. Still hoping someone can fix it or give a good work around.
I did find an alternative method of building, but it is a whole different way. Here is a link to a video on youtube showing the new net7 dotnet docker commands:
I tried above without fixing it, but fixed it by simply adding ‘–platform=$BUILDPLATFORM’ to my FROM statements, like so:
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS restore
ARG BUILD_CONFIGURATION=Debug
WORKDIR /src
COPY ["x.csproj", "x/"]
RUN dotnet restore "./x/./x.csproj" -v:detailed
I got this to work on my M3 mac, building for linux/amd64 using the sample docker file here
# Learn about building .NET container images:
# https://github.com/dotnet/dotnet-docker/blob/main/samples/README.md
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG TARGETARCH
WORKDIR /source
# copy csproj and restore as distinct layers
COPY aspnetapp/*.csproj .
RUN dotnet restore -a $TARGETARCH
# copy and publish app and libraries
COPY aspnetapp/. .
RUN dotnet publish -a $TARGETARCH --no-restore -o /app
# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:8.0
EXPOSE 8080
WORKDIR /app
COPY --from=build /app .
USER $APP_UID
ENTRYPOINT ["./aspnetapp"]
and running docker build -f <dockerfile> --platform linux/amd64 -t <template> . and it worked!! I think the key is the TARGETARCH arg being passed into the dotnet commands.