6

running flask container and when I try to read environment variables, they are not set by docker compose. I am Using docker-compose file version 2

compose file:
    services:
        test
             build: ./test
             image: test:1.0
             container_name: test_flask
             ports:
                  - "80:80"
             env_file: .env
             environment:
                  - COUCHDB=http://192.168.99.100:5984
             depends_on:
                  - couchdb

I have tried both with env_file and environment directives ? I have also tried to put the values in double quotes, single quotes, no quotes, none worked. the .env file contains:

  COUCHDB="http://192.168.99.100:5984", also tried without quotes

then I read the variables from python code like this:

COUCH_SERVER = os.environ["COUCHDB"]

I also tried os.environ.get('COUCHDB')

none worked.

The server is started from Dockerfile as this:

CMD service apache2 restart

I start the container with the command:

docker-compose up test

I am using Docker for Windows toolbox with version:

Client:
 Version:      1.13.1
 API version:  1.26
 Go version:   go1.7.5
 Git commit:   092cba3
 Built:        Wed Feb  8 08:47:51 2017
 OS/Arch:      windows/amd64

Server:
 Version:      17.04.0-ce
 API version:  1.28 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   4845c56
 Built:        Wed Apr  5 18:45:47 2017
 OS/Arch:      linux/amd64
 Experimental: false

Thank you for your help

Mezin
  • 61
  • 1
  • 3
  • What is your first line in your compose file? What is the version you are using? What is the output of `docker-compose config`? I copied your version, added `version: '2'` to the first line and saw the your environment variable – n2o Jun 06 '17 at 15:09
  • What do you see if you print all env vars? `docker-compose exec test env` – Robert Jun 06 '17 at 19:20
  • first line of docker-compose file is [version: '2'] docker-compose config : test: build: context: D:\dev\uploader container_name: test_flask depends_on: - couchdb environment: COUCHDB: http://192.168.99.100:5984 image: test:1.0 ports: - 80:80 version: '2.0' – Mezin Jun 06 '17 at 20:43

3 Answers3

7

I leave you an example of how to do to get the environment variables from the application.

docker-compose.yml

version: '2'

services:
  app:
      image: python:2.7
      environment:
        - BAR=FOO
      volumes:
        - ./app.py:/app.py
      command: python app.py

app.py

import os

print(os.environ["BAR"])
German
  • 1,449
  • 12
  • 13
  • thank you for the reply, unfortunately I do not understand what you mean by "the project is what you should do to get the values of the environment." – Mezin Jun 06 '17 at 19:03
0

I personally feel that the env variables must be set with a name and value prefixed under the environment section of your docker file. At least this is what I have worked with on Kubernetes deployment yaml

env: - name: CouchDB value: "Couch DB URL" - name: "ENV Variable 1" value: 'Some value for the variable'

The you could try to exec into the pod and try to echo out the environment variable just to be sure.

As you mentioned, accessing it throught os.environ in your python script should get you going.

0

I also have another example:
You can pass the env variable using the Dockerfile
Like This ENV SECRET="abcdefg"In the Dockerfile.
So the complete Dockerfile will look like this:

FROM python:3.7.9
LABEL maintainer_name="Omar Magdy"
COPY requirements.txt requirements.txt
ENV SECRET="abcdefg"
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
ENV FLASK_ENV=development
ENV FLASK_DEBUG=0
CMD ["flask", "run"]

Now in the docker-compose.yml:

version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - "the_data_base:/db"
volumes:
  the_data_base:
    external: true

Assuming that you have created an external volume called the "the_data_base", to store the data inside the external volume.
So, my solution suggests creating the environmental variable inside the Dockerfile, instead of inside the docker-compose file.
So you create the environmental variable at the moment of the creation of the container.
:) :)

Omar Magdy
  • 2,331
  • 14
  • 13