简体   繁体   中英

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?

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 .

This is usually a problem with the Active Record configuration, not the database itself.

You can verify that your database has available connections by running heroku pg:info from the command line and looking at the connection count compared to the maximum count for your plan.

This error is usually caused when the Active Record pool size is set too low or when trying to share a small number of database connections across a larger number of worker processes.

You can play around with this:

pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 1 } %>

As far as this part of the quesstion is concerned:

If my postgres container is running at 0.0.0.0:15432 then my rails container should be connect to it also at 0.0.0.0:15432, right?

Yes you have to use:

 docker-host-ip:15432 

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

# .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
...
...

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

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