docker build fails to load .net image - docker

I have a docker file which has following line of code. It works on an old Linux build machine but doesn't work on a new ubuntu build machine. On new machine it fails with error "net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)".
FROM microsoft/aspnetcore-build:2.0
RUN dotnet restore -s <url> <project path>
RUN dotnet publish <project path> -c Release -o <path>

.NET 2.0 is not available anymore because the EOL is reached.
Try to migrate your application to at least .NET Core 3.1 or even .NET 6.0 (LTS) / 7.0
/// To .NET Core 3.1
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
/// To .NET 7
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
</Project>
Docker images can be found here for example:
mcr.microsoft.com/dotnet/aspnet:7.0

Related

Dockerize sample dotnet core exe application to kubernetes in linux pod

I'm trying to create a dotnet core application and deploy it to a kubernetes cluster through azure yaml pipelines (with multiple repositories)
I'm hitting the following error:
CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/File.Pod/File.Pod.csproj]
The project was created with this command : dotnet new console -o File.Pod-n File.Pod
My Goal is to deploy an exe. Most of documentations on Internet talks about deployment ASPNET web applications.
csproj generated
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
**<OutputType>Exe</OutputType>**
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.1.7" />
<PackageReference Include="Models" Version="9704.0.0-beta" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.3" />
<PackageReference Include="Utils.Log" Version="2020.9.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shared.Lib\File.Service\File.Service.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Helpers\" />
</ItemGroup>
</Project>
And the docker file:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY ["File.Pod.csproj", "File.Pod/"]
COPY ["Shared.Lib/File.Service/File.Service.csproj", "Shared.Lib/File.Service/"]
COPY ["Shared.Lib/File.DAL/File.DAL.csproj", "Shared.Lib/File.DAL/"]
COPY ["nuget.config", "./"]
RUN dotnet restore "File.Pod/File.Pod.csproj" --configfile nuget.config -nowarn:msb3202,nu1503 --verbosity diag
COPY . .
WORKDIR "/src/File.Pod"
RUN dotnet build "File.Pod.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "File.Pod.csproj" -c Release -o /app
FROM build AS base
RUN apt update && apt install -y openssh-client
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "run"]
As ENTRYPOINT, I tried ENTRYPOINT ["dotnet", "run"] or ENTRYPOINT ["dotnet", "File.POD.DLL"] with no difference.
I feel that the problem is with the type of images ( FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build) , but I'm unsure. I tried various ones with same result.
Exact error log:
2020-09-11T11:05:41.9723837Z Step 10/26 : RUN dotnet build "File.Pod.csproj" -c Release -o /app
2020-09-11T11:05:42.0083655Z ---> Running in 7ed9311b9c89
2020-09-11T11:05:42.7118936Z Microsoft (R) Build Engine version 16.6.0+5ff7b0c9e for .NET Core
2020-09-11T11:05:42.7119408Z Copyright (C) Microsoft Corporation. All rights reserved.
2020-09-11T11:05:42.7119642Z
2020-09-11T11:05:43.1438745Z Determining projects to restore...
2020-09-11T11:05:45.6330496Z Restored /src/File.Pod/File.Pod.csproj (in 2.04 sec).
2020-09-11T11:05:45.6330978Z Restored /src/Shared.Lib/File.Service/File.Service.csproj (in 2.05 sec).
2020-09-11T11:05:45.7516552Z Restored /src/Shared.Lib/File.DAL/File.DAL.csproj (in 101 ms).
2020-09-11T11:05:49.6118355Z File.DAL -> /app/File.DAL.dll
2020-09-11T11:05:50.2378452Z File.Service -> /app/File.Service.dll
2020-09-11T11:05:50.6394251Z CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/File.Pod/File.Pod.csproj]
2020-09-11T11:05:50.6525494Z
2020-09-11T11:05:50.6525914Z Build FAILED.
2020-09-11T11:05:50.6531013Z
2020-09-11T11:05:50.6536286Z CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [/src/File.Pod/File.Pod.csproj]
2020-09-11T11:05:50.6541921Z 0 Warning(s)
2020-09-11T11:05:50.6542199Z 1 Error(s)
8 hours of work and 47 build attempts later:
Changed :
COPY . .
WORKDIR "/src/File.Pod"
to
WORKDIR "/src/File.Pod"
COPY . .
Note : There are tons of docs, but not an exhaustive good one.
I would expect one doc somewhere that would give sufficient knowledge to get started correctly: choice of image, the directory scope, the importance of what to copy (who is able to understand the importance of "COPY . ." that copies the source code to avoid this error, etc...
I started 2 days ago with docker, vsts pipeline, multiple repositories and can already write a book with all the (weird,generic and obscure) errors I hitted.Not talking about the next step now in error, the problems to install docker in windows 2019 (in a VM hosted in Amazon), linux images that do not work (by default?) on the windows 2019 env.
What a nightmare, I wonder what is mature, hitted more problems these past 2 days than over the last decade ! :-)

