dotnet publish with new implicit dockerfile feature using NuGet Microsoft.NET.Build.Containers fails in GitLab cicd - c#

I am trying out the new NuGet package Microsoft.NET.Build.Containers which allows building docker image with dotnet publish.
Microsoft Docs: https://learn.microsoft.com/en-us/dotnet/core/docker/publish-as-container?view=vs-2022
It works fine on local machine pushing out an image locally as it should but fails in GitLab cicd.
Project (added following to a sln file):
# create a new project and move to its directory
dotnet new mvc -n my-awesome-container-app
cd my-awesome-container-app
# add a reference to a (temporary) package that creates the container
dotnet add package Microsoft.NET.Build.Containers
# publish your project for linux-x64
dotnet publish --os linux --arch x64 -c Release -p:PublishProfile=DefaultContainer
# run your app using the new container
docker run -it --rm -p 5010:80 my-awesome-container-app:1.0.0
Cicd pipeline:
image: mcr.microsoft.com/dotnet/sdk:7.0
stages: # List of stages for jobs, and their order of execution
- publish
build-job: # This job runs in the build stage, which runs first.
stage: publish
script:
- dotnet publish --os linux --arch x64 --configuration Release -p:PublishProfile=DefaultContainer
The error produced (omitted repo):
MSBuild version 17.4.1+9a89d02ff for .NET
Determining projects to restore...
Restored /builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj (in 827 ms).
my-awesome-container-app -> /builds/.../dotnetpublishdockerimage/my-awesome-container-app/bin/Release/net7.0/linux-x64/my-awesome-container-app.dll
my-awesome-container-app -> /builds/.../dotnetpublishdockerimage/my-awesome-container-app/bin/Release/net7.0/linux-x64/publish/
Building image 'my-awesome-container-app' with tags 1.0.0 on top of base image mcr.microsoft.com/dotnet/aspnet:7.0
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: The "CreateNewImage" task failed unexpectedly. [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: System.AggregateException: One or more errors occurred. (An error occurred trying to start process 'docker' with working directory '/builds/.../dotnetpublishdockerimage/my-awesome-container-app'. No such file or directory) [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: ---> System.ComponentModel.Win32Exception (2): An error occurred trying to start process 'docker' with working directory '/builds/.../dotnetpublishdockerimage/my-awesome-container-app'. No such file or directory [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: at System.Diagnostics.Process.ForkAndExecProcess(ProcessStartInfo startInfo, String resolvedFilename, String[] argv, String[] envp, String cwd, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec) [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: at System.Diagnostics.Process.Start(ProcessStartInfo startInfo) [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: at Microsoft.NET.Build.Containers.LocalDocker.Load(Image x, String name, String tag, String baseName) in D:\a\_work\1\s\Microsoft.NET.Build.Containers\LocalDocker.cs:line 19 [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: --- End of inner exception stack trace --- [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: at System.Threading.Tasks.Task.Wait() [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: at Microsoft.NET.Build.Containers.Tasks.CreateNewImage.Execute() in D:\a\_work\1\s\Microsoft.NET.Build.Containers\CreateNewImage.cs:line 243 [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
/root/.nuget/packages/microsoft.net.build.containers/0.2.7/build/Microsoft.NET.Build.Containers.targets(124,9): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [/builds/.../dotnetpublishdockerimage/my-awesome-container-app/my-awesome-container-app.csproj]
Searching for a possible solution I came across and article for it for GitHub actions and it also works there. Also the example in there I have used for this questions project.
Article: https://devblogs.microsoft.com/dotnet/announcing-builtin-container-support-for-the-dotnet-sdk/
Also the GitLab runner is using Docker executor. Perhaps it doesn't work inside a container. The article for GitHub shows ubuntu-latest but I do not know if it is running in a container underneath or not.
Anyone know what is going on and have a solution?

Looking further into it I found the issue and a solution as well.
There are 2 problems here.
In order to build images (part of dotnet publish) docker-in-docker (dind) is required.
The .net sdk image does not provide dind support
To deal with the first problem there are a couple of changes needed.
First add dnid as a service in cicd:
services:
- docker:dind
Nex assign the docker host variable:
variables:
DOCKER_HOST: tcp://docker:2375/
And finally to enable privileged mode on docker runner (config.toml):
[[runners]]
...
[runners.docker]
...
privileged = true
...
Restart of docker runner may be required.
There can also be an issue with tls. It can be ignored (although not advised for privileged mode) by adding empty certs dir to cicd:
variables:
DOCKER_TLS_CERTDIR: ""
The deal with the second problem an alternative is to use the image 'docker' which provides dind support. From this there are 2 options:
Install the .net sdk inside:
before_script:
- apk add dotnet7-sdk
Official docs: https://learn.microsoft.com/en-us/dotnet/core/install/linux-alpine
Create a new docker image based off of docker with sdk installation added and then use this image in pipeline.
There may be other more elegant ways to solve this but it will do as a workaround.
A sample cicd pipeline including publishing to registry:
stages:
- publish
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_TLS_CERTDIR: ""
services:
- docker:dind
docker_test:
stage: publish
image: docker
before_script:
- apk add dotnet7-sdk
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- dotnet publish --os linux --arch x64 --configuration Release -p:PublishProfile=DefaultContainer
- IMAGE_ID=$(docker images --format='{{.ID}}' | head -1)
- docker tag $IMAGE_ID $CI_REGISTRY_IMAGE/my-image:1.0.0
- docker push $CI_REGISTRY_IMAGE/my-image:1.0.0

Related

Single file application .NET Core 6.0 fails to generate bundles, if the file is too large

The idea is to have an application with a single file executable, despite the size, can be a very big file.
I'm using the "Single file deployment and executable" of .NET 6.0 with the following command:
publish {csproj.FullName} -c Release -r win-x64 -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true --self-contained true -p:IncludeAllContentForSelfExtract=true -p:EnableCompressionInSingleFile=true
But when files are too big like greater then 4GB, I am not able to publish because of the following error:
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(898,5): error MSB4018: System.ArgumentException: Stream length minus starting position is too large to hold a PEImage. (Parameter 'peStream')
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(898,5): error MSB4018: at System.Reflection.Internal.StreamExtensions.GetAndValidateSize(Stream stream, Int32 size, String streamParameterName)
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(898,5): error MSB4018: at System.Reflection.PortableExecutable.PEReader..ctor(Stream peStream, PEStreamOptions options, Int32 size)
Any Idea how to fix it? Or another alternative to apply this idea.
After some research there is a limitation on C# that doesn't allow for a single file to be more than 4gb.
So the alternative that I've chosen is to use self-extracting zips.
The one that I've chosen is 7Zip. Using their SDK I am able to zip my files into a single file, and then create a self-extracting zip that has the .exe for the file execution and it works as expected!

TypeInitializationException / SSL library issue with dotnet core and Ubuntu

I have a dotnet core app (netcoreapp3.1) that crashes with the following error on launch:
$ dotnet run
Unhandled exception. System.TypeInitializationException: The type initializer for 'Crypto' threw an exception.
---> System.TypeInitializationException: The type initializer for 'CryptoInitializer' threw an exception.
---> System.DllNotFoundException: Unable to load shared library 'libSystem.Security.Cryptography.Native.OpenSsl' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibSystem.Security.Cryptography.Native.OpenSsl: cannot open shared object file: No such file or directory
at Interop.CryptoInitializer.EnsureOpenSslInitialized()
at Interop.CryptoInitializer..cctor()
--- End of inner exception stack trace ---
at Interop.CryptoInitializer.Initialize()
at Interop.Crypto..cctor()
--- End of inner exception stack trace ---
at Interop.Crypto.EvpPkeyDestroy(IntPtr pkey)
at System.Security.Cryptography.SafeEvpPKeyHandle.ReleaseHandle()
at System.Runtime.InteropServices.SafeHandle.InternalRelease(Boolean disposeOrFinalizeOperation)
at System.Runtime.InteropServices.SafeHandle.Dispose(Boolean disposing)
at System.Runtime.InteropServices.SafeHandle.Finalize()
I have searched for this error, but most of the discussion is from 2016.
I am using Ubuntu 20.04.
System info:
$ openssl version
OpenSSL 1.0.2n 7 Dec 2017
$ dotnet --version
5.0.101
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: foca
How can I run this application?
Update, I ran sudo apt-get install lsb-core.
It still does not work, but lsb_release -a is now different:
$ lsb_release -a
LSB Version: core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal
Related, but macOS dotnet core in macOS: "The type initializer for 'Crypto' threw an exception"
A hacky solution is to copy the 5.x libraries into 3.x:
sudo cp /usr/share/dotnet/shared/Microsoft.NETCore.App/5.0.2/libSystem.Security.Cryptography.Native.OpenSsl.a /usr/share/dotnet/shared/Microsoft.NETCore.App/3.1.11/libSystem.Security.Cryptography.Native.OpenSsl.a
sudo cp /usr/share/dotnet/shared/Microsoft.NETCore.App/5.0.2/libSystem.Security.Cryptography.Native.OpenSsl.so /usr/share/dotnet/shared/Microsoft.NETCore.App/3.1.11/libSystem.Security.Cryptography.Native.OpenSsl.so

Building scriptcs on Ubuntu 18.04 got errors

How to install scriptcs on Ubuntu 18.04? I've got some build errors.
I'm trying to run c# code using Visual Studio Code on Ubuntu 18.04. I downloaded VScode and installed extensions(C#, C# Extensions, C# XML Documentation Comments, Code Runner). But when I run using ctrl+shift+p, It throws error "/bin/sh: 1: scriptcs: not found". So I install mono-complete for prerequisites and tried to install scriptcs with https://github.com/scriptcs/scriptcs/wiki/Building-on-Mac-and-Linux. But it failed with several build errors.
Below is full error codes
silvara#silvara-B85N-PHOENIX-WIFI:~/workspace/scriptcs$ sudo ./build.sh
[sudo] password for silvara:
Checking Paket version (prerelease requested)...
Paket.exe 5.203.2 is up to date.
Paket version 5.203.2
The last restore is still up to date. Nothing left to do.
Performance:
- Runtime: 73 milliseconds
Building project with version: LocalBuild
Shortened DependencyGraph for Target Pack:
<== Pack
<== Test
<== Build
<== Clean
The running order is:
- Clean
- Build
- Test
- Pack
Running build with 1 worker
Starting Target: Clean
Deleting /home/silvara/workspace/scriptcs/src/ScriptCs/bin
Deleting /home/silvara/workspace/scriptcs/src/ScriptCs.Contracts/bin
Deleting /home/silvara/workspace/scriptcs/src/ScriptCs.Core/bin
Deleting /home/silvara/workspace/scriptcs/src/ScriptCs.Engine.Roslyn/bin
Deleting /home/silvara/workspace/scriptcs/src/ScriptCs.Hosting/bin
Deleting /home/silvara/workspace/scriptcs/test/ScriptCs.Core.Tests/bin
Deleting /home/silvara/workspace/scriptcs/test/ScriptCs.Engine.Roslyn.Tests/bin
Deleting /home/silvara/workspace/scriptcs/test/ScriptCs.Hosting.Tests/bin
Deleting /home/silvara/workspace/scriptcs/test/ScriptCs.Tests/bin
Deleting /home/silvara/workspace/scriptcs/test/ScriptCs.Tests.Acceptance/bin
Deleting /home/silvara/workspace/scriptcs/src/ScriptCs/obj
Deleting /home/silvara/workspace/scriptcs/src/ScriptCs.Contracts/obj
Deleting /home/silvara/workspace/scriptcs/src/ScriptCs.Core/obj
Deleting /home/silvara/workspace/scriptcs/src/ScriptCs.Engine.Roslyn/obj
Deleting /home/silvara/workspace/scriptcs/src/ScriptCs.Hosting/obj
Deleting /home/silvara/workspace/scriptcs/test/ScriptCs.Core.Tests/obj
Deleting /home/silvara/workspace/scriptcs/test/ScriptCs.Engine.Roslyn.Tests/obj
Deleting /home/silvara/workspace/scriptcs/test/ScriptCs.Hosting.Tests/obj
Deleting /home/silvara/workspace/scriptcs/test/ScriptCs.Tests/obj
Deleting /home/silvara/workspace/scriptcs/test/ScriptCs.Tests.Acceptance/obj
Finished Target: Clean
Starting Target: Build (==> Clean)
dotnet "restore"
Restore completed in 282.77 ms for /home/silvara/workspace/scriptcs/src/ScriptCs.Hosting/ScriptCs.Hosting.csproj.
Restore completed in 283.22 ms for /home/silvara/workspace/scriptcs/src/ScriptCs.Contracts/ScriptCs.Contracts.csproj.
Restore completed in 283.22 ms for /home/silvara/workspace/scriptcs/src/ScriptCs.Core/ScriptCs.Core.csproj.
Restore completed in 44.27 ms for /home/silvara/workspace/scriptcs/test/ScriptCs.Core.Tests/ScriptCs.Core.Tests.csproj.
Restore completed in 45.37 ms for /home/silvara/workspace/scriptcs/test/ScriptCs.Hosting.Tests/ScriptCs.Hosting.Tests.csproj.
Restore completed in 7.76 ms for /home/silvara/workspace/scriptcs/test/ScriptCs.Tests.Acceptance/ScriptCs.Tests.Acceptance.csproj.
Restore completed in 364.93 ms for /home/silvara/workspace/scriptcs/src/ScriptCs/ScriptCs.csproj.
Restore completed in 257.35 ms for /home/silvara/workspace/scriptcs/test/ScriptCs.Tests/ScriptCs.Tests.csproj.
Restore completed in 392.31 ms for /home/silvara/workspace/scriptcs/test/ScriptCs.Engine.Roslyn.Tests/ScriptCs.Engine.Roslyn.Tests.csproj.
Restore completed in 700.41 ms for /home/silvara/workspace/scriptcs/src/ScriptCs.Engine.Roslyn/ScriptCs.Engine.Roslyn.csproj.
Building project: ScriptCs.sln
/usr/bin/msbuild ScriptCs.sln /t:Build /v:m /p:RestorePackages="True" /p:Optimize="True" /p:Configuration="Release" /logger:Fake.MsBuildLogger+ErrorLogger,"/home/silvara/workspace/scriptcs/packages/FAKE/tools/FakeLib.dll"
Microsoft (R) Build Engine version 16.0.0.0 for Mono
Copyright (C) Microsoft Corporation. All rights reserved.
/usr/share/dotnet/sdk/2.2.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(129,5): error MSB4018: The "GenerateDepsFile" task failed unexpectedly. [/home/silvara/workspace/scriptcs/src/ScriptCs.Contracts/ScriptCs.Contracts.csproj]
/usr/share/dotnet/sdk/2.2.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(129,5): error MSB4018: System.TypeLoadException: Could not load type of field 'Microsoft.NET.Build.Tasks.DependencyContextBuilder:_filteredPackages' (12) due to: Could not resolve type with token 01000027 from typeref (expected class 'NuGet.Packaging.Core.PackageIdentity' in assembly 'NuGet.Packaging, Version=5.0.0.6, Culture=neutral, PublicKeyToken=31bf3856ad364e35') assembly:NuGet.Packaging, Version=5.0.0.6, Culture=neutral, PublicKeyToken=31bf3856ad364e35 type:NuGet.Packaging.Core.PackageIdentity member:(null) [/home/silvara/workspace/scriptcs/src/ScriptCs.Contracts/ScriptCs.Contracts.csproj]
/usr/share/dotnet/sdk/2.2.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(129,5): error MSB4018: at Microsoft.NET.Build.Tasks.TaskBase.Execute () [0x00000] in <c5e0e1dbe29a4fae9ad829d4b52ccc8b>:0 [/home/silvara/workspace/scriptcs/src/ScriptCs.Contracts/ScriptCs.Contracts.csproj]
/usr/share/dotnet/sdk/2.2.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(129,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute () [0x00029] in <58f0218f988743a48dd7c84cbe933f4e>:0 [/home/silvara/workspace/scriptcs/src/ScriptCs.Contracts/ScriptCs.Contracts.csproj]
/usr/share/dotnet/sdk/2.2.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(129,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask (Microsoft.Build.BackEnd.ITaskExecutionHost taskExecutionHost, Microsoft.Build.BackEnd.Logging.TaskLoggingContext taskLoggingContext, Microsoft.Build.BackEnd.TaskHost taskHost, Microsoft.Build.BackEnd.ItemBucket bucket, Microsoft.Build.BackEnd.TaskExecutionMode howToExecuteTask) [0x001f6] in <58f0218f988743a48dd7c84cbe933f4e>:0 [/home/silvara/workspace/scriptcs/src/ScriptCs.Contracts/ScriptCs.Contracts.csproj]
Running build failed.
Error:
Building ScriptCs.sln failed with exitcode 1.
---------------------------------------------------------------------
Build Time Report
---------------------------------------------------------------------
Target Duration
------ --------
Clean 00:00:00.0372588
Build Failure
Total: 00:00:07.0042658
---------------------------------------------------------------------
Status: Failure
---------------------------------------------------------------------
---------------------------------------------------------------------
1) Building ScriptCs.sln failed with exitcode 1.
2) MSB4018: /usr/share/dotnet/sdk/2.2.203/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(129,5): The "GenerateDepsFile" task failed unexpectedly.
3) System.TypeLoadException: Could not load type of field 'Microsoft.NET.Build.Tasks.DependencyContextBuilder:_filteredPackages' (12) due to: Could not resolve type with token 01000027 from typeref (expected class 'NuGet.Packaging.Core.PackageIdentity' in assembly 'NuGet.Packaging, Version=5.0.0.6, Culture=neutral, PublicKeyToken=31bf3856ad364e35') assembly:NuGet.Packaging, Version=5.0.0.6, Culture=neutral, PublicKeyToken=31bf3856ad364e35 type:NuGet.Packaging.Core.PackageIdentity member:(null)
4) at Microsoft.NET.Build.Tasks.TaskBase.Execute () [0x00000] in <c5e0e1dbe29a4fae9ad829d4b52ccc8b>:0
5) at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute () [0x00029] in <58f0218f988743a48dd7c84cbe933f4e>:0
6) at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask (Microsoft.Build.BackEnd.ITaskExecutionHost taskExecutionHost, Microsoft.Build.BackEnd.Logging.TaskLoggingContext taskLoggingContext, Microsoft.Build.BackEnd.TaskHost taskHost, Microsoft.Build.BackEnd.ItemBucket bucket, Microsoft.Build.BackEnd.TaskExecutionMode howToExecuteTask) [0x001f6] in <58f0218f988743a48dd7c84cbe933f4e>:0
---------------------------------------------------------------------
Here is a temporary simple fix:
sudo apt install dotnet-sdk-2.2=2.2.105-1
sudo apt-mark hold dotnet-sdk-2.2
sudo ./build.sh
This is a compatibility issue that Microsoft has no motivation to fix since we are using Linux...

c# Access to the path denied on ubuntu 18.04 server

My friend and I wanted to make a discord server. I made the bot in c# and needed something to host it on so it can run 24/7, I've chosen Digitalocean. I created a ubuntu 18.04 droplet, installed dotnet sdk on it and cloned my github bot repo (Not the best solution, I know). But when I tried to do dotnet run, I got the "Access to path the path ' ' is denied" Error. I got the same error when i tried it using docker on ubuntu 16.04. Can anyone help me with this problem? (It worked fine on windows 10)
Dockerfile:
FROM microsoft/dotnet:2.1-sdk
WORKDIR /app
# copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# copy and build everything else
COPY . ./
RUN dotnet publish -c Release -o out
ENTRYPOINT ["dotnet", "out/Bot.dll"]
the error:
Unhandled Exception: System.UnauthorizedAccessException: Access to the path '/app/out' is denied. ---> System.IO.IOException: Permission denied
--- End of inner exception stack trace ---
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at Bot.Program.MainAsync() in /app/Program.cs:line 38
at Bot.Program.Main(String[] args) in /app/Program.cs:line 16
chmod -x and chmod -R 777 didn't work
You have to make a release publish for ubuntu, give permission for execution with chmod -R 777 PATHTOPUBLISHFOLDER and then running it with path to the file without extension in the publish folder

GenerateRuntimeConfigurationFiles task failed unexpectedly

I'm getting inconsistent errors when building my docker image.
This fails approximately 1 out of 3 times, if i keep running docker build.
The full error is:
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: The "GenerateRuntimeConfigurationFiles" task failed unexpectedly. [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: System.IO.IOException: The process cannot access the file '/app/MyProject.Api/bin/Release/netcoreapp2.1/MyProject.Api.runtimeconfig.json' because it is being used by another process. [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: at System.IO.FileStream.Init(FileMode mode, FileShare share) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: at System.IO.File.Create(String path) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: at Microsoft.NET.Build.Tasks.GenerateRuntimeConfigurationFiles.WriteToJsonFile(String fileName, Object value) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: at Microsoft.NET.Build.Tasks.GenerateRuntimeConfigurationFiles.WriteRuntimeConfig(ProjectContext projectContext) [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: at Microsoft.NET.Build.Tasks.GenerateRuntimeConfigurationFiles.ExecuteCore() [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: at Microsoft.NET.Build.Tasks.TaskBase.Execute() [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [/app/MyProject.Api/MyProject.Api.csproj]
/usr/share/dotnet/sdk/2.1.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(171,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [/app/MyProject.Api/MyProject.Api.csproj]
MyProject.Api -> /app/MyProject.Api/bin/Release/netcoreapp2.1/MyProject.Api.dll
MyProject.Api -> /app/MyProject.Api/out/
The command '/bin/sh -c ps -ef && dotnet publish --configuration Release --no-restore --framework netcoreapp2.1 --output out' returned a non-zero code: 1
What bothers me is this line The process cannot access the file '/app/MyProject.Api/bin/Release/netcoreapp2.1/MyProject.Api.runtimeconfig.json' because it is being used by another process. [/app/MyProject.Api/MyProject.Api.csproj]
What other process could be locking the file ?
When the build process doesn't fail this warning shows up every time:
/usr/share/dotnet/sdk/2.1.301/Microsoft.Common.CurrentVersion.targets(4560,5): warning MSB3026: Could not copy "/app/MyProject.Api/Keys/public.pem" to "bin/Release/netcoreapp2.1/Keys/public.pem". Beginning retry 1 in 1000ms. The process cannot access the file '/app/MyProject.Api/bin/Release/netcoreapp2.1/Keys/public.pem' because it is being used by another process. [/app/MyProject.Api/MyProject.Api.csproj]
Which looks like the same issue, but is resolved on it's own, and builds the image, and the public.pem file has been copied correctly.
my dockerfile:
FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app
COPY . ./
RUN dotnet restore --source https://api.nuget.org/v3/index.json
RUN dotnet publish --configuration Release --no-restore --framework netcoreapp2.1 --output out
FROM microsoft/dotnet:2.1.1-aspnetcore-runtime as runtime
WORKDIR /app
COPY --from=build-env /app/MyProject.Api/out .
ENTRYPOINT ["dotnet", "MyProject.Api.dll"]
and i run docker build like so:
docker build --no-cache -t myproject:test -f Dockerfile .
the only similar issues i've found posted on github, are consistent failures where this works most of the time, but fails regularly, and i still don't understand why.
Any help is appreciated.
I think some of the commands in the Dockerfile need to be changed.
FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
# Build runtime image
FROM microsoft/dotnet:2.1.1-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "MyProject.Api.dll"]
References:
https://docs.docker.com/engine/examples/dotnetcore/#prerequisites
https://raw.githubusercontent.com/dotnet/dotnet-docker/master/samples/dotnetapp/Dockerfile
You can use fuser [used file] on linux
Or lsof [used file] on MacOS
to find the pid of that process.

Categories