I created Dockerfile to ruby from official docker-compose website. I used docker compose to integrate this with Postgres. When docker-compose up, then PostgreSQL starts but I cannot connect to this. The main problem is in ruby database config because I can't change to listen to another host and set the default user. I don't know how to connect these two containers without changing the ruby database config.
Dockerfile
FROM ruby:2.3.4
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp
Docker-compose
version: '3'
services:
db:
image: postgres:9.6
ports:
- "5432:5432"
web:
build: .
ports:
- "4000:4000"
depends_on:
- db
Ruby database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 5
development:
<<: *default
database: project_development
If i run inside web conatiner "bundle exec rake db:create I get error:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I don't know what env set that will connect together.
You didn't specify database host in your database.yml. Set it to db or just try this database.yml
development: &default
adapter: postgresql
encoding: unicode
database: project_development
pool: 5
username: postgres
password:
host: db
test:
<<: *default
database: project_test
You should update your database.yml with the following:
database: ENV['DB_NAME']
username: ENV['DB_USERNAME']
password: ENV['DB_PASSWORD']
There's no such thing as "default PG user", so you have to create your own user with a proper permissions.
So, once you have a PG user created inside of container, you can update your docker-compose.yml file with a proper DB settings:
web:
environment:
- DB_NAME=rails_development
- DB_USERNAME=rails_user
- DB_PASSWORD=rails_password
Also, you probably want to mount local storage of postgres in your docker-compose.yml to keep data when container restarts:
volumes:
- /var/lib/postgresql/data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With