2

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?

1
  • your rails container should not connect at 0.0.0.0:15432, it should either connect to db:5432 or <docker0-interface-ip-addr>:15432. also if you have a static docker host ip you can also use <docker-host-ip>:15432 Commented Feb 22, 2020 at 21:36

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 

2 Comments

That is correct, I had to change this value to 1 and also had to set the host do "db" as reported on the next answer
@Frederico, changed the value from 5 to 1.
0

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

Comments

0

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

Comments

0

docker-compose create

docker-compose run --rm web bin/rails db:create

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.