How to set Environment Variables from server in Docker Compose? - docker

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.

Related

Kubernetes Access Windows Environment Variable

How i can access or read windows environment variable in kubernetes. I achieved the same in docker compose file.
How i can do the same in kubernetes as i am unable to read the windows environment variables?
Nothing in the standard Kubernetes ecosystem can be configured using host environment variables.
If you're using the core kubectl tool, the YAML files you'd feed into kubectl apply are self-contained manifests; they cannot depend on host files or environment variables. This can be wrapped in a second tool, Kustomize, which can apply some modifications, but that explicitly does not support host environment variables. Helm lets you build Kubernetes manifests using a templating language, but that also specifically does not use host environment variables.
You'd need to somehow inject the environment variable value into one of these deployment systems. With all three of these tools, you could include those in a file (a Kubernetes YAML manifest, a Kustomize overlay, a Helm values file) that could be checked into source control; you may also be able to retrieve these values from some sort of external storage. But just relaying host environment variables into a container isn't an option in Kubernetes.

Where do we get the list of environment variable for NiFi Docker

I'm a beginner in NiFi setup. I'm planning to start a NiFi cluster on Kubernetes. In normal installation, I saw that, we can change the NiFi configurations under the file 'nifi.properties'. But, when it comes to docker image, I also saw that we can change that by using environment variables. In most of the cases, the properties mentioned in the nifi.properties file can be easily converted into its equivalent environment variable.
Eg:
nifi.web.http.host <=> NIFI_WEB_HTTP_HOST
But in some cases, the environment variable is different. Eg:
nifi.zookeeper.connect.string != NIFI_ZK_CONNECT_STRING
From where do we get the full list of NiFi environment variable for Docker image. Any help like links or directions is very much appreciated.
You need to look into the documentation (or the source code) of the NiFi docker images your are using. For example agturley/nifi and apache/nifi.
When you enter the docker container you can see secure.sh and start.sh under the path /opt/nifi/scripts. These are the scripts that make all prop_replace

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

Put applications's public URL in its Docker Compose environment

I have a Python API that has to know its public address to properly create links to itself (needed when doing paging and other HATEOAS stuff) in the responses it creates. The address is given to the application as an environment variable.
In production it's handled by Terraform, but I also have extensive local tests that make use of Docker Compose. In tests for paging I need to be aware of the fact that I'm running locally and I need to replace the placeholder address I'm putting in the app's env with http://localhost:<apps_bound_port> for following the links.
I don't want to do that. I'd like to have a way to put the port assigned by Docker in the app's environment variables. The problem wouldn't be there if I was using fixed ports (then I could just put something like http://localhost:8000 in the public addres variable), because I can have multiple instances of Compose running, which wouldn't work then.
I know I can pass environment variables from the shell running docker-compose to the containers, but I don't know of a way to insert the generated port using this approach.
Only solution that I have for my problem now is to find a free port before Compose runs, and then pass it as an environment variable (API_PORT=<FREE_PORT> docker-compose up), while setting up the port like this in docker-compose.yml:
ports:
- "8000:${API_PORT}"
This isn't ideal, because I run Compose both from the shell (with make) and from Python tests, so I'd need to put the logic for getting the port into an env variable in both places.
Is there something I'm missing, or should I create a feature request for Docker Compose?

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