【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

相关文章
|
2月前
|
开发框架 监控 .NET
【Azure App Service】部署在App Service上的.NET应用内存消耗不能超过2GB的情况分析
x64 dotnet runtime is not installed on the app service by default. Since we had the app service running in x64, it was proxying the request to a 32 bit dotnet process which was throwing an OutOfMemoryException with requests >100MB. It worked on the IaaS servers because we had the x64 runtime install
|
2月前
|
Java 开发工具 Windows
【Azure App Service】在App Service中调用Stroage SDK上传文件时遇见 System.OutOfMemoryException
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
|
2月前
|
安全 Apache 开发工具
【Azure App Service】在App Service上关于OpenSSH的CVE2024-6387漏洞解答
CVE2024-6387 是远程访问漏洞,攻击者通过不安全的OpenSSh版本可以进行远程代码执行。CVE-2024-6387漏洞攻击仅应用于OpenSSH服务器,而App Service Runtime中并未使用OpenSSH,不会被远程方式攻击,所以OpenSSH并不会对应用造成安全风险。同时,如果App Service的系统为Windows,不会受远程漏洞影响!
|
6月前
|
弹性计算 应用服务中间件 Linux
阿里云服务器开放端口完整图文教程
笔者近期开发完成的服务端程序部署在阿里云的ECS云服务器上面,一些应用程序配置文件需要设置监听的端口(如Tomcat的8080、443端口等),虽然通过CentOs 7系统的的「防火墙」开放了对应的端口号,任然无法访问端口号对应的应用程序,后面了解到原来还需要设置云服务器的「安全组规则」,开放相应的端口权限,服务端的接口才能真正开放。
753 1
|
6月前
|
弹性计算 运维 数据安全/隐私保护
云服务器 ECS产品使用问题之如何更改服务器的IP地址或端口号
云服务器ECS(Elastic Compute Service)是各大云服务商阿里云提供的一种基础云计算服务,它允许用户租用云端计算资源来部署和运行各种应用程序。以下是一个关于如何使用ECS产品的综合指南。
|
5月前
|
缓存 NoSQL 网络安全
【Azure Redis 缓存】使用开源工具redis-copy时遇见6379端口无法连接到Redis服务器的问题
【Azure Redis 缓存】使用开源工具redis-copy时遇见6379端口无法连接到Redis服务器的问题
|
6月前
|
网络协议 Linux Unix
面试官:服务器最大可以创建多少个tcp连接以及端口并解释下你对文件句柄的理解
面试官:服务器最大可以创建多少个tcp连接以及端口并解释下你对文件句柄的理解
156 0
面试官:服务器最大可以创建多少个tcp连接以及端口并解释下你对文件句柄的理解
|
5月前
|
网络协议
【qt】TCP的监听 (设置服务器IP地址和端口号)
【qt】TCP的监听 (设置服务器IP地址和端口号)
290 0
|
6月前
|
存储 安全 网络安全
服务器设置了端口映射之后外网还是访问不了服务器
服务器设置了端口映射之后外网还是访问不了服务器
若依修改,若依部署在本地运行时的注意事项,后端连接了服务器,本地的vue.config.js要先改成localhost:端口号与后端匹配,部署的时候再改公网IP:端口号
若依修改,若依部署在本地运行时的注意事项,后端连接了服务器,本地的vue.config.js要先改成localhost:端口号与后端匹配,部署的时候再改公网IP:端口号