I am trying to run my first .Net Core Web API(Core 2.2) on Docker in local host. I enabled Docker support(Linux) while creating the project.
Docker File
FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR /src
COPY ["SampleApp/SampleApp.csproj", "SampleApp/"]
RUN dotnet restore "SampleApp/SampleApp.csproj"
COPY . .
WORKDIR "/src/SampleApp"
RUN dotnet build "SampleApp.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "SampleApp.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "SampleApp.dll"]
When I try to run the API on Docker from VS, I get the following error
Error CTP1001 An error occurred while attempting to build Docker image.
and when I looked into the output window
1>------ Build started: Project: SampleApp, Configuration: Debug Any CPU ------
1>SampleApp -> E:\Sandbox\ContainerDeployment\SampleApp\SampleApp\bin\Debug\netcoreapp2.2\SampleApp.dll
1>docker build -f "E:\Sandbox\ContainerDeployment\SampleApp\SampleApp\Dockerfile" -t sampleapp:dev --target base --label "com.microsoft.created-by=visual-studio" "E:\Sandbox\ContainerDeployment\SampleApp"
1>#1 [internal] load build definition from Dockerfile
1>#1 transferring dockerfile: 32B done
1>#2 [internal] load .dockerignore
1>#2 sha256:0237a52bf01ebc117c468d73e0fc890d42166c17898aa5c4fd0fd96dcaee67e8
1>#1 sha256:bb73acd705ce58a2a5ccfdd8d06dc6e46e04a03a430562efdf99961870d2e178
1>#2 transferring context: 34B done
1>
1>#2 DONE 0.0s
1>
1>#3 sha256:2209054a124f7bc1424fa6735d588f36916bcec272f45a5e3e54f7288e65d73e
1>#3 [internal] load metadata for docker.io/microsoft/dotnet:2.2-aspnetcore-runtime
1>#1 DONE 0.0s
1>#3 ERROR: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
1>------
1>------
1> > [internal] load metadata for docker.io/microsoft/dotnet:2.2-aspnetcore-runtime:
1>failed to solve with frontend dockerfile.v0: failed to create LLB definition: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
1>C:\Users\gopalk\.nuget\packages\microsoft.visualstudio.azure.containers.tools.targets\1.4.10\build\Container.targets(258,5): error CTP1001: An error occurred while attempting to build Docker image.
1>Done building project "SampleApp.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
.NET Core version 2.2 is not supported anymore, the current LTS .NET version is 6.0. You can see the existing image versions of ASP.NET Core runtime on docker hub.
Updating .NET version on your machine:
If you are using Visual Studio you can use the Visual Studio Installer to install .NET 6 and all the templates that come with it. Then you should either recreate your project with .NET version 6, or upgrade you existing projects to .NET 6 and then re-generate your dockerfile, You can generate a Dockerfile in Visual Studio by right clicking the project file --> add --> docker support, before these steps you should delete your existing dockerfile.
I am trying to run RUN dotnet restore --configfile NuGet.config Ithought.Web.sln from a dockefile which looks like:-
FROM www.image.from:5000/a/private.docker-registry:1 AS build
WORKDIR /delivery
# Copy csproj and sln and restore as distinct layers
COPY NuGet.config .
COPY Ithought.Web/Ithought.Web.csproj Ithought.Web/
COPY Ithought.Web.Delivery/Ithought.Web.Delivery.csproj Ithought.Web.Delivery/
COPY Ithought.Web.sln .
RUN dotnet restore --configfile NuGet.config Ithought.Web.sln //it fails here
but docker build fails with the following error:-
ERROR [build 7/10] RUN dotnet restore --configfile NuGet.config Ithought.Web.sln 1.5s
------
> [build 7/10] RUN dotnet restore --configfile NuGet.config Ithought.Web.sln:
#14 1.190 /usr/share/dotnet/sdk/2.2.207/NuGet.targets(246,5): error MSB3202: The project file "/Smidge/src/Smidge/Smidge.csproj" was not found. [/delivery/Ithought.Web.sln]
However when I try dotnet restore --configfile NuGet.config Ithought.Web.sln from outside of the dockerfile it works fine
Please note that along with the public nuget feed I am also trying to restore packages from a private nuget feed and I am wondering if docker is able to authenticate with the private nuget although I have the private nuget's credentials added to the nuget.config file. I have seen a lot of similar questions on the internet but I have not come across anyone who is trying to do a dotnet restore from within a dockerfile from a private nuget
I have a dotnet core 2.2 api application. There are 3 layers: API layer, Business Service and Data access layer. In Data Access Layer I have used IHttpClientFactory.
While running through docker I am getting below errors:
warning MSB3245: Could not resolve this reference. Could not locate the assembly "Microsoft.Extensions.Http". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
error CS0246: The type or namespace name 'IHttpClientFactory' could not be found (are you missing a using directive or an assembly reference?) [/app/MyApp.Service/MyApp.DB.csproj]
Here is my docker file:
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build
WORKDIR /app
COPY *.sln .
COPY MyApp/. ./MyApp/
COPY MyApp.Application/. ./MyApp.Application/
COPY MyApp.DB/. ./MyApp.DB/
RUN dotnet restore
COPY MyApp/. ./MyApp/
WORKDIR /app/MyApp
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2 AS runtime
WORKDIR /app
COPY --from=build /app/MyApp/out ./
ENTRYPOINT ["dotnet", "MyApp.dll"]
I've been developing web app ASP.NET CORE 2.1 and running it in Visual Studio with Docker happily. Until now.
Out of blue when I try to start debugging it does the build part, but
completely skips the running part. So now I am unable to debug my
Docker container with Visual Studio 2017.
There are no errors shown, everything is seems to be alright, except the fact, that app does not start. Debug output even not in the list of available ones in "Output" tab.
Container seems to be running, but for some reason Visual Studio is not attaching to the Docker image for debugging.
Any help would be greatly appreciated.
Restarting Visual Studio or computer, deleting bin, obj,.vs, %userprofile%\vsdbg and %userprofile%\onecoremsvsmon didn't make any difference.
Here is my environment:
Windows10
Microsoft Visual Studio Enterprise 2017 Version 15.9.4
VisualStudio.15.Release/15.9.4+28307.222
ASP.NET Core Razor Language Services 15.8.31590
Microsoft Visual Studio Tools for Containers 1.1
Visual Studio Tools for Containers 1.0
Docker:
Version 2.0.0.0-win81 (29211)
Build: 4271b9e
Engine:18.09.0
Compose:1.23.2
Machine:0.16.0
Container appears to be running:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
22852e61b41a dockertest "tail -f /dev/null" 4 minutes ago Up 4 minutes 0.0.0.0:50659->80/tcp, 0.0.0.0:44304->443/tcp priceless_pare
Dockerfile:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["Dockertest/Dockertest.csproj", "Dockertest/"]
RUN dotnet restore "Dockertest/Dockertest.csproj"
COPY . .
WORKDIR "/src/Dockertest"
RUN dotnet build "Dockertest.csproj" -c Release -o /app
FROM build AS publish
RUN dotnet publish "Dockertest.csproj" -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Dockertest.dll"]
VS build output:
Target ContainerPrepareForLaunch:
Using "ResolveVsDbgPath" task from assembly "C:\Users\aaaa\.nuget\packages\microsoft.visualstudio.azure.containers.tools.targets\1.0.2105168\build\..\tools\Microsoft.VisualStudio.Containers.Tools.Tasks.dll".
Task "ResolveVsDbgPath"
Done executing task "ResolveVsDbgPath".
Task "ContainerError" skipped, due to false condition; ('$(TargetFrameworkIdentifier)' == '.NETCoreApp' And
'#(_Dockerfile->AnyHaveMetadataValue('TargetOS', 'Linux'))' == 'true' And
!Exists('$(ContainerVsDbgPath)\vsdbg')) was evaluated as ('.NETCoreApp' == '.NETCoreApp' And
'true' == 'true' And
!Exists('C:\Users\aaaa\vsdbg\vs2017u5\vsdbg')).
Using "ResolveMsVsMonPath" task from assembly "C:\Users\aaaa\.nuget\packages\microsoft.visualstudio.azure.containers.tools.targets\1.0.2105168\build\..\tools\Microsoft.VisualStudio.Containers.Tools.Tasks.dll".
Task "ResolveMsVsMonPath"
Done executing task "ResolveMsVsMonPath".
Using "PrepareForLaunch" task from assembly "C:\Users\aaaa\.nuget\packages\microsoft.visualstudio.azure.containers.tools.targets\1.0.2105168\build\..\tools\Microsoft.VisualStudio.Containers.Tools.Tasks.dll".
Task "PrepareForLaunch"
docker build -f "C:\Projects\Dockertest\Dockerfile" -t dockertest --label "com.microsoft.created-by=visual-studio" "C:\Projects"
Sending build context to Docker daemon 3.007GB
Step 1/18 : FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
---> 1fe6774e5e9e
Step 2/18 : WORKDIR /app
---> Using cache
---> 930ee0770e2d
Step 3/18 : EXPOSE 80
---> Using cache
---> 4f184a8a4bd4
Step 4/18 : EXPOSE 443
---> Using cache
---> bdf7dc4e92fd
Step 5/18 : FROM microsoft/dotnet:2.1-sdk AS build
---> 04868b49e01f
Step 6/18 : WORKDIR /src
---> Using cache
---> c27b25a30aeb
Step 7/18 : COPY ["Dockertest/Dockertest.csproj", "Dockertest/"]
---> Using cache
---> 009cbf61e5ae
Step 8/18 : RUN dotnet restore "Dockertest/Dockertest.csproj"
---> Using cache
---> 9baf38b818c8
Step 9/18 : COPY . .
---> ae210461c587
Step 10/18 : WORKDIR "/src/Dockertest"
---> Running in 024c6719cbd3
Removing intermediate container 024c6719cbd3
---> f398384b2049
Step 11/18 : RUN dotnet build "Dockertest.csproj" -c Release -o /app
---> Running in cacc4471c224
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 57.06 ms for /src/Dockertest/Dockertest.csproj.
Dockertest -> /app/Dockertest.dll
Dockertest -> /app/Dockertest.Views.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:09.30
Removing intermediate container cacc4471c224
---> 5854863d47c7
Step 12/18 : FROM build AS publish
---> 5854863d47c7
Step 13/18 : RUN dotnet publish "Dockertest.csproj" -c Release -o /app
---> Running in 69df3a178cd7
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 67.48 ms for /src/Dockertest/Dockertest.csproj.
Dockertest -> /src/Dockertest/bin/Release/netcoreapp2.1/Dockertest.dll
Dockertest -> /src/Dockertest/bin/Release/netcoreapp2.1/Dockertest.Views.dll
Dockertest -> /app/
Removing intermediate container 69df3a178cd7
Step 14/18 : FROM base AS final
---> 766815671751
---> bdf7dc4e92fd
Step 15/18 : WORKDIR /app
---> Using cache
---> 61f28b8a64fa
Step 16/18 : COPY --from=publish /app .
---> 6b6711105d65
Step 17/18 : ENTRYPOINT ["dotnet", "Dockertest.dll"]
---> Running in f3be0a8361ee
Removing intermediate container f3be0a8361ee
---> 774100a76bc9
Step 18/18 : LABEL com.microsoft.created-by=visual-studio
---> Running in 76d649057957
Removing intermediate container 76d649057957
---> dbf49735c067
Successfully built dbf49735c067
Successfully tagged dockertest:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
docker rm -f ce37c3274e9a73fdfeb0b5cb48d451cf3fea687ee3b5c91388ecad1d1f695149
ce37c3274e9a73fdfeb0b5cb48d451cf3fea687ee3b5c91388ecad1d1f695149
docker run -dt -v "C:\Users\aaaa\vsdbg\vs2017u5:/remote_debugger:rw" -v "C:\Users\aaaa\AppData\Roaming\ASP.NET\Https:/root/.aspnet/https:ro" -v "C:\Users\aaaa\AppData\Roaming\Microsoft\UserSecrets:/root/.microsoft/usersecrets:ro" -e "ASPNETCORE_URLS=https://+:443;http://+:80" -e "ASPNETCORE_HTTPS_PORT=44304" -e "ASPNETCORE_ENVIRONMENT=Development" -p 50659:80 -p 44304:443 --entrypoint tail dockertest -f /dev/null
22852e61b41ad164632588db2b4027ad59cc5e876956fb99c7e8eadcbcfcdd77
Done executing task "PrepareForLaunch".
Target "_CheckForInvalidConfigurationAndPlatform" skipped. Previously built successfully.
Target "_CheckForInvalidConfigurationAndPlatform" skipped. Previously built successfully.
Target ReportTypeScriptVersion:
Using "FormatLocalizedString" task from assembly "C:\Program Files (x86)\Microsoft SDKs\TypeScript\3.1\build\TypeScript.Tasks.dll".
Task "FormatLocalizedString"
Done executing task "FormatLocalizedString".
Task "FormatLocalizedString" skipped, due to false condition; ('$(TypeScriptVersionCheckResult)' == 'Downgrade' OR '$(TypeScriptVersionCheckResult)' == 'Upgrade') was evaluated as ('NoneSpecified' == 'Downgrade' OR 'NoneSpecified' == 'Upgrade').
Task "Warning" skipped, due to false condition; ('$(TypeScriptShowVersionWarning)' == 'true' AND '$(TypeScriptVersionCheckResult)' == 'NoneSpecified') was evaluated as ('' == 'true' AND 'NoneSpecified' == 'NoneSpecified').
Task "Warning" skipped, due to false condition; ('$(TypeScriptShowVersionWarning)' == 'true' AND ('$(TypeScriptVersionCheckResult)' == 'Downgrade' OR '$(TypeScriptVersionCheckResult)' == 'Upgrade')) was evaluated as ('' == 'true' AND ('NoneSpecified' == 'Downgrade' OR 'NoneSpecified' == 'Upgrade')).
Task "Warning" skipped, due to false condition; ('$(TypeScriptBuildMode)' == 'true' AND ($(TypeScriptToolsVersion.StartsWith('2.')) OR $(TypeScriptToolsVersion.StartsWith('1.')))) was evaluated as ('' == 'true' AND (False OR False)).
Target Build:
Target CleanupEmptyRefsFolder:
Using "RemoveDir" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Task "RemoveDir"
Directory "C:\Projects\Dockertest\bin\Release\netcoreapp2.1\refs" doesn't exist. Skipping.
Done executing task "RemoveDir".
Target "ExecuteToolsTarget" skipped, due to false condition; ('#(ToolsTarget)' != '') was evaluated as ('' != '').
Target "_PackAsBuildAfterTarget" skipped, due to false condition; ('$(GeneratePackageOnBuild)' == 'true' AND '$(IsInnerBuild)' != 'true') was evaluated as ('false' == 'true' AND '' != 'true').
Target "DotNetPublish" skipped, due to false condition; ( '$(DeployOnBuild)' == 'true' ) was evaluated as ( '' == 'true' ).
Done building project "Dockertest.csproj".
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:04:53.13
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
EDIT:
Noticed, that VS "seats" "too long" on following task(before that step has been faster):
docker build -f "C:\Projects\Dockertest\Dockerfile" -t dockertest --label "com.microsoft.created-by=visual-studio" "C:\Projects"
Ok, everything is working now! I am jumping up and down on a pile of pulled hairs, not because I am completely hairless now, but because I've fixed it!:)))
Without further ado, here is what broke the ice:
for some reason Docker For Windows lost "connection"(for lack of the better word or permissions, maybe?) to the drive where project has been residing on. This is exactly what I did to remedy the situation:
Open Docker For Window "Settings" menu
Went to "Shared Drives"
Unchecked "Shared" check box(mark already been there) for the drive, where project resides
Clicked "Apply" button
Checked "Shared" check box again and clicked "Apply" button again.
During last step login box popped up twice, telling me, that Docker need my credentials to access the drive. Domain name has been prepopulated with my login name. I've entered my password and clicked "OK" button on both occasions.Then I started project and, voila, login page of my web app showed in the browser as expected. Simple, yet so devious!:))
To be totally honest while I've been busy pulling my hairs out, I also did clean up my drive to free some space. I've released to the Wild about 10 GB. IDK, if that contributed or not to the success, but I've read, that some people did that too and it was beneficial for the Docker.
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