TeamCity restore TFS NuGet feed 401 unauthorized

TeamCity Professional 2019.1.2 (build 66342)
NuGet.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\packages" />
</config>
<packageSources>
<add key="TFS" value="https://tfs.mydomain.com/nuget/nuget" />
</packageSources>
</configuration>
I have a .NET CLI (dotnet) restore build step including Build Feature NuGet feed credentials
The corresponding command works fine when run from local shell :
> "C:\Program Files\dotnet\dotnet.exe" restore MySolution.sln
> --packages \\packages --source https://tfs.mydomain.com/nuget/nuget --runtime win-x64 --configfile NuGet.config
But TeamCity job run on agent fails with 401 unauthorized :
[15:30:52][restore] C:\Program Files\dotnet\sdk\2.2.101\NuGet.targets(114,5): error : Failed to retrieve information about 'Dapper' from remote source 'https://tfs.mydomain.com/nuget/nuget/FindPackagesById()?id='Dapper'&semVerLevel=2.0.0'.
[15:30:52][restore] C:\Program Files\dotnet\sdk\2.2.101\NuGet.targets(114,5): error : Response status code does not indicate success: 401 (Unauthorized).
I assume shell dotnet restore uses my Windows login creds - exactly what's specified in my Build Feature NuGet feed credentials
Note that I can successfully use same creds in VisualStudio to Manage NuGet Packages ( restore packages on build ).
Try to use the key of the source (TFS) and not the url itself.
Like this;
"C:\Program Files\dotnet\dotnet.exe" restore MySolution.sln --packages \\packages --source tfs --runtime win-x64 --configfile NuGet.config
If that doesn't work, try to add your user credentials manually
nuget sources add -name foo.bar -source C:\NuGet\local -username foo -password bar -StorePasswordInClearText -configfile %AppData%\NuGet\my.config

Where does Ionide + Fake put the output executable?

