ASPNETCORE_URLS未应用(在Docker容器中部署)

问题描述 投票:1回答:1

我正在尝试让我的asp.net核心应用使用ASPNETCORE_URLS来设置启动URL。它无法正常工作。

我已经尝试了所有在网上找到的内容,但是我一直陷于困境。该应用程序可以在没有环境变量的情况下运行,并且可以在Docker容器中正常运行。但是启用环境变量时不会起作用。

所需结果:0.0.0.0:5000结果:localhost:5000

启动:

        public Startup(IConfiguration configuration, IWebHostEnvironment env)
    {
        Configuration = new ConfigurationBuilder()
             .SetBasePath(env.ContentRootPath)
          .AddJsonFile("appsettings.json")
          .AddJsonFile($"appsettings.{env.EnvironmentName}.json")
          .AddEnvironmentVariables()
          .Build();
    }

dockerfile中的Env变量:

ENV ASPNETCORE_URLS=http://+5000

Docker文件:

#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 5000
ENV ASPNETCORE_URLS=http://+5000

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

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

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

环境变量被应用程序检测到,由于某种原因它不会被使用。

提前感谢!

docker asp.net-core environment-variables
1个回答
0
投票

这是对ENV中的DOCKERFILE关键字的常见误解将其移至应用程序图像即可生效

FROM base AS final
WORKDIR /app
ENV ASPNETCORE_URLS=http://+5000
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Platform.API.dll"]

ENV关键字根据dockerfile reference应用于当前构建阶段>

ENV指令将环境变量设置为值。此值将在所有后续环境中指令在构建阶段,可以内联替换为好。

FROM但是开始新的构建阶段

FROM指令初始化一个新的构建阶段并设置Base后续说明的图片。因此,有效的Dockerfile必须从FROM指令开始。

您可以通过构建此DOCKERFILE进行尝试

from alpine
ENV asdf test
RUN echo $asdf

from alpine
RUN echo $asdf

返回

$ docker build -t envtest .
Sending build context to Docker daemon  6.656kB
Step 1/5 : from alpine
 ---> 5cb3aa00f899
Step 2/5 : ENV asdf test
 ---> Running in 91ae4904857e
Removing intermediate container 91ae4904857e
 ---> 63ef857d07a6
Step 3/5 : RUN echo $asdf       <------ works in same build stage
 ---> Running in b9037c76cc93
test
Removing intermediate container b9037c76cc93
 ---> 17edf57d8055
Step 4/5 : from alpine
 ---> 5cb3aa00f899
Step 5/5 : RUN echo $asdf       <------- does not in next build stage
 ---> Running in 62b42e7c28d8

Removing intermediate container 62b42e7c28d8
 ---> 7e6a8a58442f
Successfully built 7e6a8a58442f
Successfully tagged envtest:latest
© www.soinside.com 2019 - 2024. All rights reserved.