Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose not setting environment variables with flask

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

like image 470
Mezin Avatar asked Jun 06 '17 14:06

Mezin


1 Answers

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"])
like image 52
German Avatar answered Nov 17 '22 21:11

German