I have just started with docker. By spending lot time on docker videos and tutorials, finally I am able to create my first docker image (and push to docker hub). I am going to use that image for my dev environment shortly.
Question is:
I have few application configuration in my appsettings.json file. Those configurations are different for different environments. While I pull docker image on my dev environment, those configuration needs to be change according to dev environment. I am not sure how to manage this. Anyone have idea on this?
Few useful information:
I have .net core 2.0 application.
I am using, docker for windows (as requirement).
I'll host that container either in VM or on Azure App Service.
have you already take a look at this
using ConfigurationBuilder
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json")
.AddEnvironmentVariables()
.Build();
In your Startup.cs, if everything is intact you should have this;
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json")
.AddEnvironmentVariables()
.Build();
you have 2 environment names other than Production. Those are
- Development
- Staging
AspNetCore understands which environment you want from the value of the Environment Variable ASPNETCORE_ENVIRONMENT which should be Production, Development or Staging
The environment you are in is important because AspNetCore picks the right settings json file based on your environment name. The code I shared above does this;
Load Settings from appsettings.json
Find environment specific settings file.
For Production -> appsettings.production.json (note, this is usually not used)
For Development -> appsettings.development.json
For Staging -> appsettings.staging.json
Override setting values based on the environment specific settings json file
So the solution to your problem is;
Make sure your appsettings.json file is filled for production values.
Add setting files for appsettings.development.json and appsettings.staging.json. Make the neccessary modifications in the contents of these files.
When you run your Docker container, override the environment variable ASPNETCORE_ENVIRONMENT based on the environment settings you would like to keep as I stated above.
When you docker run you can supply it a list of environment variables or a file with environment settings. This allows you to set the variable you need modified when you start up your container.
Using the -e flag:
docker run -it -e ASPNETCORE_ENVIRONMENT=Prod...
Using the --env-file flag:
docker run -it --env-file ./Prod.env ...
For reference: https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e-env-env-file
Related
I need to change an environment variable in my deployment (nothing else has changed). When I update my environment variables (just using export) and use docker stack deploy, the container environment is not updated. How do I make sure environment variables are updated in the container?
Apparently using docker stack deploy should work to update the environment. My error was not having the environment variable defined in the docker-compose.yml file.
I've got an ASP.NET core 2.2 web app that I have enabled docker support for. I have created a test app here for review here.
I am running it in VS with Docker locally. I want to add environment variables/secrets to the app settings secrets in order to override the values in the appsettings.json file. In order to do this locally, I have tried changing values in:
launchsettings.json
Dockerfile
however, for both of these, when I attach to my docker instance and printenv the variable values, I find that the variable for ASPNETCORE_ENVIRONMENT still shows up as Development.
I am attaching to the running container like this:
docker exec -t -i 4c05 /bin/bash
I have searched all files in my solution. I can't find ASPNETCORE_ENVIRONMENT being set to Development anywhere in the solution. However, somehow, the environment variable is still being set with that value.
What could be going wrong? I want that variable to change. Once working, what I really want to do is to add a connection string secret to environment variables so that it can be used locally via the appsettings.json file or via a docker secret environment variable if the aspnetcore web app is running in a container. I think I've got this code working, it's just that the variables are not being deployed as expected to the running container.
My VS version is:
thanks
Mmm - seems there is a problem with DockerFile support in VS. However, when I use the Orchestration Support, using docker-compose, the functionality works as expected, so I'm answering the question myself :-)
Is there a way to force docker compose to assume environment variables from the underlying machine?
Background:
I decided to play around with Docker in my ASP.NET Core Web Application, so I used the Add Docker Support option in Visual Studio, which created a .dcproj (Docker Compose project).
Prior to that, I was reading some configs from Environment Variables on the current machine (either my dev machine or a server).
I realized when I'm debugging with the docker compose project, I'm not able to get data from Environment Variables anymore, which makes sense, since docker became the new environment (not my machine anymore). I wouldn't like these values to be pushed into my git repo.
You have to specify the environment variables in the docker-compose.yml file like this
environment:
- VAR1
- VAR2=fixedvalue
In this case VAR1 assumes the value that is defined for the variable in your computer and VAR2 will assume the value that is specified regardless or what is configured in your computer.
You also have the env_file option which allows you to specify a file with all the variables set.
env_file:
- web-variables.env
You can find more information in the documentation.
I have a web service which consists of a backend and a forntend, and in the frontend I use an API uri which can change depending on the environment the service is being deployed to.
Using webpack's EnvironmentPlugin I can build the source code simply with other environment variables. The plugin allows me to use process.env in javascript which is convenient in the development phase but after bundling the frontend's code process.env will remain the same with the given environment variables when bundled.
The issue is that on the CI pipelines I build a docker image for the web service but I don't know the API uri until deploying it later on.
How can I effectively change the API uri based on environment variables?
You have two options for passing environment variables one is via a file
docker run --env-file ./env.list ubuntu bash
The other is via the command line with the -e option to the docker run command. you can stack the -e option to pass more than one environment variable.
one of the things you have in your dockerfile is the ability to declare the entry item. with that you can do something like:
set environment data via the docker run cmdline (via info above)
in the script get the environment info
finally, use it in the script to modify whatever file contains the uri data using something like sed
I build a RoR app on Heroku that must be run inside a Docker container. To do so I use the official Dockerfile. As it is very common with Heroku, I need a few add-ons to make this app fully operational. In production the variable DATABASE_URL is available within my app. But if I try some other add-ons that use environment variables (Mailtrap in my case), variables aren't copied into the instance during runtime.
So my question is simple: how can I make docker instances aware of the environment variables when executed on Heroku?
As you may ask, I already know that we can specified an environment directive right in docker-compose.yml. I would like to avoid that in order to be able to share this file through the project repository.
I didn't find any documentation about it but t appears that Heroku change very recently the way it handles config vars in Docker containers: they are now replicated automatically (values from docker-compose.yml are simply ignored).
The workaround to not commit sensitive config files, would be to create a docker-compose.yml**.example** with empty fields and commit it, then add docker-compose.yml to .gitignore.
Since that's not very practical on heroku, you can use the --env docker switch to add any variable to the container's environment.
Like this: docker run --env "MY_VAR=yolo" my_image:my_tag
You could also serve a private docker-config.yml from a secure site, that heroku would have access to (that would be my preferred solution in your case).