容器在本地运行但在 ACI 中失败

Container runs locally but fails in ACI

我创建了一个由 dotnet new web 生成的示例 asp.net 核心应用程序。添加了一个 docker 文件公开端口 5000。

Docker 文件如下

FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base
WORKDIR /app
EXPOSE 5000

ENV ASPNETCORE_URLS=http://+:5000

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /src
COPY ["Sampleapp.csproj", "./"]
RUN dotnet restore "Sampleapp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Sampleapp.csproj" -c Release -o /app/build

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

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

当我 运行 它在我的本地桌面上时,它工作正常。但是,当我将其添加到 ACR 并 运行 添加到 ACI 时,它失败了。

az container create --resource-group $res_grp `
                    --name $aci_container_name `
                    --image $image_name `
                    --registry-login-server $registry_login_server `
                    --registry-username $akv_pull_usernamevalue `
                    --registry-password $akv_pullpasswordvalue `
                    --dns-name-label $dns_name_label `
                    --output table

Name              ResourceGroup       Status    Image                                                 IP:ports          Network    CPU/Memory       OsType    Location
----------------  ------------------  --------  ----------------------------------------------------  ----------------  ---------  ---------------  --------  -------------
aci0734container  containerbasics_rg  Running   devcontainerregistry0734.azurecr.io/helloacrworld:v1  20.53.163.177:80  Public     1.0 core/1.5 gb  Linux     australiaeast

也没有错误日志。

az container logs --resource-group containerbasics_rg --name aci0734container

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://[::]:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app

当我尝试卷曲时这是响应

curl http://20.53.163.177:5000
curl : Unable to connect to the remote server
At line:1 char:1
+ curl http://20.53.163.177:5000
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

当我 运行 它在本地映射目标端口时,但 ACI 不允许端口映射。是这个问题吗?

您需要通过添加--ports 5000来公开非标准端口。 ACI 默认为端口 80,您的实例正在侦听并公开端口 5000:

az container create --resource-group $res_grp `
                --name $aci_container_name `
                --image $image_name `
                --registry-login-server $registry_login_server `
                --registry-username $akv_pull_usernamevalue `
                --registry-password $akv_pullpasswordvalue `
                --dns-name-label $dns_name_label `
                --ports 5000 `
                --output table