gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 18:10:14 26 4
gpt4 key购买 nike

我正在尝试让我的 asp.net 核心应用程序使用 ASPNETCORE_URLS 来设置启动 URL。它没有按预期工作。
我已经尝试了我在网上找到的所有东西,但我一直被卡住。该应用程序在没有环境变量的情况下工作,并在 docker 容器中正常运行。但是在启用环境变量时不起作用。
预期结果:0.0.0.0:5000
结果:本地主机: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 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"]
应用程序检测到环境变量,只是出于某种原因不会使用它。
提前致谢!

最佳答案

这是对 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 关键字适用于当前构建阶段

The ENV instruction sets the environment variable to the value. This value will be in the environment for all subsequentinstructions in the build stage and can be replaced inline in many aswell.

FROM然而开始一个新的构建阶段

The FROM instruction initializes a new build stage and sets the BaseImage for subsequent instructions. As such, a valid Dockerfile muststart with a FROM instruction.


你可以通过构建这个来尝试这个 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

关于docker - 未应用 ASPNETCORE_URLS(在 docker 容器中部署),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60546843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com