16

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

mmffggss
  • 195
  • 1
  • 1
  • 7

4 Answers4

15
ENTRYPOINT ["dotnet", "test.dll", "--environment=Development"]
wast
  • 878
  • 9
  • 27
10

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:

Mostafa Hussein
  • 11,063
  • 3
  • 36
  • 61
  • 2
    I did that but still when I am running docker log it says this: Hosting environment: Production – mmffggss Feb 27 '19 at 09:15
  • maybe you need to modify your code in order to make it detect the actual environment as shown in here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.2#environments – Mostafa Hussein Feb 27 '19 at 09:16
  • also what if you changed the name of the config file in your code to `appsettings.{env.EnvironmentName}.json` – Mostafa Hussein Feb 27 '19 at 09:20
  • then how to set different connection strings for different environments? here is my appsetings.json now: { "ConnectionStrings": { "DefaultConnection": "test" }, "Logging": { "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } } } – mmffggss Feb 27 '19 at 09:26
  • and for the code modification part, I have done that in my launchsettins.json before and it works by itself but in docker it does not – mmffggss Feb 27 '19 at 09:28
  • have you checked the following question ? https://stackoverflow.com/q/46364293/2336650 – Mostafa Hussein Feb 27 '19 at 09:29
  • yes. I can run my code from terminal like dotnet run --environment "Development" or production. the problem is with docker – mmffggss Feb 27 '19 at 09:33
  • I am not an expert in dotnet but what about using this `ENTRYPOINT ["dotnet", "run","--environment","Development","test.dll"]` – Mostafa Hussein Feb 27 '19 at 09:37
  • Also note that i have updated the answer to make Development with capital letter – Mostafa Hussein Feb 27 '19 at 09:38
  • Great answer, helped a lot. If using a console app and docker-compose you may require DOTNET_ENVIRONMENT: "Development" – Mr Slim Jul 22 '21 at 12:27
2

If you don't specify the environ while running the container, it will take “Production” by default.

To run another environ:

docker run -d -p 8000:80 –name something –env ASPNETCORE_ENVIRONMENT=Development myimage

mili spring
  • 421
  • 3
  • 8
2

I had same problem. My Project has 3 appsetings.json inside (one generic and two for development and production like this appsettings.development.json and appsettings.producation.json). I tried these:

  1. Not to mention environment in any docker files as I don't wanna stick to file (and source control respectively) because I should have possibility to run same image in different environments. I starts my container with --env ASPNETCORE_ENVIRONMENT=development or DOTNET_ENVIRONMENT=development with no result.
  2. I added ENV ASPNETCORE_ENVIRONMENT development to my dockerfile and it works. But this make me lose procs from 1st note. So I removed it.
  3. I start my container just with --env Environment=development and it somehow works! Miracle! Actually I still don't know why it works. Is 'Environment' a docker's built-in variable? And how it relate to ASPNETCORE_ENVIRONMENT? Or is ASPNETCORE_ENVIRONMENT (and DOTNET_ENVIRONMENT) variable dig to ENVIRONMENT variable eventually?