# 從0到100 docker Windows Container 架設IIS MSSQL Https憑證 Http轉址Https ###### tags: `火箭` # 下載docker desktop https://www.docker.com/products/docker-desktop/ ![](https://i.imgur.com/CqxYqFr.png) # 安裝 ![](https://i.imgur.com/FxpXA5t.png) ![](https://i.imgur.com/8at8TK5.png) # 啟動 ![](https://i.imgur.com/EaESV6S.png) 發現有錯誤 要下載 Linux 核心更新套件 WSL2 Linux 核心更新套件 (適用於 x64 電腦) https://learn.microsoft.com/zh-tw/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package # 再次啟動 ![](https://i.imgur.com/0LtVAVW.png) # 切換到windows容器 ![](https://i.imgur.com/9v9wFls.png) ![](https://i.imgur.com/NZF9N2e.png) ![](https://i.imgur.com/Vo0BKH3.png) 啟動虛擬化環境 才能切換過去 # 安裝 iis ``` FROM mcr.microsoft.com/windows/servercore/iis SHELL [ "powershell" ] #setup Remote IIS management RUN Install-WindowsFeature Web-Mgmt-Service; \ New-ItemProperty -Path HKLM:\software\microsoft\WebManagement\Server -Name EnableRemoteManagement -Value 1 -Force; \ Set-Service -Name wmsvc -StartupType automatic; #Add user for Remote IIS Manager Login RUN net user iisadmin Password~1234 /ADD; \ net localgroup administrators iisadmin /add; ``` 把上面這個dockerfile放到資料夾裡 powershell cd進去 之後輸入指令 ``` docker build -t iisserver . --no-cache ``` -t 代表tag isserver 這個tag的名字 . 代表用這個資料夾底下的dockerfile ![](https://i.imgur.com/qifjutz.png) 等待下載 完成後 執行 ``` docker run -d --name iis_server -p 80:80 -p 443:443 -v C:\dockervolumes:C:\dockervolumes iisserver ``` name 代表container 名字 -p 是port號 80 http 443 https -v 是volumes設定 iisserver 代表剛剛建的image ![](https://i.imgur.com/MytZxoC.png) cd到 volumes位置 ``` xcopy /E . C:\inetpub\wwwroot\ ``` /E 代表全檔案 . 代表目前位置 放到C:\inetpub\wwwroot\ ![](https://i.imgur.com/cTy5WHi.png) 完成後 測試 錯誤 ![](https://i.imgur.com/jF5cjqO.png) 先去把錯誤完整訊息打開 這裡要用到 Microsoft SQL Server Management Studio 18 # 安裝iis主控台 方便管理站台用 ![](https://i.imgur.com/RdGaItw.png) ![](https://i.imgur.com/FojkUhq.png) https://www.iis.net/downloads/microsoft/iis-manager 下載這個更新 才能連線到非localhost的站台 ![](https://i.imgur.com/FJ0nSDX.png) ![](https://i.imgur.com/9oyp0dL.png) 先到docker 裡面 找到ip位置 ![](https://i.imgur.com/Yamj1Nt.png) ![](https://i.imgur.com/giRXHFh.png) ![](https://i.imgur.com/by7534V.png) 這個帳號密碼是當初在建image的時候設定的 在dockerfile裡面 ![](https://i.imgur.com/2Psx5vr.png) ![](https://i.imgur.com/rG5WVwB.png) ![](https://i.imgur.com/SzS9avD.png) ![](https://i.imgur.com/IpopNka.png) 設定編輯器 ![](https://i.imgur.com/IUaeWYA.png) 到webserver httperror 把errormode改成detail ![](https://i.imgur.com/b8Ofz9B.png) 再重新整理頁面 知道更詳細的錯誤訊息 ![](https://i.imgur.com/LjwopIn.png) 這個錯誤 在 https://stackoverflow.com/questions/9794985/config-error-this-configuration-section-cannot-be-used-at-this-path 說明 To continue this, what worked for me is to (note this solution is on my DEV box and NOT a Production system): -> Sort by Delegation -> Find all of the Read Only types -> Set them to Read/Write – 把所有讀取 都改 讀寫 ![](https://i.imgur.com/Xj9YUj2.png) 到功能委派 ![](https://i.imgur.com/WcWDXx2.png) 改成讀寫 回去重整頁面 ![](https://i.imgur.com/clOiTOE.png) 在 https://stackoverflow.com/questions/13162545/handler-extensionlessurlhandler-integrated-4-0-has-a-bad-module-managedpipeli 說明 在DOCKER 打以下指令 ``` dism /online /enable-feature /featurename:IIS-ASPNET45 /all ``` ![](https://i.imgur.com/jHySCgV.png) 完成後 再重整 ![](https://i.imgur.com/adHX1Vu.png) 跑起來了 # 安裝 mssql ``` docker run -d -p 1433:1433 -e sa_password=!Passw0rd -e ACCEPT_EULA=Y kkbruce/mssql-server-windows-express:windowsservercore-1903 ``` ![](https://i.imgur.com/RFTCqMI.png) 等待下載 完成後 可以透過重新命名 ``` docker rename 本來的containername 要改的name ``` ![](https://i.imgur.com/dTGKbJL.png) 測試連線 應該要可以連得進去 # 安裝 Microsoft SQL Server Management Studio 18 用來查看資料庫 https://drive.google.com/file/d/1zPidGpsLtUdChvZcFgNcpLD_-qvZA1Jx/view # 以下這段是我自己的步驟 如果是新資料庫就不用了 還原資料庫 ![](https://i.imgur.com/sCa9JtC.png) ![](https://i.imgur.com/ZRWasIh.png) 測試 print隨便一筆資料庫資料也正常 # MSSQL 從c#連不上去的原因 因為webconfig裡的 連線字串 寫的ip 本來是 localhost 但是在docker 裡 iis 跟 mssql是不同container 所以應該要填的是mssql的區域ip 最後我是填data source=mssql,1433; 這是那個container 的名字 這樣也可對應的到 ``` <add name="Model1_m" connectionString="data source=mssql,1433;initial catalog=pet;user id=sa;password=!Passw0rd;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> ``` # 處理 浮動ip 安裝noip ![](https://i.imgur.com/26SdQZS.png) ![](https://i.imgur.com/jqLFNOU.png) 選擇已經建好的 hostname 小烏龜記得把80 443port 加到虛擬伺服器 ![](https://i.imgur.com/yxlFJRk.png) # 處理https 憑證 [憑證差別](https://hackmd.io/pxyccUn9RzmpJbGNUyl3OQ) 這段會出現有問題的地方和如何申請憑證 我有另外寫在 [文章](https://hackmd.io/Ewaxour8QjWcU8ziYszWEA#https-%E6%86%91%E8%AD%89-%E8%A8%AD%E5%AE%9A) 到docker iis container啟動 powershell 匯入pfx 憑證檔案 ``` $PfxPW = ConvertTo-SecureString -String "1234" -Force -AsPlainText Import-PfxCertificate -FilePath C:\certificate_combined.pfx -CertStoreLocation cert:\LocalMachine\My -Password $PfxPW -------- $PfxPW = ConvertTo-SecureString -String "1234" -Force -AsPlainText Import-PfxCertificate -FilePath C:\dockervolumes\rootCA.pfx -CertStoreLocation cert:\Loca Machine\My -Password $PfxPW ``` ![](https://i.imgur.com/IbDEQgz.png) 完成後 ![](https://i.imgur.com/sggwOS1.png) 回到繫結 就有憑證可以選了 # http導向httpS 參考 https://dotblogs.com.tw/yc421206/2020/10/14/iis_http_redirect_https 安裝url rewrite 步驟在 5.Install URL Rewrite inside the container https://github.com/fabioharams/container 我這裡採用的是 我先載下來檔案 放到volume在用docker 進container安裝 下載 https://www.iis.net/downloads/microsoft/url-rewrite 丟進volumes 在docker iis 用以下指令安裝 特別產生了package 看到底有沒有裝成功 ``` msiexec.exe /i "C:\dockervolumes\rewrite_amd64_en-US.msi" /L*V package.log ``` 之後重啟 重新連線 IIS管理員 ![](https://i.imgur.com/qLZqZRw.png) ![](https://i.imgur.com/S9GMly9.png) 之後寫規則 ![](https://i.imgur.com/wo6qOMj.png) 依序輸入下列資訊: 樣式 (.*) 輸入 {HTTPS} 模式 ^OFF$ 重新導向 https://{HTTP_HOST}{REQUEST_URI} CONFIG也要加 ``` <system.webServer> <rewrite> <rules> <rule name="HTTP to HTTPS redirect" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="off" ignoreCase="true" /> </conditions> <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> ``` 重啟後 HTTP都會轉成HTTPS # 紀錄一下 msiexec 這個功能 用來在DOCKER 安裝MSI檔案 ``` Windows Installer handles its installations through Msiexec.exe. The logging options offered by this tool allow you to create different types of logs, depending on the information you need about the installation. These options are: i - Status messages w - Nonfatal warnings e - All error messages a - Start up of actions r - Action-specific records u - User requests c - Initial UI parameters m - Out-of-memory or fatal exit information o - Out-of-disk-space messages p - Terminal properties v - Verbose output x - Extra debugging information + - Append to existing log file ! - Flush each line to the log * - Log all information, except for v and x options ``` # 這裡可能有一些遇到的錯誤debug 可以看 https://hackmd.io/Ewaxour8QjWcU8ziYszWEA # DOCKER一些觀念 DOCKER pull => docker hub => docker image docker build => from dockerfile => docker image docker run => from docker image => container docker commit docker run -d --name iis_server -p 80:80 -p 443:443 -v C:\dockervolumes:C:\dockervolumes iis_server:0324 powershell ```= 取得所有安裝角色服務或功能 Get-WindowsFeature 安裝WebSockets dism.exe /online /enable-feature /featurename:IIS-WebSockets ``` cd inetpub\wwwroot rmdir css\ /s/q; rmdir js\ /s/q; rmdir img\ /s/q; cd C:\dockervolumes\dist\ xcopy /E . C:\inetpub\wwwroot\ cd c:\dockervolumes\hofix xcopy /E . C:\inetpub\wwwroot\bin\ https://ccore.newebpay.com/MPG/mpg_gateway docker ps -a 看離線狀態的所有CONTAINER docker commit id 要取的新IMAGE名字 docker commit cb871626872f iis_server0530 docker images 所有iMAGEs docker save -o 名字 和image id docker save -o mssql0530.tar 02f71237a04c