Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify environment variable in dockerfile for .net core application

I have a .net core 3.1 application which I is deployed on aws ecs as a docker container. Now I want to specify environment variable in my dockerfile which I am trying to use in my code but everytime I am getting no value.

Here is the .net core:

        private IWebHostEnvironment Environment { get; set; }
        public IConfiguration Configuration { get; set; }


        public void ConfigureServices(IServiceCollection services)
        {
          var builder = new ConfigurationBuilder()
           .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
           .AddJsonFile($"appsettings.{Environment.EnvironmentName}.json", optional: true)
           .AddEnvironmentVariables();

Now I want to replace environment.EnvironmentName with the value I specify in Dockerfile but it's not working. Also I read somewhere that I can specify environment variable while execute docker-run command but in my case I can't do that because aws ecs is running the docker container

Here is the docker file:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

# Copy everything else and build
COPY . ./
ENV ASPNETCORE_ENVIRONMENT Development

RUN dotnet restore Isofy-Api/*.csproj
RUN dotnet publish Isofy-Api/*.csproj -c Release -o out


# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Isofy-Api.dll"]

What am I doing wrong?

like image 786
Ask Avatar asked Nov 22 '25 15:11

Ask


1 Answers

You should specify the environment variable in the final image

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
ENV ASPNETCORE_ENVIRONMENT Development
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "Isofy-Api.dll"]
like image 184
Isitar Avatar answered Nov 25 '25 11:11

Isitar