Reputation: 131
I have a problem with reading the .env variables from a docker-compose.yml file (version -3.7), here first I explain my folder and sample code structure
My folder structure
my_app
src
Dockerfile
docker-compose.yml
.env
.env file
This is my sample .env file
ENVIRONMENT_NAME=DEV
DATABASE_NAME=testing
docker-compose.yml file
This is my sample docker-compose.yml file (version: 3.7)
version: "3.7"
services:
my_app_test:
env_file: ./.env
image: my-app-test
build:
context: ./src
dockerfile: Dockerfile
Dockerfile
This is my sample Dockerfile
FROM python:3.7-alpine
# Install required packages
RUN apk add --update --no-cache \
build-base \
postgresql-dev \
linux-headers \
pcre-dev \
py-pip \
bash \
curl \
openssl \
nginx \
libressl-dev \
musl-dev \
libffi-dev \
rsyslog \
&& pip install Cython
# Create container's working directory
RUN mkdir -p /MyApp
# Copy all source files to the container's working directory
COPY ./ /MyApp
# Install all python dependency libs
RUN pip install -r /MyApp/requirements.txt
WORKDIR /MyApp
ENTRYPOINT ["python3"]
src/config.py
Here in the config file, I am reading all the environment variables
import os
from pathlib import Path
from dotenv import load_dotenv
ENV_PATH = Path('.env')
load_dotenv(dotenv_path=ENV_PATH)
ENVIRONMENT_NAME = os.getenv('ENVIRONMENT_NAME')
DATABASE_NAME = os.getenv("DATABASE_NAME")
src/app.py
Here in the app.py file, I am getting the variables from config.py and use that in my project. When I run this file after building a docker image, it is not working as expected. All the env variables are read as None
import config
import os
print(config.ENVIRONMENT_NAME)
print(config.DATABASE_NAME)
# Access all environment variables
print('\n')
print(os.environ)
I am trying to access env sets from the docker-compose file and not from Dockerfile.
Docker build is working fine, After that when I try to run the docker image (sudo docker run my-app-test app.py) it does not print the environment variables as I expected. The output of the current code is,
None None environ({'PATH': '/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'HOSTNAME': '6cfc0c912772', 'LANG': 'C.UTF-8', 'GPG_KEY': '0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D', 'PYTHON_VERSION': '3.7.5', 'PYTHON_PIP_VERSION': '19.3.1', 'PYTHON_GET_PIP_URL': 'https://github.com/pypa/get-pip/raw/ffe826207a010164265d9cc807978e3604d18ca0/get-pip.py', 'PYTHON_GET_PIP_SHA256': 'b86f36cc4345ae87bfd4f10ef6b2dbfa7a872fbff70608a1e43944d283fd0eee', 'HOME': '/root'})
Requirements am using is,
I want to fix this env reading issue. I am not sure where the problem is, @anyone pls help me to resolve this issue.
Upvotes: 7
Views: 4858
Reputation: 2235
docker run
will not find the env
file because the env file is specified in the docker-compose.yml
file.
Try running your container like this instead:
docker-compose run my_app_test app.py
Upvotes: 0
Reputation: 59896
You are not setting ENV at build time, so docker-compose will able to read env file
as you set configuration in the compose file, but docker run
will not read the env
as you need to specify env file
You need to specify env-file
in docker run command
docker run --env-file .env my-app-test app.py
or to just check ENV
docker run --env-file .env --entrypoint printenv my-app-test
or run the stack
docker-compose up
Upvotes: 1