Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 476
bixente57 Avatar asked Aug 31 '16 12:08

bixente57


People also ask

How can I see docker environment variables?

Fetch Using docker exec Command Here, we are executing the /usr/bin/env utility inside the Docker container. Using this utility, you can view all the environment variables set inside Docker containers.

How do you echo an environment variable?

Select Start > All Programs > Accessories > Command Prompt. In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable.

How do I pass environment variables to docker containers?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...

Can I use environment variables in Dockerfile?

Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.


1 Answers

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.

like image 189
BMitch Avatar answered Oct 30 '22 19:10

BMitch