3

The Error

When attempting to publish the app in the microsoft .NET 6.0 sdk container, the build fails with the following error:

System.IO.IOException: The process cannot access the file '/app/My-App/web.config' because it is being used by another process.

The failure occurs right after or towards the end of the React-JS frontend creation.

Here is the error in full:

#25 253.0 /usr/share/dotnet/sdk/6.0.401/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018: The "TransformWebConfig" task failed unexpectedly. [/app/My-App/My-App.csproj]
#25 253.0 /usr/share/dotnet/sdk/6.0.401/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018: System.IO.IOException: The process cannot access the file '/app/My-App/web.config' because it is being used by another process. [/app/My-App/My-App.csproj]
#25 253.0 /usr/share/dotnet/sdk/6.0.401/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at Microsoft.Win32.SafeHandles.SafeFileHandle.Init(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) [/app/My-App/My-App.csproj]
#25 253.0 /usr/share/dotnet/sdk/6.0.401/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) [/app/My-App/My-App.csproj]
#25 253.0 /usr/share/dotnet/sdk/6.0.401/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite) [/app/My-App/My-App.csproj]
#25 253.0 /usr/share/dotnet/sdk/6.0.401/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) [/app/My-App/My-App.csproj]
#25 253.0 /usr/share/dotnet/sdk/6.0.401/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at Microsoft.NET.Sdk.Publish.Tasks.TransformWebConfig.Execute() [/app/My-App/My-App.csproj]
#25 253.0 /usr/share/dotnet/sdk/6.0.401/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [/app/My-App/My-App.csproj]
#25 253.0 /usr/share/dotnet/sdk/6.0.401/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(50,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [/app/My-App/My-App.csproj]
------
executor failed running [/bin/sh -c dotnet publish "My-App/My-App.csproj" -c Release -o /app/My-App]: exit code: 1

Dockerfile

Here is the relevant part of my Dockerfile. Since it is the build container, not much effort is made to keep it small.

Note that the build worked fine in a .NET 2.2 docker build using mcr.microsoft.com/dotnet/core/sdk:2.2. The errors only appeared after the migration to mcr.microsoft.com/dotnet/sdk:6.0

# ========= Build ASP.NET Core =========
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS dotnet_backend_build
RUN apt update && apt upgrade -y
RUN apt install -y curl wget make g++

# ========== Install Node ==========
RUN curl --silent --location https://deb.nodesource.com/setup_lts.x | bash -
RUN apt install -yq --no-install-recommends build-essential nodejs
# Node needs more memory during buildtime than default (=512)
ARG NODE_OPTIONS="--max-old-space-size=4096"
# ========= Installed Node =========

WORKDIR /app
COPY . .
RUN dotnet restore "./My-App/My-App.csproj"
RUN dotnet publish "My-App.csproj" -c Release -o /My-App/My-App
# ========= Built ASP.NET Core =========

What I've tried so far

I would appreciate any further pointers. Thank you in advance.

Energeneer
  • 161
  • 2
  • 7

1 Answers1

0

I struggled with this a bunch yesterday. Our problem was running a build in Github Actions. However, it boiled down to this:

  • In net 6.0, dotnet build is invoking msbuild with the -maxcpucount flag. According to MS docs, doing that means you're allowing msbuild to select a number for how many CPU cores to use to execute. However, the build issue you're having is because there are simultaneous processes trying to access the same file.
  • Consider adding -maxcpucount:1 to the end of your dotnet publish command. This switch will be passed to the underlying msbuild invocation, and should instruct it to be single-threaded, and hence not cause the file-access conflict you're seeing.
theDurbari
  • 29
  • 3