创建Node.js APP
bash
npx express-generator lcApp --view ejs
cd lcApp
npm i
npm start
这时就可以通过3000端口来测试这个APP了。
容器化这个应用
创建Dockerfile
docker
FROM node:alpine
COPY . .
RUN npm install
CMD ["npm", "start"]
创建ACR
运行如下命令创建Azure Container Registry:
bash
az acr create --resource-group test --name lcacrnodeappdemo --location uksouth --admin-enabled --sku Basic --public-network-enabled true
这个过程很快,返回的结果类似于:
json
{
"adminUserEnabled": true,
"anonymousPullEnabled": false,
"creationDate": "2023-03-10T22:44:27.018121+00:00",
"dataEndpointEnabled": false,
"dataEndpointHostNames": [],
"encryption": {
"keyVaultProperties": null,
"status": "disabled"
},
"id": "/subscriptions/xxxxxxxxxxxxxx/resourceGroups/xxxxxx/providers/Microsoft.ContainerRegistry/registries/lcacrnodeappdemo",
"identity": null,
"location": "uksouth",
"loginServer": "lcacrnodeappdemo.azurecr.io",
"name": "lcacrnodeappdemo",
}
进入Azure Portal,ACR,Access Keys,记录下来用户名和密码,下一步需要用到。
登录ACR
输入如下命令登录Docker容器:
bash
docker login lcacrnodeappdemo.azurecr.io
Username: lcacrnodeappdemo
Password:
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
在本地构建Docker镜像
bash
docker build -t lcacrnodeappdemo.azurecr.io/test:latest .
验证已经成功创建镜像:
bash
> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lcacrnodeappdemo.azurecr.io/test latest 137dbeeb4880 1 hours ago 179MB
将镜像推送到ACR
bash
docker push lcacrnodeappdemo.azurecr.io/test:latest
这时进入Azure Portal,就能在ACR,Repositories中看到新创建的镜像。
创建App Service Plan
bash
az appservice plan create --name TestNodeAppServicePlan --resource-group test --is-linux --sku B1
创建App Service
bash
az webapp create --name LcTestNodeApp --plan TestNodeAppServicePlan --resource-group test --deployment-container-image-name lcacrnodeappdemo.azurecr.io/test:latest
删除Resource Group
最后不要忘记删除这个Resource Group及组中所有资源。
bash
az group delete -n test