Share feedback
Answers are generated based on the documentation.

.NET アプリケーションのコンテナー化

前提条件

  • Docker Desktop の最新バージョンをインストールしていること。
  • git クライアント が利用可能であること。 本節の利用例ではコマンドラインベースの git クライアントを用いていきますが、別のクライアントを用いてもかまいません。

概要

本節では .NET アプリケーションをコンテナー化して実行する手順を示していきます。

サンプルアプリケーションの入手

このガイドでは、あらかじめビルドされている .NET アプリケーションを利用します。 このアプリケーションは、Docker Blog 記事 Building a Multi-Container .NET App Using Docker Desktop においてビルドしているアプリケーションと同等のものです。 .

端末画面を開いて、作業を行うディレクトリに移動します。 そして以下のコマンドを実行してリポジトリをクローンします。

$ git clone https://github.com/docker/docker-dotnet-sample

Docker アセットの初期化

アプリケーションを入手できたので、必要な Docker アセットを生成してコンテナー化を行います。 方法として 公式 .NET イメージと Docker Hardened イメージ (DHI) のいずれかを用います。

Docker Hardened イメージ (DHI) are minimal, secure, and production-ready container base and application images maintained by Docker. DHI images are recommended for better security—they are designed to reduce vulnerabilities and simplify compliance.

Docker Hardened Images (DHIs) for .NET are available in the Docker Hardened Images catalog. Docker Hardened Images are freely available to everyone with no subscription required. You can pull and use them like any other Docker image after signing in to the DHI registry. For more information, see the DHI quickstart guide.

  1. DHI レジストリにサインインします。

    $ docker login dhi.io
    
  2. .NET SDK DHI をプルします (利用可能なバージョンについてはカタログを確認してください)。

    $ docker pull dhi.io/dotnet:10-sdk
    
  3. ASP.NET コアランタイム DHI をプルします (利用可能なバージョンについてはカタログを確認してください)。

    $ docker pull dhi.io/aspnetcore:10
    

You can use docker init to generate Docker assets, then modify the Dockerfile to use DHI images:

$ docker init
Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml
  - README.Docker.md

Let's get started!

? What application platform does your project use? ASP.NET Core
? What's the name of your solution's main project? myWebApp
? What version of .NET do you want to use? 10.0
? What local port do you want to use to access your server? 8080

In the following Dockerfile, the FROM instructions use dhi.io/dotnet:10-sdk and dhi.io/aspnetcore:10 as the base images.

Dockerfile
# syntax=docker/dockerfile:1

FROM --platform=$BUILDPLATFORM dhi.io/dotnet:10-sdk AS build
ARG TARGETARCH
COPY . /source
WORKDIR /source/src
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
    dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app

FROM dhi.io/aspnetcore:10
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "myWebApp.dll"]
メモ

DHI runtime images already run as a non-root user (nonroot, UID 65532), so there's no need to create a user or specify USER in your Dockerfile. This reduces the attack surface and simplifies your configuration.

You can use docker init to create the necessary Docker assets. Inside the docker-dotnet-sample directory, run the docker init command in a terminal. docker init provides some default configuration, but you'll need to answer a few questions about your application. Refer to the following example to answer the prompts from docker init and use the same answers for your prompts.

$ docker init
Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml
  - README.Docker.md

Let's get started!

? What application platform does your project use? ASP.NET Core
? What's the name of your solution's main project? myWebApp
? What version of .NET do you want to use? 10.0
? What local port do you want to use to access your server? 8080

This generates a Dockerfile using the official .NET 10 images from Microsoft Container Registry:

Dockerfile
# syntax=docker/dockerfile:1

FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:10.0-alpine AS build
ARG TARGETARCH
COPY . /source
WORKDIR /source/src
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
    dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app

FROM mcr.microsoft.com/dotnet/aspnet:10.0-alpine AS final
WORKDIR /app
COPY --from=build /app .
ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser
USER appuser
ENTRYPOINT ["dotnet", "myWebApp.dll"]

上により docker-dotnet-sample ディレクトリ内は以下の内容となるはずです。

├── docker-dotnet-sample/
│ ├── .git/
│ ├── src/
│ ├── .dockerignore
│ ├── compose.yaml
│ ├── Dockerfile
│ ├── README.Docker.md
│ └── README.md

各ファイルについての詳細は以下を参照してください。

アプリケーションの実行

端末画面内の docker-dotnet-sample ディレクトリから以下のコマンドを実行します。

$ docker compose up --build

ブラウザーを開いて http://localhost:8080 にアクセスしてアプリケーションを確認します。 単純なウェブアプリケーションが表示されたはずです。

端末画面から ctrl+c を押下してアプリケーションを停止します。

バックグラウンドでのアプリケーション実行

アプリケーションは端末から切り離して実行することができます。 それには -d オプションをつけます。 端末画面内の docker-dotnet-sample ディレクトリから以下のコマンドを実行します。

$ docker compose up --build -d

ブラウザーを開いて http://localhost:8080 にアクセスしてアプリケーションを確認します。 単純なウェブアプリケーションが表示されたはずです。

端末画面から以下のコマンドを実行してアプリケーションを停止します。

$ docker compose down

Compose コマンドの詳細は Compose CLI リファレンス を参照してください。

まとめ

本節では Docker を使って .NET アプリケーションをコンテナー化して実行する方法について学びました。

関連情報

次のステップ

次の節では、Docker コンテナーを使ってアプリケーションの開発を行う方法を学びます。