Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to access environment variables from docker compose env file

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

    • config.py
    • app.py
    • requirements.txt
    • other files
  • 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,

  1. python=3.7
  2. python-dotenv==0.12.0

I want to fix this env reading issue. I am not sure where the problem is, @anyone pls help me to resolve this issue.

like image 459
CHIDAMBARANATHAN M Avatar asked Jul 17 '20 09:07

CHIDAMBARANATHAN M


People also ask

How to use environment variables in Docker Compose?

According to this docker manual, if environment variables are used in docker-compose.yml file like: Docker looks for those variables in a default .env file; So changing the default env file like docker-compose up --env-file imran.env would work.

Why Docker-Compose will not read the env file?

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 Show activity on this post. docker run will not find the env file because the env file is specified in the docker-compose.yml file.

How do I use Docker-Compose with the--env command?

By default, the docker-compose command will look for a file named .env in the directory you run the command. By passing the file as an argument, you can store it anywhere and name it appropriately, for example, .env.ci, .env.dev, .env.prod. Passing the file path is done using the --env-file option:

What is the path of the docker compose project directory?

The .env file path is as follows: Project directory can be explicitly defined with the --file option or COMPOSE_FILE environment variable. Otherwise, it is the current working directory where the docker compose command is executed ( +1.28 ). For previous versions, it might have trouble resolving .env file with --file or COMPOSE_FILE.


Video Answer


1 Answers

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
like image 73
Adiii Avatar answered Oct 17 '22 07:10

Adiii