我试图让我的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"]
应用程序检测到环境变量,出于某种原因它不会被使用。
谢谢!
答案 0 :(得分:4)
这是对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