简体   繁体   English

如何编写 docker 文件以在 Azure 应用服务中使用 PORT 环境变量

[英]How do I write a docker file to use the PORT environment variable in Azure App Service

I'm learning more about Azure, Containers, App Services and the VNet integration (preview).我正在了解有关 Azure、容器、应用服务和 VNet 集成(预览)的更多信息。

I can successfully deploy my dotnet core (3.1) API directly to an Azure App Service (Linux flavor) and set up the appropriate VNet integration, which allows the API to access an on-prem 192.168.. address while the API is publicly accessible.我可以成功地将我的 dotnet 核心 (3.1) API 直接部署到 Azure 应用服务(Linux 风格)并设置适当的 VNet 集成,这允许 API 访问本地 192.168.. 地址,而 API 可公开访问。

What I would like to do is deploy the code as a Docker image to the Azure Container Registry and then deploy the container image to an App Service and use the same VNet integration.我想要做的是将代码作为 Docker 映像部署到 Azure 容器注册表,然后将容器映像部署到应用服务并使用相同的 VNet 集成。

When I do this the containerized version of the App Service does not reach the same 192.168.. address that the direct-to-app service does.当我这样做时,应用服务的容器化版本没有达到直接应用服务所达到的相同 192.168.. 地址。

Azure informs me that more information on this issue can be found here: https://github.com/Azure/app-service-linux-docs/blob/master/app_service_linux_vnet_integration.md Azure 通知我可以在此处找到有关此问题的更多信息: https : //github.com/Azure/app-service-linux-docs/blob/master/app_service_linux_vnet_integration.md

Direct from that link :直接从该链接

Linux App Service VNet Integration Azure Virtual Network (VNet) integration for Linux Web App is currently in Preview. Linux 应用服务 VNet 集成 Linux Web 应用的 Azure 虚拟网络 (VNet) 集成目前处于预览阶段。 Customers can use the VNet feature for development and integration testing with your web apps.客户可以使用 VNet 功能对您的 Web 应用进行开发和集成测试。 Please do not use the feature for production purposes.请不要将该功能用于生产目的。 Learn about how to configure VNet with your web app.了解如何使用 Web 应用配置 VNet。

During Preview you will need to modify your application in order to integrate with VNet.在预览期间,您需要修改应用程序以与 VNet 集成。 This is a temporary limitation during VNet Preview release, we will remove the limitation before GA.这是 VNet Preview 发布期间的临时限制,我们将在 GA 之前移除该限制。 In your application, please use the PORT environment variable as the main web server's listening port, instead of using a hardcoded port number.在您的应用程序中,请使用 PORT 环境变量作为主 Web 服务器的侦听端口,而不是使用硬编码的端口号。 The PORT environment variable is automatically set by App Service platform at startup time. PORT 环境变量由应用服务平台在启动时自动设置。 For example, for a Node.js Express app, you should have the following code as part of your server.js file.例如,对于 Node.js Express 应用程序,您应该将以下代码作为 server.js 文件的一部分。 The full example can be found on Github.完整的示例可以在 Github 上找到。

app.listen(process.env.PORT);

ASPNETCORE_URLS is the recommended way to configure ASP.NET Core docker image and here is the DockerFile example. ASPNETCORE_URLS 是配置 ASP.NET Core docker 镜像的推荐方式,这里是 DockerFile 示例。

Note: we're making continuous improvement to this VNet integration Preview feature for Linux web app, we will roll out feature improvements in the next few months.注意:我们正在不断改进 Linux Web 应用程序的此 VNet 集成预览功能,我们将在接下来的几个月内推出功能改进。

In short I have to apply a small workaround while VNet is in preview.简而言之,我必须在 VNet 处于预览状态时应用一个小的解决方法。

The page provides a link to a DockerFile that can apparently be used for dotnet core apps.该页面提供了一个指向 DockerFile 的链接,该链接显然可用于 dotnet 核心应用程序。 https://github.com/dotnet/dotnet-docker/blob/7c18c117d9bd2d35e30cdb4a1548ac14b9f0f037/3.0/aspnetcore-runtime/nanoserver-1809/amd64/Dockerfile https://github.com/dotnet/dotnet-docker/blob/7c18c117d9bd2d35e30cdb4a1548ac14b9f0f037/3.0/aspnetcore-runtime/nanoserver-1809/amd64/Dockerfile

