一、首先去https://www.docker.com/products/docker-desktop下載Windows版本的Docker Desktop並安裝(需要win10專業版以上操作系統,並啟用CPU虛擬化和安裝Hvper-V)。
添加國內鏡像源:
{ "registry-mirrors": [ "https://reg-mirror.qiniu.com", "http://hub-mirror.c.163.com", "https://registry.docker-cn.com" ], "insecure-registries": [], "debug": true, "experimental": false }
有時候第一次安裝完用VS調試的時候會下載一些數據包,會比較慢,類似於提示:
1>Info: Using vsdbg version '16.6.20415.1' 1>Info: Using Runtime ID 'linux-x64' 1>Info: C:\Users\UserName\vsdbg\vs2017u5 exists, deleting.
老半天不動,這時候去:https://download.csdn.net/download/5653325/12825714
下載離線包,解壓縮vs2017u5目錄中的內容到C:\Users\UserName\vsdbg\vs2017u5文件夾中根目錄中,然后重新啟動VS
二、新建一個.NetCore3.1的API項目,在創建的時候選中啟用Docker支持。
三、寫一個簡單的響應輸出
public IActionResult Hello() { return Content(JsonConvert.SerializeObject(new ContentModal{ Status = 1, Code = 200, Message = $"請求成功=>{Request.HttpContext.Connection.LocalIpAddress}:{Request.HttpContext.Connection.LocalPort}", Content = "ok", }), "application/json", Encoding.UTF8); }
四、項目中的Dockerfile文件按以下內容完善(多余的可以刪掉)
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base WORKDIR /app EXPOSE 8001 EXPOSE 443 COPY . . ENV ASPNETCORE_URLS http://+:8001 ENTRYPOINT ["dotnet", "weixinKey.dll"]
並且右鍵此DockerFile屬性,如果較新則復制。
使用Release模式生成項目,管理員運行cmd,定位到當前項目的Release目錄下的netcoreapp3.1目錄(我用的是.net core3.1版本,如果是其它版本,這個目錄名會不同)
然后運行docker build命令打包成docker包
第一次打包的時候會下載所依賴的環境。
五、打包完成后運行Power Shell(管理員模式),然后運行 docker images即可看到docker包已經推送到Docker Desktop中了
六、依次啟動多個此docker的實例,本次測試啟用了四個實例。(加上-restart=on-failure:3 可以在docker重啟后自動啟動實例(重試3次))
七、查看Docker Desktop中的實例情況
八、下載Nginx的最新版本(http://nginx.org/download/nginx-1.18.0.zip),解壓后編輯conf目錄下的nginx.conf,添加以下內容啟用負載均衡。
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; upstream wxapi{ server 10.2.18.244:8887 weight=1; server 10.2.18.244:8888 weight=1; server 10.2.18.244:8889 weight=1; server 10.2.18.244:8890 weight=1; } # down 此標記的服務器不參與負載. # weight 值越大,權重就越大,越能多次響應請求 # backup 其它服務器無法響應是會請求此種類型的服務器應急。 server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; proxy_pass http://wxapi; #proxy_redirect default; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
九、啟動Nginx(雙擊nginx.exe或者CMD下運行)
十、訪問測試
Docker中會對每個實例虛擬一個IP地址,至此,我們的API成功運行在Docker中並通過Nginx實現了負載均衡。
十一、Docker中導出、導入鏡像文件命令
導出:docker save -o 要導出的文件.tar image名:tag名
導入:docker load -i 導出的文件.tar
重寫打包運行中的容器內容:docker commit -a="wdw" -m="apt-get updated and installed libgdiplus" 17a83c47ffeb whitelist:1.1
十二、DockerDesktop中如果使用驗證碼,在沒有安裝libgdiplus的情況下會報錯,需要執行一下命令
apt-get update apt-get install libgdiplus ln -s /lib/x86_64-linux-gnu/libdl.so.2 /lib/x86_64-linux-gnu/libdl.so ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll
第一個ln解決:The type initializer for 'Gdip' threw an exception的問題
第二個ln解決:Unable to load DLL 'libgdiplus'的問題