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.
CodePudding user response:
.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.