Build .NET Core Console Application into Docker - docker

I created a new .NET Core Console Application with Visual Studio 2017 (RTM). Then added Docker support and got the docker file + compose files just fine. However there are few issues with them.
Docker compose files have version 2 which makes the build fail to the following error message
Microsoft.DotNet.Docker.CommandLineClientException: client version 1.22 is too old. Minimum supported API version is 1.24, please upgrade your client to a newer version.
This can be fixed by manually changing the compose file versions to 2.1. (not sure if valid fix) Then you'll get another error message
MSB4006 There is a circular dependency in the target dependency graph involving target "DockerCleanServiceReferences".
This I have no idea how to fix. I know the error message is due to some configuration that causes circular reference (e.g. post build event that does build)
So, any resources or tips how to package the .NET Core console application into docker container manually? I'm just getting to know Docker so don't assume I know anything of it yet.
Another question, that is there some place where I could get updated versions of these Visual Studio templates or are these known issues?

It turned out the problem for me was having my DockerFile, SLN file, and CSPROJ file all in the same folder. You know how when you create a solution, it asks you if you want to create a subdirectory? If you do not, and your SLN and CSPROJ files share the same folder, inevitably the Docker files will be added to this same folder, creating the circular reference. If your SLN file lives in the directory above your CSPROJ file, the DockerFile et al will be put into your parent directory with the SLN file, and all will be well. This solved it for me.

Can you please check if your Docker for Windows is targeting Linux? It's likely you were targeting Windows container, which is not supported with .NET Core yet.

On my first spin of VS2017 with docker, using the default template, I ran in to the same issue.
I referred to this article - https://blogs.msdn.microsoft.com/containerstuff/2017/03/13/visual-studio-2017-client-version-1-22-is-too-old/
This is what worked for me - As recommended, made this changes in docker-compose project's docker-compose.ci.build.yml :
The 'version' parameter on the top of the file which was set to 2, was change to 2.1
Repeated the same changes on the other files in the project including:
docker-compose.yml
docker-compose.override.yml
docker-compose.vs.debug.yml
docker-compose.vs.release.yml

Regarding your question on how to package a .NET Core console application into a Docker image manually. The https://github.com/dotnet/dotnet-docker-samples are intended to answer that very question. Check them out. If you run into issues with them or have suggestions please log an issue (https://github.com/dotnet/dotnet-docker-samples/issues).

Thanks for the post. We will be adding Nano Server container tooling "soon". Until then, you can work with Linux containers which will give a similar experience.

Related

Does mcr.microsoft.com support ASP.NET Core version 2?

Ever since Microsoft updated docker in August from FROM microsoft/ to FROM mcr.microsoft.com/, my docker file is not built using Jenkins.
// this is what edited docker file looks like
FROM mcr.microsoft.com/dotnet/sdk:2.1 AS build
...
FROM mcr.microsoft.com/dotnet/sdk:2.1
I followed this article to update my docker file.
However this solution also occurs the error when I build docker in Jenkins.
Step 1/10 : FROM mcr.microsoft.com/aspnetcore-build:2 AS build
manifest for mcr.microsoft.com/aspnetcore-build:2 not found: manifest unknown: manifest tagged by "2" is not found
Probably because ASP.NET Core version 2 does not exist in updated Docker hub I guess?
While I was searching to solve this problem, I found this article
Do I have to update my ASP.NET Core version?
aspnetcore-build had the sdk needed to build ASP.Net Core applications, this should be rougly the same as mcr.microsoft.com/dotnet/sdk:2.1 so you could try that image. For the runtime you could use mcr.microsoft.com/dotnet/aspnet:2.1.
But you really should update your application as well, 2.0 has been end of life for a long time already and even 2.1 (which was a LTS release) is now out of support.

How do you change Docker OS Support for a .Net Core Web Application Project?

