Experience .NET 5 with Docker and Visual Studio Code

Introduction

.NET 5 has been released, but if you don't have LTS and are wondering how to install it, you can try it with Docker and Visual Studio Code. This time, using the [Remote Development](https://marketplace.visualstudio.com/items?itemName=ms-VisualStudio Code-remote.VisualStudio Code-remote-extensionpack) extension of Visual Studio Code, .NET 5 started with Docker Let's check if we can execute / debug the program by connecting to the container of.

Build and run the test app

First, launch the test application container, create an ASP.NET MVC project, and check the operation.

Launching the .NET5 SDK image

In order to save the file created inside Docker on the host OS side, create a directory for volume mounting and start the .NET 5 SDK image. Also, in the ASP.NET MVC project, http is provided on port 5000 and https is provided on port 5001 as standard, so map each port.

mkdir src
docker run -it -p 5000:5000 -p 5001:5001 -v c:¥src:/src mcr.microsoft.com/dotnet/sdk:5.0

Create and run an ASP.NET MVC project

Once you've connected to the container, go to the / src directory you just mounted and create a new MVC project.

cd /src
mkdir WebSite1
cd WebSite1/
dotnet new mvc
The template "ASP.NET Core Web App (Model-View-Controller)" was created successfully.
This template contains technologies from parties other than Microsoft, see https://aka.ms/aspnetcore/5.0-third-party-notices for details.

Processing post-creation actions...
Running 'dotnet restore' on /src/WebSite1/WebSite1.csproj...
  Determining projects to restore...
  Restored /src/WebSite1/WebSite1.csproj (in 67 ms).
Restore succeeded.

When I run the program with dotnet run, I get the following error message saying that localhost: 5000 cannot be bound with IPV6.

root@cc0ff91bfc60:/src/WebSite1# dotnet run                          
Building...
warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
      Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to bind to https://localhost:5001 on the IPv6 loopback interface: 'Cannot assign requested address'.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to bind to http://localhost:5000 on the IPv6 loopback interface: 'Cannot assign requested address'.

You can start it safely by specifying http: //0.0.0.0:5000 with the --urls parameter or http: // *: 5000 with a wildcard.

root@cc0ff91bfc60:/src/WebSite1# dotnet run --urls http://*:5000
Building...
warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
      Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://0.0.0.0:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /src/WebSite1

If you access it with a web browser, you can see that it can be displayed. image.png

Check with Visual Studio Code on the host PC

Connect to the instance in Docker created in the previous section with Visual Studio Code, check the contents of the created project, and check if the debug function can be used.

Install Visual Studio Code extensions

Install the Visual Studio Code Remote Development (https://marketplace.visualstudio.com/items?itemName=ms-VisualStudio Code-remote.VisualStudio Code-remote-extensionpack) extension. image.png

Attach to container

You can connect to a running container by running Remote-Containers: Attach to Running Container ... from the command palette. image.png Open the / src / WebSite1 directory in Visual Studio Code to see the projects in the container. image.png

Run debug

Run .NET: Generate Assets for Build and Debug from the command palette to add the files needed for debugging (lanunch.json and tasks.json) to your project. image.png If you start debugging with F5 or Ctrl + Shift + D after adding the file for debugging, the website will start after the build and the web browser for debugging will start. After that, you can develop using the debug function of Visual Studio Code as you did when developing locally. image.png If you display the environment variables in Index.cshtml, you can see that .NET on the container side is working. image.png

Summary

If you don't want to put it in the development environment yet, you can try it quite easily, so please try it.

reference

-Hello World for those who want to touch C # but don't like putting the .NET SDK in their environment -Easy to try C # 9 (.NET 5) on Docker

Recommended Posts

Experience .NET 5 with Docker and Visual Studio Code
Getting started with Java programs using Visual Studio Code
Try to quit Eclipse and switch to Visual Studio Code
Why can I develop Java with Visual Studio Code?
Build Java program development environment with Visual Studio Code
Docker management with VS Code
Java in Visual Studio Code
Build WebAPP development environment with Java + Spring with Visual Studio Code
Writing code with classes and instances
Hello World with Docker and C
Getting Started with Docker with VS Code
Microservices With Docker and Cloud Performance
How to use PlantUML with Visual Studio Code (created on October 30, 2020)
What I learned from doing Java work with Visual Studio Code
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 1/3 [Preparation]
Spring5 MVC Web App Development with Visual Studio Code Hello World Creation
A record of setting up a Java development environment with Visual Studio Code
Building Rails 6 and PostgreSQL environment with Docker
Spring Boot2 Web application development with Visual Studio Code SQL Server connection
Link Java and C ++ code with SWIG
Spring5 MVC web application development with Visual Studio Code SQL Server connection
Spring Boot2 Web application development with Visual Studio Code Hello World creation
Communicate Gitlab and Gitlab Runner launched with Docker
Spring5 MVC Web application development with Visual Studio Code Maven template creation
[Mac] Install Java in Visual Studio Code
Try remote debugging of Java with Remote Containers in Visual Studio Code Insiders
Use ZStandard with .NET Core + Docker (Alpine)
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 2/3 [Page creation 1/2]
Spring5 MVC Web application development with Visual Studio Code Spring Security usage 3/3 [Page creation 2/2]
Fastest installation of Visual Studio Code on Ubuntu
Add --enable-preview option in Java in Visual Studio Code
Java.home cannot be set in visual studio code.
Create jupyter notebook with Docker and run ruby
Prepare a scraping environment with Docker and Java
[Mac] Install Java in Visual Studio Code (VS Code)
Link Docker log to AWS CloudWatch and monitor in real time with VS Code
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Create a Java (Maven) project with VS Code and develop it on a Docker container
Build Elastic Stack with Docker and analyze IIS logs
Installing Visual Studio Code on Ubuntu 20.04 (automatic update support)
I couldn't type Japanese in Ubuntu20.04 + Visual Studio Code
Output settings to debug console in Visual Studio Code
Access and debug Circle CI Docker container with ssh
Rewrite the code for java.io.File with java.nio.Path and java.nio.Files
Introduce JavaFX 15 and do GUI development with VS Code
Build Java development environment with WSL2 Docker VS Code
Run Mosquitto with Docker and try WebSocket communication with MQTT
Build WordPress environment with Docker (Local) and AWS (Production)
Comfortable Docker environment created with WSL2 CentOS7 and Docker Desktop
BinaryBuilder.jl is amazing ~ Automatically cross-compile with Docker and convert c and Fortran code into Julia library
Workspace setting location when connecting remotely with VS Code and working on a Docker container