Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue while building Docker with ASP.Net - dotnet restore failing #4503

Closed
kevan-mtl opened this issue Mar 19, 2023 · 5 comments
Closed

Issue while building Docker with ASP.Net - dotnet restore failing #4503

kevan-mtl opened this issue Mar 19, 2023 · 5 comments

Comments

@kevan-mtl
Copy link

kevan-mtl commented Mar 19, 2023

hi,
I'm running into issues trying to build a simple Docker container for my ASP.NET app. The build fails at the docker restore step with the following message:

Property reassignment: $(BaseIntermediateOutputPath)="//obj\" (previous value: "obj\") at /usr/share/dotnet/sdk/7.0.202/Sdks/Microsoft.NET.Sdk.Publish/targets/Microsoft.NET.Sdk.Publish.targets (80,5)
executor failed running [/bin/sh -c dotnet restore -v diagnostic --disable-parallel]: exit code: 137

Even though the error code 137 is often associated to running out of memory based on the issues I found only, that hypothesis looks weird since I've bumped my Docker engine RAM and run the build with 8Gb of ram

docker build -t sched-assignation-backend -f Dockerfile . --memory="8g" --no-cache

Somehow, this issue feels related to issues like this one where the BaseIntermediateOutputPath need to be set correctly. Setting its value to "//obj/" looks weird in the error message above, but I'm too new to .Net to say this for sure...

Below is my Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build-env
#WORKDIR ./schedule-assignation-service

# Copy everything
COPY . ./
# Restore as distinct layers
# Restores the dependencies and tools of a project.
# --disable-parallel => restore 1 package at a time
RUN dotnet restore -v diagnostic --disable-parallel
# Build and publish a release
# -c => Configuration
# -o => output directory
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:7.0
WORKDIR /App
COPY --from=build-env /App/out .
ENTRYPOINT ["dotnet", "DotNet.Docker.dll"]

.csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <RootNamespace>schedule_assignation_service</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.3"/>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
    <PackageReference Include="Google.Cloud.Firestore" Version="3.1.0"/>
    <PackageReference Include="FirebaseAdmin" Version="2.3.0"/>
  </ItemGroup>
</Project>

Project file structure:

Screenshot 2023-03-19 at 2 55 17 AM

docker version:

Client:
 Cloud integration: v1.0.29
 Version:           20.10.17
 API version:       1.41
 Go version:        go1.17.11
 Git commit:        100c701
 Built:             Mon Jun  6 23:04:45 2022
 OS/Arch:           darwin/arm64
 Context:           default
 Experimental:      true

Server: Docker Desktop 4.12.0 (85629)
 Engine:
  Version:          20.10.17
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.11
  Git commit:       a89b842
  Built:            Mon Jun  6 23:01:01 2022
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.8
  GitCommit:        9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

docker info:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc., v0.9.1)
  compose: Docker Compose (Docker Inc., v2.10.2)
  extension: Manages Docker extensions (Docker Inc., v0.2.9)
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc., 0.6.0)
  scan: Docker Scan (Docker Inc., v0.19.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 2
 Server Version: 20.10.17
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.10.124-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 4
 Total Memory: 7.667GiB
 Name: docker-desktop
 ID: QWCZ:ZROD:3WQ2:EC7D:AZ2N:ULUJ:23GP:A32F:EV7O:EE6G:ZAJW:XFB4
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5000
  127.0.0.0/8
 Live Restore Enabled: false

Thanks in advance for your help!
Kevan

@richlander
Copy link
Member

richlander commented Mar 19, 2023

I think the problem is that you are copying the source to the root directory. That always works poorly.

Try applying this pattern: https://github.com/dotnet/dotnet-docker/blob/main/samples/aspnetapp/Dockerfile

Also, the COPY . . with the dotnet restore after it isn't going to give you the effect you want. The pattern in the sample is correct. The intent is that you copy just project files first and restore them. They change rarely relative to source files. As a result, you can take advantage of Docker layer caching. The same pattern applies to any platform that has a package file.

@kevan-mtl
Copy link
Author

Hi @richlander ,
This helped me a lot. I was able to create my image. Thanks!

@superdavid0816
Copy link

Hi, I met a problem that it stocked at "dotnet restore" process when I use docker build.
It was fine. I had been build my project all the time but it stocked since 2 days ago.

I use MBP with Net7 in visual studio. Use docker build to build my image.

I tried the dockerfile from @richlander. It still stocked.

it just take half hour to do dotnet restore during docker build process then consume all memory and throw error 137.

Does anyone experience this problem before?

Thanks for your help!

截圖 2023-03-29 下午5 14 25

@richlander
Copy link
Member

Can you share the docker command you are using?

@joeangry
Copy link

joeangry commented Mar 25, 2024

This seems to be a relatively consistent problem for me. I've tried building on my MBP M2 and my Windows 11 PC with no difference. It's always dotnet restore that's stuck forever.

One of my Dockerfiles look like this

FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
USER $APP_UID
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src

# Add GitHub NuGet sources
ARG GITHUB_NUGET_USERNAME
ARG GITHUB_NUGET_PASSWORD

RUN dotnet nuget add source --username ${GITHUB_NUGET_USERNAME} --password ${GITHUB_NUGET_PASSWORD} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/my-company/index.json"

COPY ["MyProject/MyProject.csproj", "MyProject/"]
COPY ["Directory.Build.props", "Directory.Build.props"]
COPY ["Directory.Packages.props", "Directory.Packages.props"]
COPY ["NuGet.Config", "NuGet.Config"]

RUN dotnet restore "MyProject/MyProject.csproj"

COPY . .
WORKDIR "/src/MyProject"
RUN dotnet build "MyProject.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "MyProject.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

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

My build command is

docker compose build --build-arg GITHUB_NUGET_USERNAME=my-username --build-arg GITHUB_NUGET_PASSWORD=my-password

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

4 participants