If i take a copy of that DockerFile my app no longer compiles with errors around the escape character.如果我复制该 DockerFile 的副本,我的应用程序将不再编译,并在转义字符周围出现错误。 If i remove those i then receive other error messages where many of the commands are not known.如果我删除那些我然后收到其他错误消息,其中许多命令是未知的。

My problem is I do not fully understand how to make this work.我的问题是我不完全了解如何进行这项工作。

What Do i have to include in my apps DockerFile (My verison is below) to make sure that the containerized version of the app is correctly set up to use this workaround?我必须在我的应用程序 DockerFile 中包含什么(我的版本如下)以确保正确设置应用程序的容器化版本以使用此解决方法?

My current DockerFile (default docker file for the API in Visual Studio).我当前的 DockerFile(Visual Studio 中 API 的默认 docker 文件)。 The only thing I changed here was the version of Windows that was being targeted.我在这里唯一改变的是目标 Windows 版本。

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.


FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["AZtoOnPremLDC/AZtoOnPremLDC.csproj", "AZtoOnPremLDC/"]
RUN dotnet restore "AZtoOnPremLDC/AZtoOnPremLDC.csproj"
COPY . .
WORKDIR "/src/AZtoOnPremLDC"
RUN dotnet build "AZtoOnPremLDC.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "AZtoOnPremLDC.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AZtoOnPremLDC.dll"]

To configure the listening port to ENV PORT during docker build.在 docker build 期间将监听端口配置为 ENV PORT。 The ENTRYPOINT command in the Dockerfile looks like this: Dockerfile 中的 ENTRYPOINT 命令如下所示:

ENTRYPOINT "dotnet" "Tutorial.WebApi.dll" --urls="http://0.0.0.0:${PORT:-80}"

For more explanation of the problem with VNet, see my complete answer here有关 VNet 问题的更多解释,请在此处查看我的完整答案

According to the message, you do not need to change anything in your Dockerfile to make everything is OK.根据消息,您无需更改 Dockerfile 中的任何内容即可使一切正常。 The environment variable PORT just works in the Azure Web App integrate with Vnet and it's used in your application code when you want to listen to the port, not in the Dockerfile.环境变量 PORT 仅适用于与 Vnet 集成的 Azure Web 应用程序,并且在您想要侦听端口时在应用程序代码中使用,而不是在 Dockerfile 中使用。

What you need to do is that use the suitable base image, create the image with the Dockerfile and then test it until it works fine locally.您需要做的是使用合适的基础镜像,使用 Dockerfile 创建镜像,然后对其进行测试,直到它在本地正常工作。 Then change the listening port with the environment variable PORT in your application with the language you use.然后使用您使用的语言在您的应用程序中使用环境变量 PORT 更改侦听端口。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Azure 中为应用服务设置环境变量 - How to set environment variable in Azure for a App service 如何从我的 SQL Azure 应用服务 Web 应用写入日志文件? - How do I write a log file from my SQL Azure App Service Web App? 如何将新文件路径添加到 azure 应用服务中的环境变量路径 - How to add new file path to environment variable Path in azure app service 如何在 Azure 上为我的应用服务设置环境变量? - How to set environment variable for my app service on Azure? 如何在Web App Service中使用“Azure文件存储”? - How can I use “Azure File Storage” with Web App Service? 如何在Azure App上查找源FQDN,源IP,端口,协议,服务 - How do I find Source FQDN, Source IP, Port, Protocol, Service on Azure App 如何使用Powershell Commandlet在App Service Environment中创建Azure WebApp - How do I create an Azure WebApp in an App Service Environment using the Powershell Commandlets 我们如何将 Azure 应用服务计划从应用服务环境 v2 迁移到应用服务环境 v3 - How do we migrate Azure app service plan from App service environment v2 to App service environment v3 azure 中的 Docker 容器应用服务。 如何将 DefaultAzureCredential 用于 keyvault - Docker container app service in azure. How to use DefaultAzureCredential for keyvault Azure 应用服务 - 我需要在 docker-compose 中公开端口吗 - Azure App Service - Am I required to expose port in docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM