2

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?

Ask
  • 3,076
  • 6
  • 30
  • 63
  • 1
    as described originally in https://stackoverflow.com/questions/52376855/aspnetcore-environment-in-docker/52383707 you could try to change the entrypoint to `ENTRYPOINT["dotnet", "Isofy-Api.dll", "--environment=Development"]` – pero_hero Mar 06 '20 at 08:38

3 Answers3

9

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"]
Isitar
  • 1,286
  • 12
  • 29
0

I think its better to specify the ASPNETCORE_ENVIRONMENT variable on the task definition environment variables instead of the Dockerfile itself. This will allow you to change the ASPNETCORE_ENVIRONMENT depending on which environment you are deploying to (eg. test/prod etc).

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ecs-taskdefinition-containerdefinitions.html#cfn-ecs-taskdefinition-containerdefinition-environment

If you want to run it locally via docker-compose, add the environment variable in the service definition within docker-compose.

https://docs.docker.com/compose/environment-variables/

richardd
  • 128
  • 1
  • 6
0

Rather than have the environment variables in my docker or env file I prefer to have separate aws-ecs-tools-defaults.json for each environment, and publish with a command line:

dotnet ecs deploy-scheduled-task  --cfg aws-ecs-tools-defaults.json --container-environment-variables EnvironmentVariableName1=test1;EnvironmentVariableName1=test1

If you haven't got dotnet ecs installed you can do so via:

dotnet tool install --global Amazon.ECS.Tools
Liam
  • 5,033
  • 2
  • 30
  • 39