環境
- Windows 10 Pro 20H2 19042.630
- .NET SDK 5.0.100 (x64)
- Visual Studio Code 1.51.1
- Docker Desktop for Windows 2.5.0.1 (49550) / Windows Container
- .NET RuntimeのDockerイメージ
.NET SDK 5.0をホスト環境にインストールする
Download .NETから.NET SDK x64をダウンロードする。

.NET SDK x64のインストーラーを実行する。
dotnet-sdk-5.0.100-win-x64.exe
.NETのバージョンを確認する。
$ dotnet --version 5.0.100
参考情報
- Download .NET / Downloads for .NET Framework and .NET Core, including ASP.NET and ASP.NET Core
- dotnet command - .NET Core CLI | Microsoft Docs
.NET RuntimeのDockerイメージで.NETのバージョンを調べる
.NET RuntimeのDockerイメージを取得する。
Container Base ImageのNano Serverの説明には次のようにある。
Built for .NET Core applications.
Nano ServerのDockerイメージには.NETは含まれていない。
.NETのDockerイメージ > .NET RuntimeのDockerイメージを辿って"Windows Server, version 2009 amd64 Tags"から.NET Runtimeを含むNano ServerのDockerイメージを取得する。
$ cd C:\Temp\dotnet $ docker pull mcr.microsoft.com/dotnet/runtime:5.0.0-nanoserver-2009
イメージのサイズは332.12MBになっている。

Dockerfileを作成する。
C:\Temp\dotnet\DotNetVersions\Dockerfile
FROM mcr.microsoft.com/dotnet/runtime:5.0.0-nanoserver-2009 WORKDIR /app CMD [ "cmd", "/S", "/C", "dir", "C:\\" ]
Dockerイメージをビルドする。
$ cd C:\Temp\dotnet\DotNetVersions $ docker build --tag net-5-nano-2009-amd64:dotnetversions .

Dockerイメージをコンテナとして実行する。"--detach"オプションでバックグラウンド実行にすると出力が表示されない。
$ cd C:\Temp\dotnet\DotNetVersions $ docker run --name Net-5-Nano-2009-amd64-DotNetVersions net-5-nano-2009-amd64:dotnetversions Volume in drive C has no label. Volume Serial Number is 86DC-120F Directory of C:\ 11/27/2020 08:20 PM <DIR> app 11/06/2020 07:09 PM 5,510 License.txt 11/11/2020 03:16 AM <DIR> Program Files 11/06/2020 07:11 PM <DIR> Users 11/11/2020 03:15 AM <DIR> Windows 1 File(s) 5,510 bytes 4 Dir(s) 21,303,574,528 bytes free
.NETのバージョンの確認方法を参考にしてProgram Files配下を確認する。
C:\ +-Program Files +-dotnet |-dotnet.exe +-host | +-fxr | +-5.0.0 | |-hostfxr.dll |-LICENSE.txt +-shared | +-Microsoft.NETCore.App | +-5.0.0 |-ThirdPartyNotices.txt
"dotnet"コマンドが入っているのでそれを使って.NETのバージョンを確認する。
Dockerfileを作成する。
C:\Temp\dotnet\DotNetVersions\Dockerfile
FROM mcr.microsoft.com/dotnet/runtime:5.0.0-nanoserver-2009 WORKDIR /app CMD [ "cmd", "/S", "/C", "dotnet", "--version" ]
Dockerイメージをビルドする。
$ cd C:\Temp\dotnet\DotNetVersions $ docker build --tag net-5-nano-2009-amd64:dotnetversions .
Dockerイメージをコンテナとして実行する。"--detach"オプションでバックグラウンド実行にすると出力が表示されない。
$ cd C:\Temp\dotnet\DotNetVersions $ docker run --name Net-5-Nano-2009-amd64-DotNetVersions net-5-nano-2009-amd64:dotnetversions Could not execute because the application was not found or a compatible .NET SDK is not installed. Possible reasons for this include: * You intended to execute a .NET program: The application '--version' does not exist. * You intended to execute a .NET SDK command: It was not possible to find any installed .NET SDKs. Install a .NET SDK from: https://aka.ms/dotnet-download
C:\Program Files\dotnet\dotnet.exeはSDKのCLIではないのでバージョンの確認はできない。
C:\Program Files\dotnet\shared\Microsoft.NETCore.App\5.0.0がバージョンになる。
参考情報
- Switch between Windows and Linux containers | Docker Desktop for Windows user manual | Docker Documentation
- Get started: Prep Windows for containers | Microsoft Docs
- Windows Container Base Images | Microsoft Docs
- Nano Server | Docker Hub
- Long-Term Servicing Channel (LTSC) | Windows Server Servicing channels | Microsoft Docs
- Semi-Annual Channel | Windows Server Servicing channels | Microsoft Docs
- Sample Dockerfile | Build and run your image | Docker Documentation
- Run your image as a container | Build and run your image | Docker Documentation
- How to check that .NET is already installed | Check installed .NET versions on Windows, Linux and macOS | Microsoft Docs
.NET RuntimeのDockerイメージのフォルダ構造を確認する
C:\ |-License.txt | +-Program Files | +-dotnet | |-dotnet.exe | +-host | | +-fxr | | +-5.0.0 | | |-hostfxr.dll | |-LICENSE.txt | +-shared | | +-Microsoft.NETCore.App | | +-5.0.0 | |-ThirdPartyNotices.txt | +-Users | +-ContainerAdministrator (File Not Found) | +-ContainerUser (空フォルダ) | +-Public (空フォルダ) | +-Windows +-AppCompat +-apppatch +-Branding +-DiagTrack +-Globalization +-INF +-L2Schemas +-LiveKernelReports +-PolicyDefinitions +-ServiceProfiles +-ServiceState +-System32 +-SystemResources +-SysWOW64 +-Temp +-WaaS