I have a .NET Core Web Application project where I chose the incorrect OS under the "Enable Docker Support" checkbox:
How do I change this for an existing project? And to be clear, I want to target linux, not "Switch to Windows Containers..." in docker.
This turned out to be easy, and I did it through editing the csproj file: Changed <DockerDefaultTargetOS>Windows</DockerDefaultTargetOS> to <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> and reload.
I am still not sure where you would do this from Visual Studio (if it possible).
If only one project is there in the solution, I would delete the project associated with docker-compose.dcproj and Dockerfile inside the API project. After that I would right click and again add docker support to that project.
This would create new dockerfile with linux base image.
And new dcproj will have linux mentioned
<DockerTargetOS>Linux</DockerTargetOS>
In my case, I am using a .Net Core 3.1 API project and just editing the project file didn't work. I also had to indicate that the docker image was different. This is what I did:
On the Solution Explorer, Right-click the project, select "Unload"
Right-click the project, and edit it (The project file can also be edited outside. Even Right click, Add, Docker support worked once)
As mentioned by others, change:
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
To:
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
(Only change "Windows" to "Linux")
Change the image name. Edit the file called "Dockerfile". Change:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-nanoserver-1903 AS base
To:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
I also changed the build name accordingly. Changed:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-nanoserver-1903 AS build
To:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
The image names may be different. Just make sure to change the Windows image to the Linux image.
For ASP.NET Core projects targeting .NET Core, there are two options for adding Docker support via the tooling. Open the project in Visual Studio, and choose one of the following options:
Select Docker Support from the Project menu.
Right-click the project in Solution Explorer and select Add > Docker Support.
you should see a windows like this
And select your desired OS.
You can find more info HERE

Docker GetServiceReferences: The given path's format is not supported

Trying to run locally a Web MVC application using Docker. Application compiles and runs fine when Set as StartUp Project outside of the docker-compose project but fails when docker-compose is Set as StartUp Project during the build when it comes to Target DockerGetServiceReferences: with
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Sdks\Microsoft.Docker.Sdk\build\Microsoft.VisualStudio.Docker.Compose.targets(195,5): error : The given path's format is not supported.
I have added the docker-compose through Visual Studio 2017 to an existing application as described here:
How to containerize the .NET Framework web apps with Windows Containers and Docker
I appreciate this might be quite vague so if there are any more details I could provide please let me know.
EDIT:
Adding docker-compose to just default MVC template in a new project works fine so the problem seems to be with the legacy solution. Also noticed I can't navigate docker-compose project General Properties, I get the URI formats are not supported popup error
EDIT:
Adding a brand new MVC project from the template to the solution without any references to other projects also generates the error. Could this indicate that the problem might be somewhere on the solution level?
EDIT:
I have created a new Solution file and started to migrate all the projects. This way I am successfully able to compose docker image. Once I am finished migrating all of the project I will do a comparison on .sln files and try to see what is different.

.Net Core application fails to publish to GCP App Engine becuase of MVC.Abstractions

