why dotnet 5 docker container fail in run time? - c#

I have a dotnet5 Console application that is inside a solution and has dependencies on other sibling projects. I create the Docker file with visual studio tools and this is the Docker file:
FROM mcr.microsoft.com/dotnet/runtime:5.0-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["src/MyProject/MyProject.Launcher/MyProject.Launcher.csproj", "src/MyProject/MyProject.Launcher/"]
COPY ["src/MyProject/MyProject.DataAccess/MyProject.DataAcess.csproj", "src/MyProject/MyProject.DataAccess/"]
COPY ["src/MyProject/MyProject.Service/MyProject.Service.csproj", "src/MyProject/MyProject.Service/]
COPY ["src/Domain/MyProject.Domain.Model/MyProject.Domain.Model.csproj", "src/Domain/MyProject.Domain.Model/"]
RUN dotnet restore "src/MyProject/MyProject.Launcher/MyProject.Launcher.csproj"
COPY . .
WORKDIR "/src/src/MyProject/MyProject.Launcher"
RUN dotnet build "MyProject.Launcher.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyProject.Launcher.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyProject.Launcher.dll"]
building image is ok and it added normally to my local image registry but when I try to run this image and create a container I am getting this error:
It was not possible to find any compatible framework version The
framework 'Microsoft.AspNetCore.App', version '5.0.0' was not found.
No frameworks were found.
You can resolve the problem by installing the specified framework
and/or SDK.

Use this as your base image:
FROM mcr.microsoft.com/dotnet/aspnet:5.0.0-buster-slim AS base

I Think You should check docker hub and search for .NET 5 SDK then install that image on your container then its will be fixed :
I Found Dotnet 3.1 SDK but try to find dotnet 5 SDK
I think after installation this error will be fix
Sorry For my Bad English
:)

Related

multiple asp.net core project have problems in docker

I used Visual Studio 2019 and created my dockerfile with it.
my docker file
#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/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["BackEnd.service.api.csproj", "BackEnd.service.api"]
COPY ["src/BackEnd.Service.Application/BackEnd.Service.Application.csproj", "src/BackEnd.Service.Application/"]
COPY ["src/BackEnd.Service.Model/BackEnd.Service.Model.csproj", "src/BackEnd.Service.Model/"]
RUN dotnet restore "src/BackEnd.service.api/BackEnd.service.api.csproj"
COPY . .
WORKDIR "/src/src/BackEnd.service.api"
RUN dotnet build "BackEnd.service.api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "BackEnd.service.api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BackEnd.service.api.dll"]
and my project tree:
now I have this error:
please help me to how can I change my route of project to build it in docker?
The problem is most likely your build context. Visual Studio seems to be using you web app project as the build context, meaning it can't access your Application and Model projects from the src folder.
I can suggest that you try to explicitly set the Dockerfile context in your web app's .csproj file with the following property to the src directory from which all projects are accessible:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<DockerfileContext>..\</DockerfileContext>
</PropertyGroup>
Then you may need to update your Dockerfile to point to your reference projects (Application and Model) to be relative towards the src folder.
COPY ["BackEnd.service.api/BackEnd.service.api.csproj", "src/BackEnd.service.api"]
COPY ["BackEnd.Service.Application/BackEnd.Service.Application.csproj", "src/BackEnd.Service.Application/"]
COPY ["BackEnd.Service.Model/BackEnd.Service.Model.csproj", "src/BackEnd.Service.Model/"]
RUN dotnet restore "src/BackEnd.service.api/BackEnd.service.api.csproj"
You may need to play around with the directories a bit, but its very important to remember that the build context is the key.
Alternatively you can try to add a docker-compose project to your solution and have that build your Dockerfile. You will be able to specify a build context in the docker-compose.yml file for each project that should be built.
I suggest follow this article aspnetcore-app-dockerfiles to make dockerfile manually instead of vs project's docker template. Also, use .dockerignore will help you simplify your dockerfile script, ignore files might not need runtime.
Here is an example from my project, hope helps.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /build
COPY . .
RUN dotnet restore
RUN dotnet publish "src/BackEnd.service.api/BackEnd.service.api.csproj" -c Release -o /app --no-restore
FROM base AS final
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "BackEnd.service.api.dll"]

Error CTC1014 Docker command failed with exit code 1 with dotnet core api

