LoginSignup
2
1

More than 1 year has passed since last update.

.NET 6 PreviewをDocker上で動かしてみました。

Last updated at Posted at 2021-07-27

はじめに

Dockerの勉強として、.NET 6 PreviewをDocker上で動かしてみました。
Preview版のSDKをローカルにインストールするのは悩むなぁという方は参考になるかもしれません。

以下の記事がとても参考になりました。ありがとうございます!!

Dockerコンテナ内でMVCプロジェクトを立てる!

.NET 6 Previewのイメージを以下から取得し、コンテナを立ち上げます。
https://hub.docker.com/_/microsoft-dotnet-nightly-sdk/

※今回はUbuntu環境のイメージを利用しました。

C:\Work\Docker_.Net6Preview>docker run -it -p 5000:5000 -v c:\Work\Docker_.Net6Preview:/src mcr.microsoft.com/dotnet/sdk:6.0-focal

※ポートは5000、ローカル環境のc:\Work\Docker_.Net6Previewフォルダをコンテナ内のsrcフォルダへマウントしています。

コンテナを立ち上げた後はsrcフォルダ内で以下のようにMVCプロジェクトを立ち上げてみます。

root@a5d31eefeda7:/# cd src
root@a5d31eefeda7:/src# ls
root@a5d31eefeda7:/src# mkdir WebSite1
root@a5d31eefeda7:/src# cd WebSite1/
root@a5d31eefeda7:/src/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/6.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 69 ms).
Restore succeeded.

Restore succeededと出たので問題なく出来上がったようです。
dotnet runコマンドでMVCプロジェクトを実行してみましょう。
--urlsでURLとポートの形式を指定します。

root@a5d31eefeda7:/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[14]
      Now listening on: http://[::]: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
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
      Failed to determine the https port for redirect.

localhost:5000へアクセスしてみると・・・
home1.png

お!しっかり表示されていますね!(^^)!
良い感じです。

コンテナを立ち上げる際にローカルフォルダへマウントしていますので、対象のフォルダからタイトルを変えてみましょう。
タイトルを「Welcome」→「Welcome変更しました」としてみました。
home2.png

しっかり変わっていますね!

.NET 6 Preview 6で追加されたコマンドも試してみる

現時点(この記事を書いた2021年7月27日)で最新のバージョンとなっている.NET 6 Preview 6について、以下の記事で追加されたコマンドがあるようですのでその中の1つである「dotnet workload search」というコマンドを打ってみました。

dotnet workload searchコマンドはインストール可能なワークロードを表示します。
以下のようにインストール可能なワークロードがいくつか出ていることが分かります。
(どうやらPreview6にも対応してるっぽいですね。)

root@a5d31eefeda7:/src/WebSite1# dotnet workload search

Workload ID                                  Description
-----------------------------------------------------------------------------------------
microsoft-net-sdk-blazorwebassembly-aot      Browser Runtime native performance tools
microsoft-net-runtime-android                Android Mono Runtime
microsoft-net-runtime-android-aot            Android Mono AOT Workload
microsoft-net-runtime-ios                    iOS Mono Runtime and AOT Workload
microsoft-net-runtime-maccatalyst            MacCatalyst Mono Runtime and AOT Workload
microsoft-net-runtime-tvos                   tvOS Mono Runtime and AOT Workload
microsoft-net-runtime-mono-tooling           Shared native build tooling for Mono runtime
microsoft-android-sdk-full                   Android SDK
microsoft-maccatalyst-sdk-full               Microsoft MacCatalyst SDK
maui                                         .NET MAUI SDK for all platforms
maui-mobile                                  .NET MAUI SDK for Mobile
maui-desktop                                 .NET MAUI SDK for Desktop
maui-core                                    .NET MAUI SDK Core Packages
maui-android                                 .NET MAUI SDK for Android
maui-maccatalyst                             .NET MAUI SDK for Mac Catalyst
maui-ios                                     .NET MAUI SDK for iOS
maui-windows                                 .NET MAUI SDK for Windows
microsoft-macos-sdk-full                     Microsoft macOS SDK
microsoft-ios-sdk-full                       Microsoft iOS SDK
microsoft-tvos-sdk-full                      Microsoft tvOS SDK

おわりに

Dockerはこんな感じで簡単に環境が作れてしまうのでとても良いですね!(イメージが用意されてればではありますが・・・)
今まではローカルの方にインストールしまくっていたので今後は積極的に利用していかねば!

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1