問題描述
通過Docker Desktop for Linux,配置Nginx鏡像后,自定義nginx.conf文件,修改啟動目錄和對 /out 路徑的反向代理到博客園的博文地址 (https://www.cnblogs.com/lulight/p/15180884.html), 然后部署到Azure App Service中的整體實現方案。
操作步驟(共5步)
第 0 步:啟動本地 Docker Desktop,並拉取Nginx 鏡像
# 1. pull nginx image ... need docker for linux docker pull nignx
注意:必須切換為 Linux Container,避免在拉去 Nginx 鏡像時候出現如下錯誤:
C:\Users\bu>docker pull nginx Using default tag: latest latest: Pulling from library/nginx no matching manifest for windows/amd64 10.0.19043 in the manifest list entries
第一步:創建Dockerfile 文件
FROM nginx COPY appnginx.html /home/site/wwwroot/index.html COPY . /home/site/wwwroot COPY nginx.conf /etc/nginx/nginx.conf RUN .
注意:
- 這里Dockerfile的名字必須為Dockerfile
- 第一行 FROM nginx 表示這次構建的image是以nginx的鏡像為基礎
- 第二行 表示把本地目錄中的一個appnginx.html靜態文件復制到 /home/site/wwwroot/下的index.html文件中
- 第三行 表示把本地當前與Dockerfile同級目錄中的所有內容都復制到 /home/site/wwwroot 中
- 第四行 表示把自定義的nginx.conf文件復制到linux下的nginx的安裝目錄中 /etc/nginx/nginx.conf,代替默認的nginx.conf
第二步:定義nginx.conf文件,其中包含修改啟動路徑,配置方向代理路徑
worker_processes 1; events{ worker_connections 1024; } http{ include mime.types; default_type application/cotet-stream; sendfile on; keepalive_timeout 65; server { listen 80 default; server_name localhost; access_log /var/log/nginx/localhost.access.log; location / { root /home/site/wwwroot/; index index.html index.htm; } location /out { proxy_pass https://www.cnblogs.com/lulight/p/15180884.html; } } }
注意:
- 在Server節點中,配置了兩種路徑處理,當訪問的是 / 根目錄時,路徑修改為 /home/site/wwwroot/, 默認的啟動頁面時 index.html 或者時 index.htm
- 當請求路徑時 localhost:80/out 時,反向代理請求發送到博客園博文地址 https://www.cnblogs.com/lulight/p/15180884.html
第三步:創建鏡像后,Push到ACR中
使用az指令來創建ACR並通過docker登錄到ACR中,然后push mynginx鏡像到ACR中,為第四步准備。全文參考文檔:https://docs.microsoft.com/zh-cn/azure/app-service/tutorial-custom-container?pivots=container-linux#create-a-resource-group
# 1. pull nginx image ... need docker for linux docker pull nignx # 2. build image docker build -t mynginx:latest . # 3. run images docker run --name mynginxtest3 -p 8081:80 mynginx:v4 # 4. Push to ACR az cloud set --name AzureChinaCloud az login # az group create 命令創建資源組 az group create --name appacr-rg --location chinanorth2 # az acr create 命令創建 Azure 容器注冊表 az acr create --name lbacr01 --resource-group appacr-rg --sku Basic --admin-enabled true # az acr show 命令以檢索注冊表的憑據 az acr credential show --resource-group appacr-rg --name lbacr01 # docker login 命令登錄到容器注冊表 docker login lbacr01.azurecr.cn --username lbacr01 # 為ACR 標記本地 Docker 映像 docker tag mynginx lbacr01.azurecr.cn/mynginx:latest # docker push 命令將映像推送到為ACR docker push lbacr01.azurecr.cn/mynginx:latest #az acr repository list 命令驗證推送是否成功 az acr repository list -n lbacr01 # 5. 創建 app service 門戶或者是CLI指令
注意:
- 以上指令中 lbacr01 為測試demo中的ACR名稱,實際需要根據情況修改
上傳成功后結果為:
第四步:Azure門戶中創建App Service,選擇Docker並從ACR中獲取鏡像
注意:整個操作根據Azure門戶提示一步一步進行。 https://portal.azure.cn/#create/Microsoft.WebSite, 如要使用AZ命令,則同樣參考第三步鏈接(將應用服務配置為從注冊表部署映像:https://docs.microsoft.com/zh-cn/azure/app-service/tutorial-custom-container?pivots=container-linux#configure-app-service-to-deploy-the-image-from-the-registry)
第五步:驗證App Service的訪問及反向代理結果
附錄:方案中的靜態頁面內容
appnginx.html
<html> <body> <h1>Hello docker + nginx from china azure app service /home/site/wwwroot/ !</h1> <h2>Hello docker + nginx from china azure app service /home/site/wwwroot/ !</h2> <h3>Hello docker + nginx from china azure app service /home/site/wwwroot/ !</h3> <h4>Hello docker + nginx from china azure app service /home/site/wwwroot/ !</h4> <h5>Hello docker + nginx from china azure app service /home/site/wwwroot/ !</h5> <h6>Hello docker + nginx from china azure app service /home/site/wwwroot/ !</h6> </body> </html>
update.html
<html> <body> <h1>update page ....... !</h1> <h5>China azure app service /home/site/wwwroot/ !</h5> </body> </html>
參考資料
nginx反向代理配置兩個不同服務器:https://www.cnblogs.com/momjs/p/10615088.html
使用自定義容器將自定義軟件遷移到 Azure 應用服務: https://docs.microsoft.com/zh-cn/azure/app-service/tutorial-custom-container?pivots=container-linux