3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

CBL-Mariner Linux の .NET 6 コンテナイメージを使ってみる

Last updated at Posted at 2021-10-06

はじめに

CBL-Mariner Linux は Microsoft が公開している Linux のディストリビューションで、Azure のバックエンドや WSL などで利用されています。DockerHub の.NET のページ には記載がありませんが、.NET 6.0 Previwe7 が出たあたりから、.NET のコンテナのタグに cbl-mariner も登録されるようになっています。ちょっと使ってみましょう。

CBL-Mariner Linux で ASP.NET Core を動かす

CBL-Mariner Linux 用のコンテナイメージは次のタグで公開されています。

  • 3.1-cbl-mariner1.0
  • 5.0-cbl-mariner1.0
  • 6.0-cbl-mariner1.0

例えば .NET 6 の SDK コンテナを起動したいのであれば次のようなコマンドラインでコンテナを起動します。

❯ docker run -it -p 5000:5000 -p 5001:5001 mcr.microsoft.com/dotnet/sdk:6.0-cbl-mariner1.0 bash

とりあえず ASP.NET Core のプロジェクトを作って、現在の環境を書き出すようにします。

$ dotnet new web -o myapp
$ cd /myapp
$ vi Program.cs

ディストリビューションを表示したかったのですが、うまい環境変数が無かったのでPOWERSHELL_DISTRIBUTION_CHANNELを表示しています。

Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => $@"Hello World!
  {Environment.OSVersion},
  {Environment.GetEnvironmentVariable("POWERSHELL_DISTRIBUTION_CHANNEL")},
  {Environment.GetEnvironmentVariable("ASPNET_VERSION")}");

app.Run();

実行してみます。

$ dotnet run

おや?エラーが出ましたね。

Building...
Unhandled exception. System.InvalidOperationException: Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found or is out of date.
To generate a developer certificate run 'dotnet dev-certs https'. To trust the certificate (Windows and macOS only) run 'dotnet dev-certs https --trust'.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.
   at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions, Action`1 configureOptions) in Microsoft.AspNetCore.Server.Kestrel.Core.dll:token 0x60001df+0x4d
   at Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(ListenOptions listenOptions) in Microsoft.AspNetCore.Server.Kestrel.Core.dll:token 0x60001d5+0x1a
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.AddressesStrategy.BindAsync(AddressBindContext context, CancellationToken cancellationToken) in Microsoft.AspNetCore.Server.Kestrel.Core.dll:token 0x6000fa5+0x52
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindAsync(IEnumerable`1 listenOptions, AddressBindContext context, CancellationToken cancellationToken) in Microsoft.AspNetCore.Server.Kestrel.Core.dll:token 0x6000431+0xc7
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.BindAsync(CancellationToken cancellationToken) in Microsoft.AspNetCore.Server.Kestrel.Core.dll:token 0x6000290+0x193
   at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.StartAsync[TContext](IHttpApplication`1 application, CancellationToken cancellationToken) in Microsoft.AspNetCore.Server.Kestrel.Core.dll:token 0x600028d+0x108
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken) in Microsoft.AspNetCore.Hosting.dll:token 0x60000b1+0x23b
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken) in Microsoft.Extensions.Hosting.dll:token 0x6000070+0x177
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) in Microsoft.Extensions.Hosting.Abstractions.dll:token 0x600001e+0x93
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token) in Microsoft.Extensions.Hosting.Abstractions.dll:token 0x600001e+0x1be
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host) in Microsoft.Extensions.Hosting.Abstractions.dll:token 0x600001d+0x15
   at Microsoft.AspNetCore.Builder.WebApplication.Run(String url) in Microsoft.AspNetCore.dll:token 0x6000041+0x7
   at <Program>$.<Main>$(String[] args) in /myapp/Program.cs:line 11

証明書がないということなので、テスト証明書を入れてもう一度実行します。

$ dotnet dev-certs https
The HTTPS developer certificate was generated successfully.
$ dotnet run
Building...
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost: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: /myapp

起動しましたね。
image.png

おわりに

ということで、ASP.NET Core を CBL-Mariner Linux の上で実行してみました。
今のところ物珍しさしかないのですが、提供されている機能が絞られているためコンテナを起動するためのベースイメージとしては適しています。.NET の標準ベースイメージは Debian で提供されていますがそのうち CBL-Mariner で提供される日も来るかもしれませんね。

3
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?