LoginSignup
38
32

More than 3 years have passed since last update.

Docker で ASP.NET Core ウェブアプリケーションを動かす

Last updated at Posted at 2018-09-10

はじめに

Docker 上で ASP.NET Core を動かすサンプルです。
Microsoft Docs のドキュメントが若干古くそのまま動かなかったため、記録を残します。
また、ドキュメントではコンソールアプリでしたが、ウェブアプリケーションでやることにしました。

動作環境

  • 実施日 2018年9月10日
  • mac OS High Sierra 10.13.6
  • .NET Core SDK 2.1.302
  • Docker CE 18.06.1
# バージョン確認
$ dotnet --version
2.1.302
$ docker --version
Docker version 18.06.1-ce, build e68fc7a

 ASP.NET Core ウェブアプリケーションを Docker で動かす

最初にASP.NET Core Web アプリ (モデル ビュー コントローラー)を作成してそのまま動かしてみます。

$ dotnet new mvc -o netcore_on_docker
$ cd netcore_on_docker
$ dotnet run
# 略
Now listening on: https://localhost:5001
# 略

localhost:5001でサーバーが受付を開始するのでアクセスします。
image.png
※ サーバーをシャットダウンするにはcontroll + cを押します。

次に、Docker で動かす環境を構築します。

プロジェクトのルートフォルダ(netcore_on_docker直下)にDockerfileを作成します。

Dockerfile
# .NET Core 2.1 をベースイメージとする
FROM microsoft/dotnet:2.1-sdk
# 作業ディレクリの指定
WORKDIR /app

# プロジェクトをコピーして別のレイヤーで復元します。
COPY *.csproj ./
RUN dotnet restore

# 全てコピーしてビルドします。
COPY . ./
RUN dotnet publish -c Release -o out

ENTRYPOINT ["dotnet", "out/netcore_on_docker.dll"]

Docker でアプリをビルドします。

# tag を指定してビルド
$ docker build -t dotnetapp-dev .
Sending build context to Docker daemon  4.038MB
Step 1/7 : FROM microsoft/dotnet:2.1-sdk
 ---> bde01d9ed6eb
Step 2/7 : WORKDIR /app
 ---> Using cache
 ---> 887885acec50
Step 3/7 : COPY *.csproj ./
 ---> 81314f7df261
Step 4/7 : RUN dotnet restore
 ---> Running in 85e8a39221d5
  Restoring packages for /app/netcore_on_docker.csproj...
  Generating MSBuild file /app/obj/netcore_on_docker.csproj.nuget.g.props.
  Generating MSBuild file /app/obj/netcore_on_docker.csproj.nuget.g.targets.
  Restore completed in 607.72 ms for /app/netcore_on_docker.csproj.
Removing intermediate container 85e8a39221d5
 ---> 1c6fc982580c
Step 5/7 : COPY . ./
 ---> 8a27bad2f52c
Step 6/7 : RUN dotnet publish -c Release -o out
 ---> Running in ea299f67388d
Microsoft (R) Build Engine version 15.8.166+gd4e8d81a88 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restoring packages for /app/netcore_on_docker.csproj...
  Generating MSBuild file /app/obj/netcore_on_docker.csproj.nuget.g.props.
  Generating MSBuild file /app/obj/netcore_on_docker.csproj.nuget.g.targets.
  Restore completed in 532.05 ms for /app/netcore_on_docker.csproj.
  netcore_on_docker -> /app/bin/Release/netcoreapp2.1/netcore_on_docker.dll
  netcore_on_docker -> /app/bin/Release/netcoreapp2.1/netcore_on_docker.Views.dll
  netcore_on_docker -> /app/out/
Removing intermediate container ea299f67388d
 ---> b6e6f0acc805
Step 7/7 : ENTRYPOINT ["dotnet", "out/netcore_on_docker.dll"]
 ---> Running in 26cad4f1f322
Removing intermediate container 26cad4f1f322
 ---> bbd7848d8e16
Successfully built bbd7848d8e16
Successfully tagged dotnetapp-dev:latest

ホスト名をつけて実行します。

# コンテナのポート 80 をホストのポート 5000 に割り当てて実行します。
$ docker run --rm -p 5000:80 dotnetapp-dev netcore_on_docker from Docker
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {34f4db44-299b-4d63-9eac-449703dacb6b} may be persisted to storage in unencrypted form.
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

http://localhost:5000にアクセスすると Docker ASP.NET Core ウェブアプリにアクセスできます。
image.png

--rmをつけて実行したのでサーバーをシャットダウン後にコンテナは削除されます。

$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
38
32
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
38
32