1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.NET のプロジェクトを作成し DockerImage を作成する

Posted at

概要

Microsoftのチュートリアルを参考に.NETのWEB APIプロジェクトの作成からDocerImageを作成する

ハンズオン内容

minimal WEB API プロジェクトの作成
dotnet による image の発行
DockerFile による Image の発行

前提

.NET 8.0
VsCode
Docker
DockerDesktop

minimal WEB API を作成する

  • プロジェクトの作成
dotnet new web -o SampleStore -f net8.0
  • アプリの確認
dotnet run

ビルドが成功しURLを叩くと"Hello World"が表示されることを確認する

DockerFile から DockerImage の作成

  • Dockerfile をrootに作成
# ベースイメージとして .NET SDK を使用してビルドステージを定義
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build

# 作業ディレクトリを設定
WORKDIR /app

# プロジェクトファイルをコピーして依存関係を復元
COPY SampleStore.csproj .
RUN dotnet restore

# ソースコードをコピーしてリリースビルドを実行
COPY . .
RUN dotnet publish -c Release -o /out

# 実行用のベースイメージとして ASP.NET Core ランタイムを使用
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /out .

# ポートを公開
EXPOSE 80

# アプリケーションを実行
ENTRYPOINT ["dotnet", "SampleStore.dll"]
  • Dockerをビルドしてimageの作成
docker build ./ -t samplestore:latest
  • image の確認
docker image ls
  • コンテナの起動
docker run -it --rm -p 32001:8080 samplestore

dotnet publish から DockerImage を作成する

  • 以下コマンドにより image を作成する
    dotnet publish /p:PublishProfile=DefaultContainer
  • image の確認
    docker image ls
  • コンテナの起動
    docker run -it --rm -p 32001:8080 pizzastore
  • API の確認
    http://localhost:32001/

エラーメモ

  • DockerfileをDockerFileと作成していてビルド時にエラーがでた

参考

minimal API、ASP.NET Core、.NET を使用して Web API をビルドする
https://learn.microsoft.com/ja-jp/training/modules/build-web-api-minimal-api/
.NET を使用して最初のマイクロサービスをビルドする
https://learn.microsoft.com/ja-jp/training/modules/dotnet-microservices/

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?