Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set the environment of dotnet core in docker?

I want to be able to run dotnet core by docker in different environments(for now just development and production) but my docker always start in production environment. here is my docker file:

FROM microsoft/dotnet:sdk AS build-env

WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -o out

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "test.dll"]

I have appsettings.Production.json and appsettings.Development.json and I have configured my two environments in program.cs like below:

 public static IWebHostBuilder CreateWebHostBuilder (string[] args) =>
            WebHost.CreateDefaultBuilder (args)

            .ConfigureAppConfiguration ((hostingContext, config) => {
                config.AddJsonFile ("appsettings.Development.json", optional : false, reloadOnChange : false)
                    .AddJsonFile ("appsettings.Production.json", optional : false, reloadOnChange : false);
            })
            //End of update
            .UseStartup<Startup> ();

I build the docker image and container but when it starts it starts in production mode I want it to start in development mode

like image 390
mmffggss Avatar asked Feb 27 '19 09:02

mmffggss


People also ask

How do I set an environment variable in Docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.


1 Answers

ASP.NET Core reads the following environment variable ASPNETCORE_ENVIRONMENT if not set then it defaults to production. What you need to do is to use this in your Dockerfile

ENV ASPNETCORE_ENVIRONMENT Development

Or if you are using docker-compose.yml file

environment:
  ASPNETCORE_ENVIRONMENT: Development

For more details:

  • Use multiple environments in ASP.NET Core
  • ENV usage in Dockerfile
like image 125
Mostafa Hussein Avatar answered Oct 01 '22 19:10

Mostafa Hussein