What are we trying to do?

What should we have already?

  1. .Net Core 3.1
  2. Visual Studio Code
  3. Docker Desktop for Windows
  4. Basic knowledge about writing code in .Net platform(C#).
  5. Basic knowledge about docker.

How can I start ?

Create the application
  1. Create a folder for the project.
  2. Open command prompt and create a Web API project
    dotnet new webapi
  3. Open the project in vs code
    code .
  4. 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 Dockerfile and .dockerignore files are added to the folder.
12. Add the following environment variable in Dockerfile
ENV ASPNETCORE_URLS=http://*:5000

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
http://localhost:5000/WeatherForecast

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.

  1. Open the launch.json.
  2. 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.

writer@adevtalks.com

By Ajay Bhasy

7 Years of experience in developing applications with .Net (C#). 2 Years of Experience with Angular. Strong advocate of Continuous Integration and Delivery practices with experience in using Bamboo,Octopus Deploy and docker. Engineering professional with a Bachelor’s Degree focused in Information Technology from Dr. NNCE. Recently started learning technologies like MongoDB and Node Js

One thought on “Create a .Net Core API and add Docker support using VS Code”
  1. I followed these steps but in the end I got a message like this:

    ‘\app\controllers\Test.cs’ (Error: Unable to resolve non-existing file ‘app\Controller\Test.cs’)

Leave a Reply

Your email address will not be published. Required fields are marked *