How to echo environment variables in Docker

时间:2016-08-31 12:29:14

标签: docker docker-compose

My docker-compose.yml is:

version: '2'

volumes:
  postgres_data: {}
  postgres_backup: {}

services:
  postgres:
    build: ./compose/postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - postgres_backup:/backups
    env_file: .env

  django:
    build:
      context: .
      dockerfile: ./compose/django/Dockerfile
    user: django
    depends_on:
      - postgres
      - redis
    command: /gunicorn.sh
    env_file: .env

  nginx:
    build: ./compose/nginx
    depends_on:
      - django

    ports:
      - "0.0.0.0:80:80"


  redis:
    image: redis:latest
    restart: always

And in my .env file, I have:

# PostgreSQL
POSTGRES_PASSWORD=mysecretpass
POSTGRES_USER=postgresuser

How do I test if the environment variables are effectively set?

I've tried tu run on the remote machine:

docker run sorbetcitron_django echo $POSTGRES_USER

where sorbetcitron_django is my django image, but it outputs nothing.

1 个答案:

答案 0 :(得分:4)

我使用:

docker-compose run postgres env

如果您将$ POSTGRES_USER传递给您的cli,它将被主机上的shell解释,如果您逃脱$,您需要评估该行让shell解析容器内的$