debugging of an ASP. NET Core Web API application in a Docker container environment
This article focuses on debugging an ASP. NET Core Web API application in a Docker container environment through the visual Studio Tools for Docker–preview plug-in. In the course of their own experiments also encountered some problems, after some testing and search data, the basic solution to these problems, this article will also introduce these issues, so as to avoid the same needs of friends more detours.
Download and installation of plugins
Until this article is written, the Visual Studio Tools for Docker plug-in is still in the preview version (version number: 0.31.0) and can be downloaded here. The following prerequisites are required to properly install this plug-in and be able to successfully debug with the plug-in in Visual Studio 2015:
- Microsoft Visual Studio Update 3
- Enterprise
- Professional
- Community
- Microsoft. NET Core 1.0 SDK for Windows and vs tooling Preview 2
- Windows system has installed Docker for Windows or Docker Toolbox
This article will be presented as a development environment with VS. Enterprise + Windows Ten + Docker for Windows.
Once you have downloaded the plugin, you can install it according to the normal software installation process. Please exit Visual Studio 2015 during the installation process. After the installation is successful, you can find Visual Studio Tools for Docker–preview in extensions and updates in Visual Studio:
Enable Docker support on the ASP. NET Core Web API Project
Open our Dockerwebapi project, right-click on the project and choose the Add –> Docker support menu:
Over time, VS Tools for Docker will add some files to the project:
We will not elaborate on the specific contents and functions of these documents for the time being. Basically in the properties directory is used for visual Studio 2015 and the compilation environment, while Docker-compose and Dockerfile are used for Docker, need to know more information of friends, can first search the Internet to understand.
Start debugging the ASP. NET Core Web API application
Before you start debugging, first open the Docker for Windows Setup, and in the shared drivers, tick the drive where the project is located. If this step is not done, the debugger cannot be started successfully.
Next, you can start debugging by pressing the F5 shortcut key directly. Of course, you can also press the "Start" button on the toolbar to start debugging, and you can see that our Debug button has been set to Docker by default:
The process of starting debugger in Visual Studio 2015 is roughly as follows:
- Invoke PowerShell script Dockertask.ps1 to clean up the environment, such as stopping a running container and deleting an existing Docker image
- Visual Studio compiles a project
- Call PowerShell script Dockertask.ps1 to publish the project to the Bin/docker/debug/app directory
- Start building Docker Image based on docker-compose.debug.yml file content
- Start visual Studio Debugger and ensure that Docker container has successfully loaded by pinging the Http://localhost/api/values endpoint continuously
- Open default browser, start debugging, wait for breakpoint hit
It is important to note that VS Tools for Docker uses 80 ports by default, and if a service with 80 ports is already installed on the system, such as IIS, either stop using the 80 port service, or modify the Yml file in the project and choose to use a different port. Otherwise the compilation process will not complete. Another important point to note is that, in the last case, we specified through the Useurls API that our application can accept 5000 port requests from any address, so We also need to modify the Docker-compose.debug.yml file accordingly so that it can map the host's 80 port to port 5000, as follows:
Once you have successfully started the debugger, you can set breakpoints and, when you hit a breakpoint, use the various debugging experiences provided by Visual Studio as you would debug a normal C # application. As you can see, our debug context is already in the Docker container (Environment.MachineName returns the ID of the Docker container):
About auto-generated Docker Image
After visual Studio Tools for Docker finishes compiling the project, a docker Image named "Username/xxxx:debug" is generated (xxxx is the project name):
Since it is a Docker image, we should be able to execute the Docker image in the container using the Docker Run command. Now let's try it out:
The discovery did not perform successfully and prompted a bash error: integer expression expected. This application is also not accessible from the browser at this time. After a period of research, found in the Dockerfile.debug file of the last EntryPoint command, will:
| 1 |
ENTRYPOINT ["/bin/bash", "-c", "if [ \"$REMOTE_DEBUGGING\" -eq 0 ]; then dotnet DockerWebAPI.dll; else sleep infinity; fi"] |
Switch
| 1 |
ENTRYPOINT ["/bin/bash", "-c", "if [[ \"$REMOTE_DEBUGGING\" -eq 0 ]]; then dotnet DockerWebAPI.dll; else sleep infinity; fi"] |
(If the clause following the IF uses two double brackets).
At this point, the compilation runs again, the debugging process will not have any problems, again through the command line to execute the newly generated Docker Image, you can see that this error has been fixed:
Actually, this is just a little discovery I'm using vs Tools for Docker, and it's not much of a practical sense:
- In debug mode, if this problem is not fixed, debugger can still start
- In release mode, Dockerfile does not have this instruction at all (since release mode only needs to start the application properly)
Anyway, this experience is also shared, perhaps also can help to have the same doubts friends.
Summarize
This article provides a brief introduction to Visual Studio Tools for Docker. In subsequent articles, I will continue to introduce some of the Docker's experiences and also introduce some of the development experience of the ASP.
Category: Docker,. NET Core
The ASP. NET Core Web API in a Docker container environment