This is my first foyer into .Net Core and App Engines, so please forgive me if I sound uninformed.
We have a .Net Core Application that we're trying to get published to a GCP App engine (obviously). when I run dotnet publish -c Release it builds just fine without any errors. When I test the program locally it runs just fine and I'm able to access it. However whenever I try to get it on GCP I get the following error:
Updating service [default] (this may take several minutes)...
.................................................................................................................................................failed.
ERROR: (gcloud.app.deploy) Error Response: [9]
Application startup error:
Error:
An assembly specified in the application dependencies manifest (ApplicationName.deps.json) was not found:
package: 'Microsoft.AspNetCore.Mvc.Abstractions', version: '2.0.2'
path: 'lib/netstandard2.0/Microsoft.AspNetCore.Mvc.Abstractions.dll'
This assembly was expected to be in the local runtime store as the application was published using the following target manifest files:
aspnetcore-store-2.0.5.xml
Failed to deploy project WebApiDotNetCore to App Engine Flex.
We tried removing it from the dependencies JSON, and that just ended up breaking everything, so it is indeed required. It is installed in the project via nuget, so it should be included with dotnet restore. I've looked around and some sources seem to think that it's the installation of the dotnet core sdk, but I've tried it on three computers and always get the same thing.
Lastly, I should say this happens when I try to deploy through command line as well as directly through Visual Studio with the GCP SDK.
Has anyone experienced this error, or something similar? Any advice or guidance is very much appreciated.
Thanks!
-BT
OP REVISION
As an update I was able to get this resolved aside from the fact that I get a 502 error when I try to load the application. Here are the steps I took for anyone else that is looking what to do:
Pre-reqs: Docker for Windows and Google Cloud SDK installed and running. Running turned out to be a pain with Docker for Windows. Many many restarts and reinstallations.
Open the solution and ensure that the startup project is set correctly.
Right click the startup Project, and select Add > Docker Support.
Select Linux in the popup window and allow the files to be created.
When complete, the Dockerfile should appear in the preview window. Do the following:
For me the first line read: FROM microsoft/aspnetcore:2.0 AS base. Change this to FROM microsoft/aspnetcore-build:2.0 AS base.
Additionally, check to make sure that the last line has the correct .dll name. Docker for Windows will put whatever the project name is rather than the class name, so for me my final .dll names were different than the project name.
Lastly, if your project has any dependencies that are required to run but not to build, then you'll need to manually add them. For me we have a couple of XML files that needed to be put in the app folder, so I had to add COPY *.xml /app/ and put those files in the same folder as the solution file is in.
If there's anything else you need to do to the Dockerfile I highly recommend this page. It's a how-to on all Dockerfile commands written in ENGLISH! (that was my biggest problem with all of this - I have little experience with Linux and even less with Docker and everything was written in Greek for me).
Create an app.yaml file. I just used the standard:
runtime: custom
env: flex
Copy the Dockerfile found in the startup project's folder into the folder with the solution.
Initialize gcloud to the right project, then navigate to the solution folder. The type gcloud app deploy app.yaml, and follow the onscreen guide.
for me it takes about 15 minutes to deploy the GCP, so depending on the complexity of your project it may take longer, though this one is rather complex.
Now I'm trying to figure out my 502 error... I've tried what seems like everything - changing the listening port in the application, exposing the listening port on the dockerfile, trying to get GCP to open that port, and trying half a dozen different ports. It's slow-going since it's such a chore to deploy each time.
Hope this helps anyone that was like me a couple weeks ago and had never even heard of Docker!
Which version of .NET Core is this? Also, have you tried to run in Cloud Shell? Maybe that will provide more clues on what might be wrong.
It looks that you don't have the Microsoft.AspNetCore.Mvc.Abstractions library installed in your system. Using the .NET CLI, type the following command:
dotnet add package Microsoft.AspNetCore.Mvc.Abstractions --version 2.0.2
After that, to ensure the library is included, run the following:
dotnet restore
dotnet build
Try running it locally (it should work), and then use the dotnet publish -c Release command again.

docker support visual studio 2017 fails

I've been playing with docker for a while and was pleased to see the new tools in VS2017. I've had issues before with docker for windows but I can't see why this one is happening.
I create a webapp just straight from the box and add docker support. I then simply try and launch it with no changes and I get the following error.
I have checked that the container is registered and it does look to have gotten that far:
a quick docker ps -a yields this:
The thing is the error reporting appears to be trying to deploy from a build path does does indeed not exist (my projects are on my G drive not F, that is where visual studio is installed).
So what gives? I can run the docker-compose from the command line inside the project path and then docker-compose up and it seems fine. Seems visual studio is just using the incorrect path, not sure why.
Version in use are:
Docker for windows 17.04.0-ce-rc2-win6(10840)
Visual studio 2017: Version 15.1(26403.0)
I checked again which version of docker I was using and I was using from the edge stream. I fell back to stable and this issue went away.
I had the same problem. It turns out solution told above of reverting to Docker stable version solved it. With Edge version, the build looks for solution file in the VS IDE folder instead of current folder.

Resources