Reputation: 121
I'm trying to have my asp.net core app use ASPNETCORE_URLS to set the launch URL. It is not working as expected.
I have tried everything I found online, but I keep getting stuck. The app works without the environmental variables and runs in a docker container fine. But wont work when enabling environmental variables.
Desired result: 0.0.0.0:5000 Result : localhost:5000
Startup:
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json")
.AddEnvironmentVariables()
.Build();
}
Env variable in dockerfile:
ENV ASPNETCORE_URLS=http://+:5000
Docker File:
#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"]
The environmental variable is detected by the application, it just wont be used for some reason.
Thanks in advance!
Upvotes: 12
Views: 36171
Reputation: 2695
this is a common misconception of the ENV
keyword in DOCKERFILE
move it to the app image to have an effect
FROM base AS final
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:5000
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Platform.API.dll"]
the ENV
keyword applies to the current build stage according to the dockerfile reference
The ENV instruction sets the environment variable to the value . This value will be in the environment for all subsequent instructions in the build stage and can be replaced inline in many as well.
FROM
however starts a new build stage
The FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction.
you can try this by building this DOCKERFILE
from alpine
ENV asdf test
RUN echo $asdf
from alpine
RUN echo $asdf
returns
$ 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
Upvotes: 21