【Azure 应用服务】 部署到App Service for Linux 服务的Docker 镜像,如何配置监听端口呢?

简介: 【Azure 应用服务】 部署到App Service for Linux 服务的Docker 镜像,如何配置监听端口呢?

问题描述

根据以下DockerFile文件,创建了一个ASP.NET Core的 helloworld 镜像,通过监听3721端口来接受请求。

# 1. 指定编译和发布应用的镜像
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
# 2. 指定(编译和发布)工作目录
WORKDIR /app
# 3. 拷贝.csproj到工作目录/app,然后执行dotnet restore恢复所有安装的NuGet包
COPY *.csproj ./
RUN dotnet restore
# 4. 拷贝所有文件到工作目录(/app),然后执行dotnet publish命令将应用发布到/app/out目录下
COPY . ./
RUN dotnet publish -c Release -o out
# 5. 编译生成Docker镜像
# 5.1.设置基础镜像
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
# 5.2. 设置(运行)工作目录,并将发布文件拷贝到out子目录下
WORKDIR /app
COPY --from=build /app/out .
# 5.3. 利用环境变量设置ASP.NET Core应用的监听地址
ENV ASPNETCORE_URLS http://0.0.0.0:3721
# 5.4. 执行dotnet命令启动ASP.NET Core应用
ENTRYPOINT ["dotnet", "helloworld.dll"]

在本地构建镜像后,可以通过 docker run -d -p 8088:3721 --name myapp helloworldapp,指定本机8088端口映射到Docker Container中的3721端口。

但是在部署到微软云后,如何来设置映射端口呢? 默认情况下App Service 使用的是80端口,从而导致容器启动失败。

错误日志为:

2022-06-15T08:10:50.546Z INFO - Status: Downloaded newer image for lbacrtest01.azurecr.cn/helloworldapp:v1
2022-06-15T08:10:50.587Z INFO - Pull Image successful, Time taken: 2 Minutes and 5 Seconds
2022-06-15T08:10:50.676Z INFO - Starting container for site
2022-06-15T08:10:50.676Z INFO - docker run -d --expose=80 --name lbimagetest01_0_1e3ba35e -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITE_SITE_NAME=lbimagetest01 -e WEBSITE_AUTH_ENABLED=False -e PORT=80 -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=lbimagetest01.chinacloudsites.cn -e WEBSITE_INSTANCE_ID=fca8f86e2aee5216a9504a5ef5a82caeab7a80b1093c88842a262c639a933047 -e WEBSITE_USE_DIAGNOSTIC_SERVER=False lbacrtest01.azurecr.cn/helloworldapp:v1
2022-06-15T08:10:50.676Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2022-06-15T08:11:02.091Z INFO - Initiating warmup request to container lbimagetest01_0_1e3ba35e for site lbimagetest01
2022-06-15T08:11:17.595Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 15.5042301 sec
2022-06-15T08:11:33.023Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 30.9320513 sec
2022-06-15T08:11:48.176Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 46.0847374 sec
2022-06-15T08:12:03.309Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 61.2176683 sec
2022-06-15T08:12:18.466Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 76.3745667 sec
2022-06-15T08:12:33.640Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 91.5484349 sec
2022-06-15T08:12:48.766Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 106.6748131 sec
2022-06-15T08:13:03.926Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 121.8346172 sec
2022-06-15T08:13:19.084Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 136.9925924 sec
2022-06-15T08:13:34.841Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 152.7495565 sec
2022-06-15T08:13:54.936Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 172.8446712 sec
2022-06-15T08:14:10.134Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 188.0428893 sec
2022-06-15T08:14:29.366Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 207.274851 sec
2022-06-15T08:14:44.507Z INFO - Waiting for response to warmup request for container lbimagetest01_0_1e3ba35e. Elapsed time = 222.4153247 sec
2022-06-15T08:14:52.646Z ERROR - Container lbimagetest01_0_1e3ba35e for site lbimagetest01 did not start within expected time limit. Elapsed time = 230.5552784 sec
2022-06-15T08:14:58.488Z ERROR - Container lbimagetest01_0_1e3ba35e didn't respond to HTTP pings on port: 80, failing site start. See container logs for debugging.

 

问题解答

在App Service中,如果要为容器暴露端口,可以使用应用配置参数 WEBSITES_PORT,  把它的值设置为DockerFile中配置的端口就行。如本示例中就配置它的值为3721.

 

保存后将自动重启App Service,查看Docker日志,发现 --expose的端口变为3721, Container 初始化成功并且准备接受请求。

