'How to connect to a PostgreSQL container with Rails

I have a Docker-file which is declaring my Ruby on Rails application with nginx and a PostgreSQL database.

I'm struggling to understand how my Rails application connects to my PostgreSQL container, because it doesn't connect at all.

I built the Docker-file linking the images:

web:
  build: .
  command: bundle exec puma -C config/puma.rb
  volumes:
    - /tmp:/tmp
    # - /log:/data
    - .:/my_project_folder
  links:
    - nginx
    - db

nginx:
  build: ./nginx
  volumes:
    - /tmp:/tmp
  ports:
    - 80:80

db:
  image: postgres
  environment:
      POSTGRES_PASSWORD: "Postgres2019!"
  ports:
    - "15432:5432"
  volumes:
    - /Users/fred/Documents/Postgres/data:/var/lib/postgresql/data

and configured my database.yml like:

default: &default
  adapter: postgresql
  encoding: unicode
  host: 0.0.0.0
  port: 15432
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: postgres
  password: Postgres2019!
development:
  <<: *default
  database: mos_development

But whenever I try to run my application, Rails can't connect to the database returning

ActiveRecord::ConnectionTimeoutError

but when I check my containers everything is running fine:

CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                     NAMES
1cfbc010829a        project_web           "bundle exec puma -C…"   19 minutes ago      Up 13 minutes                                 project_web_1
1774e2b89452        postgres              "docker-entrypoint.s…"   20 minutes ago      Up 13 minutes       0.0.0.0:15432->5432/tcp   project_db_1
0ce7dbb6d735        project_nginx         "nginx -g 'daemon of…"   3 hours ago         Up 13 minutes       0.0.0.0:80->80/tcp        project_nginx_1

If my PostgreSQL container is running at 0.0.0.0:15432, then my Rails container should be able to connect to it also at 0.0.0.0:15432, right?



Solution 1:[1]

Try this:

default: &default
  adapter: postgresql
  encoding: unicode
  host: db    # <-- change this line
  port: 15432
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: postgres
  password: Postgres2019!
development:
  <<: *default
  database: mos_development

The Rails application is linked to db, db:15432.

Solution 2:[2]

If you have too many environment variables, you can set them in a env files. e.g.

# .env.docker
RAILS_MAX_THREADS=4
USERNAME=posgres
...
...

Then specify the .env.docker file in your docker-compose file by env_file:

db:
  image: postgres
  env_file: .env.docker
  environment:
      POSTGRES_PASSWORD: "Postgres2019!"
  ports:
    - "15432:5432"
  volumes:
    - /Users/fred/Documents/Postgres/data:/var/lib/postgresql/data
...
...

Solution 3:[3]

I had a same issue. Try this

default: &default
  adapter: postgresql
  encoding: unicode
  host: db # change this line
  port: 5432 # change this line too, use internal port
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: postgres
  password: Postgres2019!
development:
  <<: *default
  database: mos_development

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 the Tin Man
Solution 2 Daniel
Solution 3