What are we trying to do?
- Create a .Net Core API
- Add docker files to the application
- Build the image
- Test the container
- Debug in container with VS Code
What should we have already?
- .Net Core 3.1
- Visual Studio Code
- Docker Desktop for Windows
- Basic knowledge about writing code in .Net platform(C#).
- Basic knowledge about docker.
How can I start ?
Create the application
- Create a folder for the project.
- Open command prompt and create a Web API project
dotnet new webapi
- Open the project in vs code
- When prompted to add assets for build and debug select Yes.
This can also be done by opening Command Palette (
Ctrl+Shift+P) and selecting .NET: Generate Assets for Build and Debug command.
Add Docker files
5. Install the docker extension in VS Code.
6. Now access the Command Palette with (Ctrl+Shift+P) and select Docker: Add Docker Files to Workspace… command
7. Select the ASP.NET Core option
8. Select the OS (Only select Windows if the docker installation is configured to use Windows containers)
9. Include Docker optional files? “YES”
10. Set the endpoint port to 5000
11. Now you can see that
.dockerignore files are added to the folder.
12. Add the following environment variable in Dockerfile
Build the Image
1. Open Command Palette (Ctrl+Shift+P) and issue Docker Images: Build Image… command.
2. Open the docker plugin view and verify that our image has been created (Images section in the docker sidebar)
Test the container
1. Right click on the newly generated image and select run. This will start the container and you will be able to see the container in the containers pane.
I got an error like this
docker: Error response from daemon: mkdir C:\Program Files\dotnet\sdk\NuGetFallbackFolder: Access is denied.
What I did was create a folder NuGetFallbackFolder in the specified directory manually.
2. Access the following link to test if your application is working
Debug in container with Vs Code
Being able to debug is really important for us as a developer. So lets try to do that with the brand new container that we setup.
- Open the launch.json.
- Click on Add configuration
3. Add the .NET Core Attach (Preview) option. When done a configuration will be added to launch.json .
4. Add a break point in the Get method of WeatherForecastController
5. Start debugging using F5. But we need to make sure that the selected configuration is .NET Core Attach (Preview).
6. Select the container as shown below
7. Access the application link http://localhost:5000/WeatherForecast. The break point will be hit.
Tadaaa! The breakpoint is hit. Happy debugging!!!
Feel free to post your thoughts as comments or email@example.com