I'm trying to build the most basic web Api application using VS2019 along with docker. Basically it is just the demo app provided by VS. I'm ending up with below error:
Severity Code Description Project File Line Suppression State
Error CTC1014 Docker command failed with exit code 1.
hcsshim::PrepareLayer - failed failed in Win32: Incorrec
Below my dockerfile
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
WORKDIR /app
EXPOSE 8080
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
WORKDIR /src
COPY ["RegexTesterApi/RegexTesterApi.csproj", "RegexTesterApi/"]
RUN dotnet restore "RegexTesterApi/RegexTesterApi.csproj"
COPY . .
WORKDIR "/src/RegexTesterApi"
RUN dotnet build "RegexTesterApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "RegexTesterApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "RegexTesterApi.dll"]
Well this is the auto generated dockerfile. But it looks pretty ok to me (also I'm new to docker).
What is actually causing the error.
I ran into the very same error (CTC1014) in the past. I found out that my project was being run in "Release" mode, when it should have been running in "Debug" mode. So I would like to suggest this as a workaround.
Here's how you change it in VS2019
Of course, running your application in Release mode shouldn't be a problem. So I assumed it must have been related to some optimization employed by most release build configurations, in conflict with Docker. Not exactly sure which one, though.
Cheers! o/
I faced this issue too, but with .net core 2.1. I'm not sure why it's working but, If this solution is available for you then try the following:
Restart Docker Desktop (This solution is offered in the output window)
Change the docker file.
I replaced this
FROM mcr.microsoft.com/dotnet/runtime:2.1 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:2.1 AS build
WORKDIR /src
to
FROM mcr.microsoft.com/dotnet/core/runtime:2.1-alpine AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:2.1-alpine AS build
WORKDIR /src
Then debug started to work fine even after changing the docker file to start variant, including stops on breakpoints. And my configuration has one additional difference:
I use Debug argument in the docker file and use Debug in Solution configuration
RUN dotnet build "DebugLinux.csproj" -c Debug -o /app/build
This error migh be raised when the dockerfile in your project don't match the Target Framework in the project settings.
Just delete your dockerfile and create a new one and this will fix the problem if this is the issue.

How to run an asp.net core application in docker container?

I'm completelly new to .Net Core and Docker.
I created a project in ASP.NET Core 2.2 and selected docker to run. My objective is to study about .Net Core and Docker.
In my test app I use Rotativa.AspNetCore.
The problem is:
- When I run the project using IIS Express, everything works.
- When I run using docker, the project stops when I call a function in Rotativa.AspNetCore.
The line throwing error is:
return new ViewAsPdf("views/relatorios/render.cshtml", vm);
It works when debuging with IIS Express, but fails using Docker.
I know that, in this moment, Rotativa.AspNetCore calls wkhtmltopdf.exe, maybe the problem is in this call.
Here is my dockerfile, It was automatically generated:
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-nanoserver-1803 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-nanoserver-1803 AS build
WORKDIR /src
COPY ["Internacional/Internacional.csproj", "Internacional/"]
RUN dotnet restore "Internacional/Internacional.csproj"
COPY . .
WORKDIR "/src/Internacional"
RUN dotnet build "Internacional.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Internacional.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Internacional.dll"]
Whats wrong with my setup?
The wkhtmltopdf does not work under the image mcr.microsoft.com/dotnet/core/aspnet, because that based on nanoserver.
The nanoserver is headless, so the OS does not support GUI. And driver printer is not installed in the OS.
Switch the base image to the mcr.microsoft.com/windows/servercore.

Failed to build: Error parsing reference: "microsoft/dotnet:2.1-runtime AS base" is not a valid repository/tag:

I got the below error when build docker for console application using .net core.
Step 1/15 : FROM microsoft/dotnet:2.1-runtime AS base
Error parsing reference: "microsoft/dotnet:2.1-runtime AS base" is not a valid repository/tag: invalid reference format
My Dockerfile looks below
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ConsoleApp2/ConsoleApp2.csproj ConsoleApp2/
RUN dotnet restore ConsoleApp2/ConsoleApp2.csproj
COPY . .
WORKDIR /src/ConsoleApp2
RUN dotnet build ConsoleApp2.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish ConsoleApp2.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "ConsoleApp2.dll"]
My docker version is
Docker version 17.03.1-ce-rc1, build 3476dbf
Can some one suggest what I am doing wrong in this.
Thanks in advance.
Multi-stage builds (which added the FROM ... AS ... syntax along with multiple FROM lines) require 17.05 or newer. You'll need to upgrade your docker server.
For more details on multi-stage builds, see: https://docs.docker.com/develop/develop-images/multistage-build/

AspNetCore project gives System.IO.FileNotFoundException only on linux container

I've got a dotnet core project running AspNetCore webserver. I use a couple of other DLLs which is are fairly simple class libaries.
I can download the repository from git onto my windows PC, go in and run:
dotnet restore
dotnet run
And everything works fine.
However if I do the same thing in a docker container based on microsoft/aspnetcore-build:1.0.7, I get the following error on an HTTP PUT:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLBKHRVH7OND": An unhandled exception was thrown by the application.
System.IO.FileNotFoundException: Could not load file or assembly 'KolData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
Now the file Koldata.dll does exist in the git repository and it's there in the bin/Debug/netcoreapp1.1 folder.
I can re-create the error in Windows by deleting the KolData.dll file in the build directory. So it seems to be that dotnet core on Linux cannot see that file, and I'm unsure why.
I've even tried replacing the DLL with a version built on the machine from source, and it still brings the same error.
One solution
I managed to get it working by changing the csproj file's target framework from:
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
to:
<PropertyGroup>
<TargetFramework>netcoreapp1.0</TargetFramework>
<PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
</PropertyGroup>
This feels a bit strange since KolData.dll is running on 1.1
But now it runs without the error.
You have to create Dockerfile and build docker image.
EDIT:
An example of what Dockerfile should look like. The following file builds Visual Studio project and creates docker image.
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY WebApplication/WebApplication.csproj WebApplication/
RUN dotnet restore
COPY . .
WORKDIR /src/WebApplication
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "WebApplication.dll"]
If you have already built your application include .dll in container with Dockerfile:
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "application.dll"]
Build and run your app:
docker build -t application
docker run -d -p 8000:80 application
Building Docker Images for .NET Core Applications

Categories

Resources