网上很多文章都是在win10下,用Docker for windows工具进行Docker的安装部署的。用知道windows server 2016已经原生支持Docker了,其windows Container已经和Linux下的内核技术是一致的了,况且最重要的原因就是Docker容器的部署肯定是在Windows server 上面的。所以何不尝个鲜,直接用windows server 2016上装个visual studio 2017来进行开发呢。
一、Windows Server 2016安装Docker
在windows server 2016上面安装Docker十分的简单,不需要像win10一样,装个Dokcer for windows工具,也不需要开启Hyper-V建个Linux虚拟机了。直接在Shell命令下面运行如下命令:
Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
Install-Package -Name docker -ProviderName DockerMsftProvider
Restart-Computer -Force
可以参考 Windows Containers on Windows Server
重启完电脑,运行docker version会弹出如下界面,说明Docker已经安装完成
二、在ASP.NET Web Application项目上启用Docker的支持(注意不是.net core应用)
使用Visual Studio 2017新建一个ASP.NET Web Application项目
Docker for Mac, Docker for Windows, and Docker Toolbox include Docker Compose, so most Mac and Windows users do not need to install Docker Compose separately.
If you are running the Docker daemon and client directly on Microsoft Windows Server 2016 (with Docker EE for Windows Server 2016), you do need to install Docker Compose.
To do this, start an “elevated” PowerShell (run it as administrator). Search for PowerShell, right-click, and choose Run as administrator. When asked if you want to allow this app to make changes to your device, click Yes.
Run the following command to download Docker Compose, replacing $dockerComposeVersion with the specific version of Compose you want to use:
For example, to download Compose version 1.12.0, the command is:
none Invoke-WebRequest "https://github.com/docker/compose/releases/download/1.12.0/docker-compose-Windows-x86_64.exe" -UseBasicParsing -OutFile $Env:ProgramFiles\docker\docker-compose.exe
Now, run the executable to install Compose.
ERROR: client version 1.22 is too old. Minimum supported API version is 1.24, please upgrade your client to a newer version
查了一下,原来如果你使用Docker 1.13以上版本,需要将docker-compose.yml中的version: '2'修改为version: '2.1'也就是这里的version
这个真的是蛮坑的地方,网上也没有相关资料,倒是在stackoverflow发现相关问题:Running Visual Studio Remote Debugger in Windows Container (Docker managed),但是没什么用。最后想了半天,原来是server版的限制比较高,必须在防火墙中开发端口,或者直接关掉防火墙,就OK了。
参考:
BUILD AND RUN YOUR FIRST DOCKER WINDOWS SERVER CONTAINER
Docker的Windows容器初体验
Docker容器环境下ASP.NET Core Web API应用程序的调试