在 Azure App 服務 上部署容器化 Flask 或 FastAPI Web 應用程式
本教學課程說明如何使用適用於容器的 Web 應用程式功能,將 Python Flask 或 FastAPI Web 應用程式部署至 Azure App 服務。 適用於容器的 Web 應用程式提供一個輕鬆的斜坡,讓開發人員能夠利用完全受控 Azure App 服務 平臺,但也想要包含應用程式及其所有相依性的單一可部署成品。 如需在 Azure 中使用容器的詳細資訊,請參閱 比較 Azure 容器選項。
在本教學課程中,您會使用 Docker CLI 和 Docker 選擇性地建立 Docker 映射,並在本機進行測試。 此外,您可以使用 Azure CLI 在 Azure 中建立 Docker 映像,並將其部署至 Azure App 服務。 您也可以使用已安裝 Azure 工具延伸模組的 Visual Studio Code 進行部署。 如需建置和建立 Docker 映射以在 Azure Container Apps 上執行的範例,請參閱 在 Azure Container Apps 上部署 Flask 或 FastPI Web 應用程式。
注意
本教學課程示範如何建立可在App Service上執行的 Docker 映像。 這不需要使用App Service。 您可以直接從本機工作區將程式代碼部署至 App Service,而不需要建立 Docker 映射。 如需範例,請參閱快速入門:將 Python (Django 或 Flask) Web 應用程式部署至 Azure App 服務。
必要條件
若要完成本教學課程,您需要:
您可以在其中將 Web 應用程式部署至 Azure App 服務 和 Azure Container Registry 的 Azure 帳戶。
Azure CLI 可建立 Docker 映像,並將其部署至 App Service。 此外, 您也可以選擇 Docker 和 Docker CLI 來建立 Docker,並在本機環境中進行測試。
取得範例程式碼
在您的本機環境中,取得程序代碼。
新增 Dockerfile 和 .dockerignore 檔案
新增 Dockerfile 以指示 Docker 如何建置映射。 Dockerfile 會指定 Gunicorn 的使用,這是將 Web 要求轉送至 Flask 和 FastAPI 架構的生產層級 Web 伺服器。 ENTRYPOINT 和 CMD 命令會指示 Gunicorn 處理應用程式物件的要求。
# syntax=docker/dockerfile:1
FROM python:3.11
WORKDIR /code
COPY requirements.txt .
RUN pip3 install -r requirements.txt
COPY . .
EXPOSE 50505
ENTRYPOINT ["gunicorn", "app:app"]
50505
用於此範例中的容器埠(內部),但您可以使用任何免費埠。
請檢查requirements.txt檔案,以確定它包含 gunicorn
。
Flask==2.2.2
gunicorn
Werkzeug==2.2.2
新增 .dockerignore 檔案,以從映射中排除不必要的檔案。
.git*
**/*.pyc
.venv/
設定 gunicorn
Gunicorn 可以使用 gunicorn.conf.py 檔案進行設定。 當 gunicorn.conf.py 檔案位於執行 gunicorn 的相同目錄中時,您不需要在 Dockerfile 中指定其位置。 如需指定組態檔的詳細資訊,請參閱 Gunicorn 設定。
在本教學課程中,建議的組態檔會根據可用的CPU核心數目來設定 gunicorn 來增加其背景工作角色數目。 如需 gunicorn.conf.py 檔案設定的詳細資訊,請參閱 Gunicorn 組態。
# Gunicorn configuration file
import multiprocessing
max_requests = 1000
max_requests_jitter = 50
log_file = "-"
bind = "0.0.0.0:50505"
workers = (multiprocessing.cpu_count() * 2) + 1
threads = workers
timeout = 120
在本機建置並執行映像
在本機建置映像。
注意
docker build
如果命令傳回錯誤,請確定 docker deamon 正在執行。 在 Windows 上,確定 Docker Desktop 正在執行。
在 Docker 容器中本機執行映像。
docker run --detach --publish 5000:50505 flask-demo
http://localhost:5000
在瀏覽器中開啟 URL,以查看在本機執行的 Web 應用程式。
選項 --detach
會在背景中執行容器。 選項 --publish
會將容器埠對應至主機上的埠。 主機埠 (external) 是配對中的第一個埠,而容器埠 (internal) 則是第二個。 如需詳細資訊,請參閱 Docker 執行參考。
建立資源群組和 Azure Container Registry
使用 az group create 命令建立群組。
az group create --name web-app-simple-rg --location eastus
Azure 資源群組是在其中部署與管理 Azure 資源的邏輯容器。 建立資源群組時,您可以指定位置,例如 eastus。
使用 az acr create 命令建立 Azure Container Registry。
az acr create --resource-group web-app-simple-rg \ --name webappacr123 --sku Basic --admin-enabled true
注意
登錄名稱在 Azure 中必須是唯一的。 如果您收到錯誤,請嘗試其他名稱。 登錄名稱可以包含5-50個英數位元。 不允許連字元和底線。 若要深入瞭解,請參閱 Azure Container Registry 名稱規則。 如果您使用不同的名稱,請確定您使用您的名稱,而不是
webappacr123
在下列各節中參考登錄和登錄成品的命令中。Azure Container Registry 是私人 Docker 登錄,可儲存映射以用於 Azure 容器執行個體、Azure App 服務、Azure Kubernetes Service 和其他服務。 建立登錄時,您可以指定名稱、SKU 和資源群組。 第二個命令會使用 az credential show 命令,將密碼儲存至變數。 密碼是用來在稍後的步驟中向登錄進行驗證。
將環境變數設定為登錄的密碼值。
$ACR_PASSWORD=(az acr credential show \ --resource-group web-app-simple-rg \ --name webappacr123 \ --query "passwords[?name == 'password'].value" \ --output tsv)
Bash 殼層會顯示用來建立環境變數的命令。 視需要變更其他殼層的語法和接續字元 (
\
)。您也可以前往登錄、選取 [存取金鑰],以及複製密碼,從 Azure 入口網站 取得密碼
ACR_PASSWORD
。
在 Azure Container Registry 中建置映射
使用 az acr build 命令在 Azure 中建 置 Docker 映射。 命令會使用目前目錄中的 Dockerfile,並將映像推送至登錄。
az acr build \
--resource-group web-app-simple-rg \
--registry webappacr123 \
--image webappsimple:latest .
選項 --registry
會指定登錄名稱,而 --image
選項會指定映像名稱。 映射名稱的格式 registry.azurecr.io/repository:tag
為 。
將 Web 應用程式部署至 Azure
使用 az appservice plan 命令建立 App Service 方案 。
az appservice plan create \ --name webplan \ --resource-group web-app-simple-rg \ --sku B1 \ --is-linux
使用 az webapp create 命令建立 Web 應用程式。
az webapp create \ --resource-group web-app-simple-rg \ --plan webplan --name webappsimple123 \ --docker-registry-server-password $ACR_PASSWORD \ --docker-registry-server-user webappacr123 \ --role acrpull \ --deployment-container-image-name webappacr123.azurecr.io/webappsimple:latest
注意:
Web 應用程式名稱在 Azure 中必須是唯一的。 如果您收到錯誤,請嘗試其他名稱。 名稱可以包含英數位元和連字元,但不能以連字元開頭或結尾。 若要深入瞭解,請參閱 Microsoft.Web 名稱規則。
如果您使用的名稱與
webappacr123
Azure Container Registry 不同,請務必適當地更新--docker-registry-server-user
和--deployment-container-image-name
參數。建立 Web 應用程式可能需要幾分鐘的時間。 您可以使用 az webapp log tail 命令來檢查部署記錄。 例如:
az webapp log tail --resource-group web-app-simple-rg --name webappsimple123
。 如果您在其中看到具有「熱身」的專案,則會部署容器。Web 應用程式的網址為
<web-app-name>.azurewebsites.net
,例如https://webappsimple123.azurewebsites.net
。
進行更新並重新部署
進行程式代碼變更之後,您可以使用 az acr build 和 az webapp update 命令重新部署至 App Service。
清理
本教學課程中建立的所有 Azure 資源都位於相同的資源群組中。 拿掉資源群組會移除資源群組中的所有資源,這是移除應用程式所用所有 Azure 資源最快的方式。
若要移除資源,請使用 az group delete 命令。
az group delete --name web-app-simple-rg
您也可以移除 Azure 入口網站 或 Visual Studio Code 和 Azure Tools Extension 中的群組。
下一步
如需詳細資訊,請參閱以下資源: