首页 > 解决方案 > 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).

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.

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.

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.

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

Direct from that link:

Linux App Service VNet Integration Azure Virtual Network (VNet) integration for Linux Web App is currently in Preview. Customers can use the VNet feature for development and integration testing with your web apps. Please do not use the feature for production purposes. Learn about how to configure VNet with your web app.

During Preview you will need to modify your application in order to integrate with VNet. This is a temporary limitation during VNet Preview release, we will remove the limitation before 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. The PORT environment variable is automatically set by App Service platform at startup time. For example, for a Node.js Express app, you should have the following code as part of your server.js file. The full example can be found on 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.

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.

In short I have to apply a small workaround while VNet is in preview.

The page provides a link to a DockerFile that can apparently be used for dotnet core apps. 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. 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?

My current DockerFile (default docker file for the API in Visual Studio). The only thing I changed here was the version of Windows that was being targeted.

#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"]

标签: azuredockerdockerfileazure-web-app-service

解决方案


在 docker build 期间将监听端口配置为 ENV PORT。Dockerfile 中的 ENTRYPOINT 命令如下所示:

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

有关 VNet 问题的更多解释,请在此处查看我的完整答案


推荐阅读