How can I configure environment variables within docker-compose

1

Greetings colleagues, I am trying to configure a file docker-compose.yml , with the following commands:

nginx:
  restart: always
  build: ./nginx/
  ports:
    - "80:80"
  volumes:
    -  /www/static
  volumes_from:
    - web
  links:
    - web:web

version: '2'
services:
  postgres:
    env_file: .env_development
    restart: always
    build: ./postgres
    ports:
      - "5432:5432"
    expose:
      - "5432"
    tty: true
    volumes:
      - /var/lib/postgresql/data/
    networks:
      app_net:
        ipv4_address: ${POSTGRES_IP}


  web:
    env_file: .env_development
    restart: always
    build: ./web
    tty: true
    ports:
      - "8000:8000"
    links:
      - postgres:postgres
    volumes:
      - /home/kalismash/Docker/app_django/web/app:/home/julian_develop/apps
    networks:
      app_net:
        ipv4_address: ${DJANGO_IP}
    command: python manage.py runserver 0.0.0.0:8000

networks:
  app_net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.0.0/24

In addition to the file I have another file .env which contains all the environment variables, it is as follows:

#Set of enviroments global
LANG=es_CO.utf8
SHELL=/bin/bash
SHELL2=/bin/sh
TERM=xterm


#Postgress environment

PG_SERVICE=postgres
PGDATA=/var/lib/postgresql/data
POSTGRES_USER=develop_julian
POSTGRES_PASSWORD=postgress
POSTGRES_DB=django
ENVIROMENT=Development
ENVPORT=5432
POSTGRES_IP=172.25.0.9

#Django environment

USER=django_develop
SECRET_KEY=6g_,p?@$/t+Kgfy8c=BC*zQPNM*eJ,qZ&*@iL>9Q
DJANGO_IP=172.25.0.8

When I try to run docker-compose up web postgres I get the following warning:

  

WARNING: The POSTGRES_USER variable is not set. Defaulting to a blank   string WARNING: The IP_POSTGRES variable is not set. Defaulting to a   blank string.

I tried to follow the documentation of docker Docker enviroment variables but I can not get it to work properly, plus I tried to do it for args , because docker warns about the process of build

  

Note: If your service specifies a build option, variables defined in   environment files will not be visible visible during the build.   Use the args sub-option of build to define build-time environment   variables

But I also can not configure environment variables, if someone has faced this problem I would appreciate your help, thank you very much colleagues.

    
asked by julian salas 18.10.2016 в 04:53
source

1 answer

1

You must include the env property: In your docker-compose file and define the .env file there. The .env file must be at the same level as the docker-compose.yml . Within the .env file, you define the variables in the traditional way, VAR=VALUE; . Then, within docker-compose you just call the variable.

Something like this:

  

Here you define the env file :

version: '2'    
services:
  postgres:
    env_file: .env
  

Assuming that your working directory is $ HOME , you should place your .env file there:

cat $HOME/.env
VAR=foo
  

Remember that docker-compose.yml must be at the same level, that is, in $ HOME as well. So, you can use your variables, in the compose file:

version: '2'    
services:
  postgres:
    env_file: .env
    container_name: "$VAR"
    
answered by 27.01.2017 в 16:42