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?