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.
Configure Compose using environment variablesSeveral environment variables are available for you to configure the Docker Compose command-line behavior.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With