dockerdocker-compose

How to echo environment variables in Docker


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.


Solution

  • I'd use:

    docker-compose run postgres env
    

    If you pass a $POSTGRES_USER to your cli, it's going to get interpreted by the shell on the host, and if you escape the $, you'll need to eval the line to get the shell to parse the $ inside the container.