2022-06-15T08:14:59.910Z INFO - Pulling image: lbacrtest01.azurecr.cn/helloworldapp:v1
2022-06-15T08:15:00.307Z INFO - v1 Pulling from helloworldapp
2022-06-15T08:15:00.318Z INFO - Digest: sha256:1f6d1d7452248155c22adac3007d19c59da2825d982da8383fc2cf33f80cade7
2022-06-15T08:15:00.325Z INFO - Status: Image is up to date for lbacrtest01.azurecr.cn/helloworldapp:v1
2022-06-15T08:15:00.327Z INFO - Pull Image successful, Time taken: 0 Minutes and 0 Seconds
2022-06-15T08:15:00.488Z INFO - Starting container for site
2022-06-15T08:15:00.495Z INFO - docker run -d --expose=3721 --name lbimagetest01_0_fa97e372 -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITES_PORT=3721 -e WEBSITE_SITE_NAME=lbimagetest01 -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=lbimagetest01.chinacloudsites.cn -e WEBSITE_INSTANCE_ID=fca8f86e2aee5216a9504a5ef5a82caeab7a80b1093c88842a262c639a933047 -e WEBSITE_USE_DIAGNOSTIC_SERVER=False lbacrtest01.azurecr.cn/helloworldapp:v1
2022-06-15T08:15:00.496Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2022-06-15T08:15:04.397Z INFO - Initiating warmup request to container lbimagetest01_0_fa97e372 for site lbimagetest01
2022-06-15T08:15:05.738Z INFO - Container lbimagetest01_0_fa97e372 for site lbimagetest01 initialized successfully and is ready to serve requests.

 

附录:创建,推送,部署Image的命令

## 本地构建 helloworldapp 镜像文件
docker build -t helloworldapp .
## 本地运行 helloworldapp 镜像,8088端口映射容器中3721端口
docker run -d -p 8088:3721 --name myapp helloworldapp
## 登录到ACR(azure容器库)中
docker login <acrtest01>.azurecr.cn --username <testuser01>
## 与ACR关联本地镜像文件
docker tag helloworldapp:v1 <acrtest01>.azurecr.cn/helloworldapp:v1
## PUSH 镜像文件到ACR中
docker push <acrtest01>.azurecr.cn/helloworldapp:v1

 

参考文档:

使用自定义容器将自定义软件迁移到 Azure 应用服务:https://docs.azure.cn/zh-cn/app-service/tutorial-custom-container?pivots=container-linux#configure-app-service-to-deploy-the-image-from-the-registry

相关文章
|
11天前
|
Linux Docker Windows
Docker配置https证书案例
本文介绍了如何为Docker的Harbor服务配置HTTPS证书,包括安装Docker和Harbor、修改配置文件以使用证书、生成自签名证书、配置证书以及验证配置的步骤。
18 2
Docker配置https证书案例
|
5天前
【Azure Logic App】使用Event Hub 连接器配置 Active Directory OAuth 认证无法成功连接到中国区Event Hub的解决之法
An exception occurred while retrieving properties for Event Hub: logicapp. Error Message: 'ClientSecretCredential authentication failed: AADSTS90002: Tenant 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Che
|
11天前
|
安全
【Azure App Service】App service无法使用的情况分析
App Service集成子网后,如果子网网段中的剩余IP地址非常少的情况下,会在App Service实例升级时( 先加入新实例,然后在移除老实例 )。新加入的实例不能被分配到正确的内网IP地址,无法成功的访问内网资源。 解决方法就是为App Service增加子网地址, 最少需要/26 子网网段地址。
|
16天前
|
缓存 Serverless Docker
函数计算产品使用问题之怎么修改Docker守护进程配置
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
|
19天前
|
关系型数据库 MySQL 应用服务中间件
配置docker阿里云镜像地址
配置docker阿里云镜像地址
|
16天前
|
jenkins Java 持续交付
jenkins学习笔记之十九:Docker安装jenkins master及动、静态配置slave
jenkins学习笔记之十九:Docker安装jenkins master及动、静态配置slave
|
2月前
|
弹性计算 应用服务中间件 Linux
阿里云服务器开放端口完整图文教程
笔者近期开发完成的服务端程序部署在阿里云的ECS云服务器上面,一些应用程序配置文件需要设置监听的端口(如Tomcat的8080、443端口等),虽然通过CentOs 7系统的的「防火墙」开放了对应的端口号,任然无法访问端口号对应的应用程序,后面了解到原来还需要设置云服务器的「安全组规则」,开放相应的端口权限,服务端的接口才能真正开放。
482 1
阿里云服务器开放端口完整图文教程
|
2月前
|
弹性计算 运维 数据安全/隐私保护
云服务器 ECS产品使用问题之如何更改服务器的IP地址或端口号
云服务器ECS(Elastic Compute Service)是各大云服务商阿里云提供的一种基础云计算服务,它允许用户租用云端计算资源来部署和运行各种应用程序。以下是一个关于如何使用ECS产品的综合指南。
|
20天前
|
缓存 NoSQL 网络安全
【Azure Redis 缓存】使用开源工具redis-copy时遇见6379端口无法连接到Redis服务器的问题
【Azure Redis 缓存】使用开源工具redis-copy时遇见6379端口无法连接到Redis服务器的问题
|
2月前
|
网络协议 Linux Unix
面试官:服务器最大可以创建多少个tcp连接以及端口并解释下你对文件句柄的理解
面试官:服务器最大可以创建多少个tcp连接以及端口并解释下你对文件句柄的理解
58 0
面试官:服务器最大可以创建多少个tcp连接以及端口并解释下你对文件句柄的理解