I am trying to use .NET Core + Ionide + VS Code + Fake + Paket on macOS High Sierra.
Using the project generator, I have created a Suave application called Test. Ionide seems to have generated the appropriate files. After tweaking the TargetFramework to .NET Core, I can build successfully:
$ ./build.sh
...
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:03.72
Finished Target: Build
---------------------------------------------------------------------
Build Time Report
---------------------------------------------------------------------
Target Duration
------ --------
Clean 00:00:00.0026904
InstallDotNetCLI 00:00:01.2292511
Restore 00:00:04.2731055
Build 00:00:07.1234434
Total: 00:00:12.7035334
---------------------------------------------------------------------
Status: Ok
---------------------------------------------------------------------
There are now some files in Test/bin, but none of them are .exe, which is what I would expect as output from fsharpc.
Where does Ionide + Fake put the output executable?
My project has OutputType executable:
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Test.fs" />
</ItemGroup>
<Import Project="..\.paket\Paket.Restore.targets" />
</Project>
.Net Core compiles all projects (even executable applications) to .dll not .exe that can be run with dotnet PATH_TO_DLL. In the bin folder, in the subfolder for given framework target there should be file YOUR_PROJECT_NAME.dll that can be run with dotnet CLI.
To generate an exe you need to supply a run-time identifier. You can include this in the fsproj with
<PropertyGroup>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
</PropertyGroup>
but you don't need to do that. You can use VSCode's tasks.json file to set up a build task.
My suggestion is just to run the dotnet CLI:
dotnet build -c Release -r win10-x64
This will create an exe in the bin\release\netcoreapp2.0\win10-x64 folder. You can also dotnet publish if you want a self-contained directory to deploy (this can get large). The id for OSX will be something like osx-x64.
By default ionide generates an fsproj file that is targetting net461, and you might also need Fake 5 for dotnetcore. I also suggest you use paket in magic-mode, and commit the .exe to github (if you use git).

Why does this dotnet test run crash only in a Docker container?

I have this .NET Core 1.1 solution including an ASP.NET Core project and an NUnit test project. Each of projects has the necessary properties (I can show more if necessary).
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
</PropertyGroup>
I can restore, build, and run the test suite locally (Windows 10) without any problems.
$ dotnet restore
Restoring packages for...
Restore completed in...
Generating MSBuild file...
$ dotnet build
Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Example -> ...
Test -> ...
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed ...
$ cd test/
$ dotnet test
Build started, please wait...
Build completed.
Test run for ...
Microsoft (R) Test Execution Command Line Tool Version 15.3.0-preview-20170628-02
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
NUnit Adapter 3.8.0.0: Test execution started
Running all tests in ...
NUnit3TestExecutor converted 11 of 11 NUnit test cases
Total tests: 11. Passed: 11. Failed: 0. Skipped: 0.
Test Run Succeeded.
Test execution time: ...
The test suite uses NUnit 3 and I'm on all the latest stable versions. Here are the interesting package refs.
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="Microsoft.DotNet.InternalAbstractions" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.3" />
<PackageReference Include="FluentAssertions" Version="4.19.3" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.3" />
<PackageReference Include="NUnit" Version="3.8.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0" />
However, when I run the tests from a .NET Core SDK Docker image/container, they fail dramatically!
The active test run was aborted. Reason: Unable to communicate with test host process.
Here's the simple Dockerfile
FROM microsoft/dotnet:1.1-sdk
WORKDIR /app
COPY . .
The restore and build work find, but the test aborts!
$ docker image build --tag example:latest .
...
$ docker container run --rm -it example:latest
root#abcdef12345:/app# dotnet restore
...
root#abcdef12345:/app# dotnet build
...
root#abcdef12345:/app# cd test/
root#abcdef12345:/app# dotnet test
...
Microsoft (R) Test Execution Command Line Tool Version 15.0.0.0
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
NUnit Adapter 3.8.0.0: Test execution started
Running all tests in ...
NUnit3TestExecutor converted 11 of 11 NUnit test cases
The active test run was aborted. Reason: Unable to communicate with test host process.
Searches for this error message get me nowhere. I'm not sure how to debug a test run abort. Higher levels of verbosity do not print anything useful. Let me know what I can try to get more information, eliminate possibilities, and provide more useful info!

How do I build when Tfs Read-only files block assembly and config file updates (via ccnet)

What are the work-arounds for builds failing when moved to TFS because now each file is set to read-only?
Using CC.net, TFS2008 (TFS 2010 within 2 months), target framework 3.5, msbuild 3.5.
You can use an exec command to do this in the AfterGet target:
<PropertyGroup>
<MyProperty>D:\BuildSource</MyProperty>
</PropertyGroup>
....
<Exec Command="Attrib -r -s *.* /S /D" WorkingDirectory="$(MyProperty)" />

Resources