简体   繁体   中英

Rails app with docker-compose does not read .env file

I'm trying to run a Rails app locally built with docker-compose. I'd like to keep all my config vars within .env and have docker-compose read from this file. However I continually run into the error when attempting this:

 ActiveRecord::ConnectionNotEstablished (FATAL:  password authentication failed for user "postgres")

My docker-compose file looks like this:

version: "3.9"
services:
  db:
    container_name: postgres
    image: postgres
    volumes:
      - postgres-data:/var/lib/postgresql/data 
    ports:
      - "5432:5432"
    env_file: 
      - ".env"
  web:
    container_name: puma
    stdin_open: true
    tty: true
    build: 
      context: .
      dockerfile: Dockerfile
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    env_file:
    - ".env"            
    ports:
      - "3000:3000"
    depends_on:
      - db
volumes: 
  postgres-data:
    

When I run docker-compose config I can see that docker-compose is correctly reading the POSTGRES_PASSWORD config I've set:

$ docker-compose config
services:
  db:
    container_name: postgres
    environment:
      POSTGRES_PASSWORD: admin
    image: postgres
[...]

My .env lives at the root of my project directory, just like docker-compose.yml and looks like this (minus two other key value pairs that are redacted here):

POSTGRES_PASSWORD=admin

Lastly, my database.yml file looks like this:

default: &default
  adapter: postgresql
  encoding: unicode
  database: myapp_db
  host: db
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  username: postgres
  password: ENV["POSTGRES_PASSWORD"]

Some other points in my attempts at troubleshooting this:

  • I've started an interactive byebug session within my web container and can confirm that my Rails app (running in the web ) container is returning the correct value of POSTGRES_PASSWORD .
  • When I explicitly set my docker-compose.yml to include the password as seen below, the app authenticates to the DB just fine. That said, I'd really prefer to use a .env file, even for test and development use cases:
[...]
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: admin
[...]
  • I'm using the dotenv gem to load my environment variables into Rails.
  • I've torn down (via docker-compose down --volumes) and rebuilt the container (`docker-compose build --no-cache) in various attempts to get this working - with no luck.

tl;dr 👉 I feel like I've tried nearly everything to get Rails to authenticate to Postgres when using docker-compose, but just can't get it working. I'm probably missing something obvious here so I'd love another pair of 👀

You forgot to wrap ENV["POSTGRES_PASSWORD"] with <%= %> on the database.yml .

You have

password: ENV["POSTGRES_PASSWORD"]

You need

password: <%= ENV["POSTGRES_PASSWORD"] %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM