.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.
First, launch the test application container, create an ASP.NET MVC project, and check the operation.
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
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.
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 the Visual Studio Code Remote Development (https://marketplace.visualstudio.com/items?itemName=ms-VisualStudio Code-remote.VisualStudio Code-remote-extensionpack) extension.
You can connect to a running container by running Remote-Containers: Attach to Running Container ...
from the command palette.
Open the / src / WebSite1
directory in Visual Studio Code to see the projects in the container.
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.
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.
If you display the environment variables in Index.cshtml, you can see that .NET on the container side is working.
If you don't want to put it in the development environment yet, you can try it quite easily, so please try it.
-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