first of all I'm using windows 11 with WSL2 ubuntu 20.04. I'm build an image with docker where are install ruby and postgresql.
docker-compose.yml
version: '3.9'
services:
db:
image: 'postgres:14.1-bullseye'
volumes:
- 'postgres:/var/lib/postgresql/data'
ports:
- '5432:5432'
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
redis:
image: 'redis:6.2.6-bullseye'
command: redis-server
ports:
- '6379:6379'
volumes:
- 'redis:/data'
web:
depends_on:
- 'db'
- 'redis'
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
ports:
- '3002:3000'
environment:
- DATABASE_HOST=db
volumes:
redis:
postgres:
ruby 3.1.2-bullseye
Everything works fine, I created a new project with rails new project-name --api --database=postgresql -T
and a docker compose build
works fine.
After that I want to create a database with docker compose run web rails db:create
and now problem appears.
I'm getting this error:
We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.
To resolve this issue:
- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.
To create your database, run:
bin/rails db:create
Couldn't create 'greenhouse_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.
To resolve this issue:
- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.
To create your database, run:
bin/rails db:create
Caused by:
PG::ConnectionBad: 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"?
Tasks: TOP => db:create
(See full trace by running task with --trace)
I don't know what is going on, for help and sign what I'm doing wrong thansk in advance.
UPDATE
database.yml
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: <%= ENV.fetch("DATABASE_HOST") { "db" } %>
port: <%= ENV.fetch("DATABASE_PORT") { 5432 } %>
username: <%= ENV.fetch("DATABASE_USERNAME") { "postgres" } %>
password: <%= ENV.fetch("DATABASE_PASSWORD") { "password" } %
development:
<<: *default
database: greenhouse_development
test:
<<: *default
database: greenhouse_test
production:
<<: *default
database: greenhouse_production
username: greenhouse
password: <%= ENV["GREENHOUSE_DATABASE_PASSWORD"] %>
CodePudding user response:
I vaguely recall there being some wonkiness around loading env vars in db config files. You can initialize rails without a DB connection while docker is building if you use the nulldb adapter:
# database.build.yml
build:
adapter: nulldb
FROM somebase as build
...
RUN cp ./config/database.build.yml ./config/database.yml
&& RAILS_ENV=build bundle exec rake assets:precompile
CodePudding user response:
Unfortunately, I did not solve this problem directly. To generally solve this problem I downloaded a sample rails 7 docker repository and changed only API under my own. Here is the link to the repository I used https://github.com/ryanwi/rails7-on-docker