Objective
. Net core was born to solve cross-platform issues, so the. NET Core app is nothing new to run on Linux, MacOS, or Docker.
It is believed that many of the. NET core projects have been deployed in Docker or Linux environments. However, the development environment of the general developer will not be Ubuntu, redhat or something, since the development environment and deployment environment is very different, we always hope that in the development of debugging, can be more close to the actual deployment environment. So today this article says how to debug an ASP. NET Core app deployed in Docker.
If you have installed VS2017, this is actually the best, debugging and deployment is to select a few buttons, and then you can happily debug.
But there are always cases where VS2017 cannot be installed, so the following is the use of VS code debugging.
The necessary environment for developing the machine:
1.. NET Core 1.1
2. VS code (try the latest version)
3. Docker 17.06.0 '
Project structure
We use the dotnet CLI to first build the project, go to the directory to use, run the following commands,
dotnet New MVC-n webdotnet new sln mytestdotnet sln mytest.sln Add Web/web.csproj
Well, after the project is built, we use vs to open, wait, Vscode will automatically help us to create a. Vscode folder, there will be some debugging configuration files, such as:
Open the Program.cs file, modify the code of the Main method, mainly to modify the port, in order to avoid conflicts with other ports
Public Static void Main (string[] args) { varnew webhostbuilder () . Usekestrel () . Usecontentroot (Directory.GetCurrentDirectory ()) . Useurls ("http://*:6106") . Useiisintegration () . Usestartup<Startup>() . Build (); Host. Run ();}
The project is basically complete, but it has to be dockerfile, mostly pushed to Docker's configuration file
We create a new dockerfile in the Web directory with the following content: (Note: This file is not a suffix)
1From microsoft/dotnet:latest2 3RUN apt-Get update4 5RUN Apt-getInstallCurlUnzip-y6 7RUN Curl-ssl https://Aka.ms/getvsdbgsh | bash/dev/stdin-v latest-l ~/vsdbg8 9RUNchmod 777/root/vsdbgTen OneCOPY./publish/app A -Workdir/app - theentrypoint ["dotnet","Web.dll"]
Simply explain the configuration above,
Line 1th means the latest version of the image based on microsoft/dotnet.
Lines 3rd and 5 are the 2 tools for updating apt-get and installing Curl,unzip (where the last-y is required, otherwise Docker will wait for command input, which will eventually cause debugging to fail)
The 7th, 9 line is curl installs Vsdebuger, and this installs the directory to adjust the permission
Line 11 is to copy the contents of the Publish directory under the development environment to the/app directory in the Docker image
13 rows is the specified Docker working directory is/app
Line 15 is the entrance to the run, the translation is Dotnet Web.dll (a bit like a local deployment after the run)
Deploy to Docker
We must first publish the. NET Core app, be sure to select the debug mode, the subsequent specified release path is also specified, otherwise the 11th line of Dockerfile will be modified
dotnet publish-c Debug-o./publish
After running the Web directory will be more publish directory, the following are a lot of dotnet release files (today do not talk about the minimum number of releases, things are not related to the point)
Download the image microsoft/dotnet:lastest from Docker and make your own image Myimage1
Docker pull microsoft/dotnetdocker image tag Microsoft/dotnet:latest myimage1:latest
After that, we're going to publish the. NET core stuff to Docker. The last point is "." is a must!!!
Docker build-t Myimage1-rm .
The process of deployment depends on your network, because there are a few things to install, the Vsdebuger is quite slow, but it can be solved, we discuss the solution after the end of the article
When the Docker build is finished, we let the app run in the container.
6106:6106 --name mytestcontainer myimage1
Where-P 6,106:6,106 refers to the 6106 port of the machine to point to the container 6106 port, in fact, here can not correspond.
--name Mytestcontainer refers to this myimage1 image, using the name Mytestcontainer to run the container. The advantage is that the operation of our team container can be done just by remembering this name, without remembering that Docker automatically gives us the generated ID.
So the. NET Core app runs in the container and we open the localhost:6106 and we actually see the effect.
VS Code Debug Configuration
Open a. vscode/launch.json file. Click on the add configuration in the lower right corner to add a debug config.
After that Vscode will automatically prompt to choose which debugging configuration, we directly select the. Net:attach to remote. Net Core Console App (that is, the second ... ),
After the click, the following configuration will be generated, there are a lot of things to change. There are also some hints, such as Debuggerpath is the location of the input debugger on the target computer.
Finally, we're going to change the contents of the configuration node to the following
1 {2"Name": ". NET Core Attach",3"Type": "CoreCLR",4"Request": "Attach",5"ProcessId": "${command:pickremoteprocess}",6"Pipetransport": {7"PIPECWD": "${workspaceroot}",8"Pipeprogram": "Bash",9"Pipeargs": [Ten"-C", One"Docker exec-i Mytestcontainer ${debuggercommand}" A ], -"Debuggerpath": "/root/vsdbg/vsdbg" - }, the"Sourcefilemap": { -"/users/admin/my/github/test_debug_docker": "${workspaceroot}" - } -}
The-I mytestcontainer in line 11th is the name of the container we specified earlier, and it can be replaced with an ID.
Debuggerpath in line 13 is just curl download installed vsdbg, here to pay attention to the Docker build when vsdbg installed directory where
16 lines of Sourcefilemap refers to the entire project in your development environment location, simple point is vscode you open the directory (this is actually more profound, this is not discussed here)
OK, everything is ready, debug it!!
Debug effect
Press F5, and then let you choose which process to debug, of course choose dotnet Web.dll This
Wait some time, then we can, and then we're going to set a breakpoint on Homecontroller.cs this file's about method
Then open this link: http://localhost:6106/Home/About, you can see the endpoint has gone in.
And then you can see his value in the left variable, the same as the left.
We can also modify his value in debug console, such as entering viewdata["Message" = "test test", and you can see that the left variable has changed.
The content of the page also becomes test test.
Summarize
We used to debug the MVC page in VS, we changed the content of cshtml, we can see the change effect immediately after saving and refresh the page, but not in vs code. It is estimated that the reason for Docker is that it should be solved later.
Another disadvantage is that attaching to a process is always a disadvantage, he cannot attach multiple processes at the same time, if you want to debug multiple systems, you can only start multiple vs code at the same time, this contrast vs debugging is too big difference. Since the system would like to be deployed in Docker, it is estimated that it is modular or even a microservices unit, such a relatively complete system, it is sure to design a lot of sub-modules between debugging. Although each module may not be the same person to do, the development environment can be attached to debugging is not much better.
Solve the Vsdebugger download super slow Way
In fact, the installation of Vsdebugger, just download the file, and then put in the specified directory.
So the solution is to download this vsdebugger, and then unzip, put in the development environment under the Web directory, and finally with Dockerfile configuration, the vsdbg copy the past container, the following is the modified Dockerfile
From microsoft/Dotnet:latestrun Apt-Get Updaterun apt-getInstallCurlUnzip-y# RUN Curl-ssl https://Aka.ms/getvsdbgsh | bash/dev/stdin-v latest-l ~/vsdbgCOPY./vsdbg/root/Vsdbgrunchmod 777/root/vsdbgcopy./publish/Appworkdir/Appentrypoint ["dotnet","Web.dll"]
This way, you don't have to wait for Curl to install Vsdebugger.
Download links for Vsdebugger:
Https://vsdebugger.azureedge.net/vsdbg-15-1-10630-1/vsdbg-linux-x64.zip
VS code docker Debug ASP.