How to read environment variables from YAML file in docker? - python-3.x

I'm having a .env file that contains variables
VALUE_A=10
NAME=SWARNA
I'm having an env.yml file.
Information:
value: $VALUE_A
name: $NAME
I'm having a python file envpy.py
from envyaml import EnvYAML
# read file env.yml and parse config
env = EnvYAML('env.yml')
print(env['Information']['value'])
print(env['Information']['name'])
Here is my Dockerfile
FROM python:3
ADD envpy.py /
ADD env.yml /
RUN pip install envyaml
CMD [ "python", "./envpy.py" ]
Expected output:
10
SWARNA
But I got :
VALUE_A
NAME
I'm using the commands to build the docker and run:
docker build -t python-env .
docker run python-env
How to print the values. Please correct me or suggest me where I'm doing wrong. Thank you.

.env is a docker-compose thing, which defines default values for environment variables to be interpolated into docker-compose.yml, and only there. They are not available anywhere else and certainly not inside your image.
You can make the values available as environment variables inside your image by copying .env into the image and in your Python code do
from dotenv import load_dotenv
load_dotenv()
(which requires you to install dotenv).
Also mind that there are probably better ways to achieve what you want:
If the values must be set at build time, you'd rather interpolate them into the resulting file at build time and copy the file with the hardcoded values into the image.
If the values should be overridable at runtime, just define them via ENV with a default value inside the Dockerfile.

Related

How to load external config file in Docker?

I am using Docker to build a Node app image. I am having my configurations in a YAML file which is located at source_folder/config.yaml.
When doing await readFile(new URL('../config.yaml', import.meta.url), 'utf8') in my index file it says file not found after running. However, doing - COPY config.yaml ./ in Dockerfile solves it but I don't want to copy my credentials in the image build.
Is there any solution I can load the config file after building the image?
Using ESM.
I use dotenv to load my env variables. I understand the need to not include it in builds. Docker provides a runtime solution of including these variables to your env by passing the file as an argument. So this is what I do to load my env while using docker run:
docker run -e VARIABLE_NAME=variable_value image_to_be_executed
# or
docker run --env-file path_to_env_file image_to_be_executed

Setting environmental variables for node from host machine for build server

I'm using bitbucket pipelines as a build server.
I need to pass environmental variables from a host machine into a .env file which will then set the var values to be used in the build.
For example, lets say an environmental variable in a docker container running the build is AWS_ACCESS_KEY_ID.
In my .env file I'd like something like the following:
ACCESS_KEY=${AWS_ACCESS_KEY_ID}
I would then run the build and the ACCESS_KEY var would have a value equal to the env var in the docker container.
My current idea for a solution right now involves replacing values with sed, but that feels pretty hacky. Example:
.env file contains the following line:
ACCESS_KEY=<_access_key_replace_me_>
sed "s/<_access_key_replace_me_>/${AWS_ACCESS_KEY_ID}/g" .env
Any better solution than this?

how to setup multiple paths in user environmental variables in windows 10

I want to set up multiple paths in the user environmental variables in windows 10. but can only set a single path, how to overcome this issue?
If you want to add user defined environment variable then use react-native-config library. So in short if there are variables defined in .env file then you can use it anywhere in the react-native app like below:-
.env file
API_URL = "https://baseurl/endpoint"
//To use the above environment variable
import config react-native-config
function apiCall() { axios(config) }
With the help of react-native-config you can use environment variable in native code also and vise-versa.
For better use define .env.staging file. By default config lib finds .env file. To run the application with .env.staging file see below command.
$ ENVFILE=.env.staging react-native run-ios # bash
$ SET ENVFILE=.env.staging && react-native run-ios # windows
$ env:ENVFILE=".env.staging"; react-native run-ios # powershell
For more information checkout :- https://github.com/luggit/react-native-config
Happy coding mate :)
to add multiple paths for a single environment variable, list them all with a semicolon(";") in between like that:
C:\Cpp_Headers\pybind11;C:\Cpp_Headers\cpython\Include;C:\Users\admin\Cpp_Headers\cpython\PC
(and so forth). From this input Windows will automatically recognize multiple definitions.

Variable substitutions in docker-compose in Azure Docker Web app

I have a docker-compose file that contains a secret for my database.
port: 4466
managementApiSecret: ${DB_SECRET}
So I would like to use Docker-Compose's Variable Substitution (https://docs.docker.com/compose/compose-file/#variable-substitution) to be able to provide my secrets through an environment variable instead of my yaml file.
Is this at all possible using the "Application Settings" variables on my Azure Linux Docker Instance?
It doesn't seem to work for me, I've tried both ${VAR} and $VAR syntax and neither worked. I also tried with secrets that only contain alphanumerics and numbers.
Thanks in advance.
Frank
Environment variables that you need to start the container (for example: you want to include build number in container name) can be added to .env file in same directory as the docker-compose.yml file.
sample .env file:
DB_SECRET=foo
run: docker-compose config and verify that the variable has been substituted with the value you have in .env file
Also, I recommend using managementApiSecret:"${DB_SECRET}" (note the quotes around the variable) in your docker-compose.yml file
There might be azure specific way to share secrets but I didn't try that yet.
If you want to pass in environment variables that the container needs, then https://docs.docker.com/compose/compose-file/#env_file is what you want. Those environment vars will become part of environment inside docker container.

Application cannot access env variables while they are available on a container level

node.js application, built and deployed using docker-compose, doesn't see any custom set variables. console.log(process.env.VAR) logs undefined to console for any of those.
Variables are set using env_file property in yaml file. Only env_file property is used. There is an ENV value set in Dockerfile and it's accessible by the application.
docker exec -it <container-id> envdoes return all custom values. docker exec -it <container-id> sh returns only those set in base image - node-alpine (wiped out by exec?).
What can be wrong with the setup?
I've found that the issue is not with the compose file or wrong usage of env_file field.
The issue was with the env file itself. It used spaces when setting values. Like this: VAR = VAL instead of VAR=VAL.
While dotenv npm package allows this (I've used a sample that comes with a project as a base for deployment), docker and environment don't.

Resources