Visual studio for Mac が発表されたりして.Net Coreの注目度が(自分の中で)あがって来ました。

てことで、少しいじってみます。

もしかしたらクラウドな環境にデプロイするかもしれないんで、試しにDockerで開発してみます。

環境

諸般の事情でwindows7です。win7なんでDocker Toolboxになります。

Windows 7 SP1

Visual studio 2015 Pro Update3

.NET Core SDK (contains .NET Core 1.0 and 1.1) Visual Studio 2015 Tools

Visual Studio Tools for Docker – Preview これインストールするとdocker-composeとかをビルド対象にして、いい感じでdockerで実行してくれるみたいです。

Docker Toolbox version 1.13.1

プロジェクトの作成

New project -> Visual C# -> .Net Core -> ASP.NET Core Web Application(.NET Core)

今回の名前はCoreWebApp1

TemplateはWeb Applicationを使用。

Projectを右クリックでAdd->Docker support

Docker ホストの構成

ビルドでエラー

こんな感じのエラーが出たら、要ホスト構成です。

1
Microsoft.DotNet.Docker.CommandLineClientException: error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.26/containers/json?filters=%7B%22name%22%3A%7B%22corewebapp1_corewebapp1%22%3Atrue%7D%7D&limit=1: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.

環境構成

こちらを参考にPackage Manager Consoleで環境構成。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#defaultマシンがあるか確認
PM> docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default   -        virtualbox   Running   tcp://192.168.99.100:2376           v1.13.1

#構成用のコマンド確認。コマンドオプションがMSのサイトと若干違います。
PM> docker-machine.exe env default --shell powershell
$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.100:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\??????\.docker\machine\machines\default"
$Env:DOCKER_MACHINE_NAME = "default"
$Env:COMPOSE_CONVERT_WINDOWS_PATHS = "true"
# Run this command to configure your shell:
# & "C:\Program Files\Docker Toolbox\docker-machine.exe" env default --shell powershell | Invoke-Expression

#
# 構成実行!!
#
PM> docker-machine.exe env default --shell powershell | Invoke-Expression

この設定はVS再起動でクリアされてしまうので、毎回実行する必要があるみたいです。

Task runnerとかで上手い事自動化できないもんでしょうか?

ボリュームマッピングの変更

こんな感じのエラーが出たら、docker-compose.dev.debug.ymlを修正します。

1
2
Error	MSB4018	The "PrepareForLaunch" task failed unexpectedly.
System.InvalidOperationException: Unable to validate volume mapping. For troubleshooting, follow instructions from http://aka.ms/DockerToolsTroubleshooting
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
--- a/src/CoreWebApp1/docker-compose.dev.debug.yml
+++ b/src/CoreWebApp1/docker-compose.dev.debug.yml
@@ -4,14 +4,13 @@ services:
   corewebapp1:
     build:
       args:
-        source: obj/Docker/empty/
+        source: .
     labels:
       - "com.microsoft.visualstudio.targetoperatingsystem=linux"
     environment:
       - ASPNETCORE_ENVIRONMENT=Development
       - DOTNET_USE_POLLING_FILE_WATCHER=1
     volumes:
-      - .:/app
       - ~/.nuget/packages:/root/.nuget/packages:ro
       - ~/clrdbg:/clrdbg:ro
     entrypoint: tail -f /dev/null

上手くいくとDockerコンテナができます。

1
2
3
4
PM> docker ps
CONTAINER ID        IMAGE                  COMMAND               CREATED             STATUS              PORTS                   NAMES
1c591fcc3508        user/corewebapp1:dev   "tail -f /dev/null"   3 minutes ago       Up 3 minutes        0.0.0.0:32769->80/tcp   corewebapp1_corewebapp1_1
PM>

Docker outputに

Launching http://localhost:32769/

みたいなのが出力されますが、localhostではだめで、DOCKER_HOSTのipにアクセスします。

手元の環境ではhttp://192.168.99.100:????/でした。(docker for windowsならlocalhostでも行ける?)

Properties/launchSettings.jsonを編集しときます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
--- a/src/CoreWebApp1/Properties/launchSettings.json
+++ b/src/CoreWebApp1/Properties/launchSettings.json
@@ -24,8 +24,8 @@
       }
     },
     "Docker": {
-      "launchBrowser": true,
-      "launchUrl": "http://localhost:{ServicePort}"
+        "launchBrowser": true,
+        "launchUrl": "http://192.168.99.100:{ServicePort}"
     }
   }
 }

デバッグ

ブレークポイントもちゃんと動作してくれます。

変なWatchも動いてます。

すごいね。

その他エラー

1
Error response from daemon: Container xxxxxxxxxxxxx is not running

コンテナを削除しちゃうのが手っ取り早いと思われます。

1
2
PM>docker stop xxxxxxxxxxxxx
PM>docker rm xxxxxxxxxxxxx

VS2015はオワコン@.Net Core

えーー

プレビュー バージョンの .NET Core ツールで Visual Studio 2015 を使用できる場合でも、これらのプロジェクトは project.json ベースであるため、推奨されなくなりました。 Visual Studio 2017 では、MSBuild に基づくプロジェクト ファイルが使用されます。 この形式変更の詳細については、変更点の大まかな概要に関するページをご覧ください。