How do you reload environment file in a docker swarm service? - docker

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.

Related

How to access container environment variables with kubernetes env?

I'm trying to run my application on Kubernetes. My docker container have environment variables such as PATH and LD_LIBRARY_PATH, which are set in Dockerfile. I tried to change them in the yaml file like this:
env:
- name: LD_LIBRARY_PATH
value: "foo:$(LD_LIBRARY_PATH)"
The above configuration doesn't work, I just see LD_LIBRARY_PATH=foo:$(LD_LIBRARY_PATH) in the pod. This method seems work for Kubernetes env variables such as KUBERNETES_PORT_443_TCP_PROTO, but not for docker env variables.
My questions are:
I think the env settings in yaml are injected into docker before the running time of the container, so kubernetes cannot read the value of LD_LIBRARY_PATH. Therefor it can't change the variable. Do I understand it right?
How to change container environment variables with kubernetes env? I know that I can set the env variables in the command field of yaml file, but that seems not clean and are there other ways to do that?
If Kubernetes can't change existed envs, does it mean that the env field in yaml file is designed to add new envs only?
Thank you!
The Kubernetes variable expansion syntax only works on things Kubernetes directly knows about. Inside a container an environment variable could come from a couple of places (the Dockerfile ENV directive, the base container environment itself, setup in an entrypoint script) and Kubernetes doesn't consider any of these; it only considers things in the same container spec. The API definition of EnvVar hints at this:
Variable references $(VAR_NAME) are expanded using the previous defined environment variables in the container and any service environment variables.
You can't use this Kubernetes syntax to change environment variables in the way you're describing. You can only refer to other things in the same env: block (which may come from ConfigMaps or Secrets) and the implicit variables that come from other Services.
(Changing path-type variables at the Kubernetes level doesn't make a lot of sense. Since an image is self-contained, it already contains all of the commands and libraries it would need. It's difficult in Kubernetes to inject more tools or libraries; it'd be better to install them directly in your image, ideally in /usr/lib or /usr/local/lib, but failing that you can update ENV in a Dockerfile similar to how you suggest here.)

aspnet core 2.2 web app environment variables not changing in docker

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 :-)

How to set Environment Variables from server in Docker Compose?

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.

.Net Core and Docker (Manage Application Settings)

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

Transmit Heroku environment variables to Docker instance

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).

Resources