问题描述
根据以下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