Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose environment variables

I am trying to setup a postgres container and want to setup the postgres login with:

POSTGRES_USER: docker POSTGRES_PASSWORD: docker 

So I have created the docker-compose.yml like so

web:   build: .   ports:     - "62576:62576"   links:    - redis    - db db:   image: postgres   environment:     POSTGRES_PASSWORD: docker     POSTGRES_USER: docker  redis:    image: redis 

I have also tried the other syntax for environment variable declaring the db section as:

db:   image: postgres   environment:    - POSTGRES_PASSWORD=docker    - POSTGRES_USER=docker 

However neither of these options seem to work because for whatever reason whenever I try to connect to the postgres database using the various connection strings:

postgres://postgres:postgres@db:5432/users postgres://postgres:docker@db:5432/users postgres://docker:docker@db:5432/users 

They all give me auth failures as opposed to complaining there is no users database.

like image 618
royalaid Avatar asked Apr 11 '15 17:04

royalaid


People also ask

Can you use environment variables in docker-compose?

Configure Compose using environment variablesSeveral environment variables are available for you to configure the Docker Compose command-line behavior.

How pass env variable in docker-compose command?

Set environment variable with the --env-file flag If you want to pass a file containing all of your environment variables to a Docker container, you can use the --env-file flag. The --env-file flag allows you to pass a file containing environment variables to a Docker container.

Does docker-compose override environment variables?

But docker-compose does not stop at the . env and the host's current environment variables. It's cool that you can simply override values of your . env file, but this flexibility is can also be the source of nasty bugs.

How do I set an environment variable in docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.


1 Answers

I struggled with this for a while and wasn't having luck with the accepted answer, I finally got it to work by removing the container:

docker-compose rm postgres 

And then the volume as well:

docker volume rm myapp_postgres 

Then when I did a fresh docker-compose up I saw CREATE ROLE fly by, which I'm assuming is what was missed on the initial up.


The reasons for this are elaborated on here, on the Git repo for the Docker official image for postgres.

like image 51
davetapley Avatar answered Oct 06 '22 05:10

davetapley