C# .NET dockerで環境構築
普段はWindowsを使ってますが、Mac mini環境でもC#をさわってみたくなりました。
最初から環境を作成するのはとても大変なため、dockerで環境構築してみることに。
前提条件
- DockerHubのアカウント登録後、Docker for Mac を install
- install 確認コマンド
docker version
構成
最終的にsampleフォルダ内にプロジェクトが作成されます
. ├── Dockerfile ├── docker-compose.yml └── sample
必要なファイル
- docker-compose.yml
- Dockerfile
docker-compose.yml
version: '3' services: web: build: . ports: - 5000:5000 tty: true volumes: - ./sample:/sample
- コンテナを起動させ続ける
tty: true
- データの永続化
- ./sample:/sample
localのsampleフォルダ内にコンテナのsampleフォルダをマウントさせる
これをすることで、コンテナを削除してもデータを保存しておくことができる
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 RUN mkdir /sample WORKDIR /sample
- 公式のimageを使用
FROM mcr.microsoft.com/dotnet/core/sdk:3.1
手順
- image作成
docker-compose build
- コンテナを作成 起動
docker-compose up -d
ここでlocalにsampleフォルダが作成されます
- コンテナ内に入る
docker-compose exec web bash
- プロジェクト作成
dotnet new mvc
- 起動
dotnet run
エラー
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.DataProtection.KeyManagement.XmlKeyManager[35] No XML encryptor configured. Key {36020275-9c58-4ee1-bc60-9c7050c13fa7} may be persisted to storage in unencrypted form. crit: Microsoft.AspNetCore.Server.Kestrel[0] Unable to start Kestrel. 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'.
コンテナからアクセスできるようにエンドポイントの設定をする
- appsettings.Development.jsonに設定を追加
"urls": "http://*:5000;"
http://localhost:5000にアクセスして確認
参考サイト
qiita.com
qiita.com
mseeeen.msen.jp