Visual Studio 2017 調試 windows server 2016 Docker Container

來源:互聯網
上載者:User

標籤:msf   article   x86_64   logs   dir   shell命令   重要   link   env   

  網上很多文章都是在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 -ForceInstall-Package -Name docker -ProviderName DockerMsftProviderRestart-Computer -Force

可以參考 Windows Containers on Windows Server

  重啟完電腦,運行docker version會彈出如下介面,說明Docker已經安裝完成

二、在ASP.NET Web Application項目上啟用Docker的支援(注意不是.net core應用)

  使用Visual Studio 2017建立一個ASP.NET Web Application項目

在模板上隨便選一個,這裡不像.net core程式會有直接的Enable Docker Support的選項

沒關係,我們可以直接在項目右鍵vs2017-》Add-》Docker Support

就會多出如下檔案

基本上都是docker-compose和Dockerfile構建Docker容器用的。

三、啟用Docker調試

如果這時按F5進行選擇Docker進行調試,會出錯,主要是docker-compose命令未找到。在Docker官網上找到了如下的說法:

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:

Invoke-WebRequest "https://github.com/docker/compose/releases/download/$dockerComposeVersion/docker-compose-Windows-x86_64.exe" -UseBasicParsing 
-OutFile $Env:ProgramFiles\docker\docker-compose.exe

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.

參考: Install Docker Compose,我們先找一下docker-compose的最新版本https://github.com/docker/compose/releases,發現是1.13.0,那麼那句話可以直接改為

Invoke-WebRequest "https://github.com/docker/compose/releases/download/1.13.0/docker-compose-Windows-x86_64.exe" -UseBasicParsing -OutFile $Env:ProgramFiles\docker\docker-compose.exe

滿心期待的去運行F5,結果還是報錯:

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

真的是一波三折,總算是沒問題了,編譯也能通過了。可是當調試F5的時候,卻偏偏又出現遠端偵錯錯誤。。。

這個時候,運行docker ps的命令,可以看到我們的容器已經啟動了

我們查看一下容器IP

docker inspect --format="{{.NetworkSettings.Networks.nat.IPAddress}}" $CONTAINER_ID

然後直接在遊覽器中訪問,已經可以看到網站在運行了。

這個真的是蠻坑的地方,網上也沒有相關資料,倒是在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應用程式的調試

Visual Studio 2017 調試 windows server 2016 Docker Container

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.