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
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With