数据库在对RubyonRails应用程序进行码头化时出现问题



我正在尝试对现有的rails应用程序进行码头化。但是它在尝试调用rake db:create时遇到了一个错误。

错误如下:

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"?
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, 
"database"=>"app_development", "username"=>"postgres", "password"=>"postgres"}
rake aborted!
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"?

这是我的Dockerfile

FROM ruby:2.7.2
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
RUN bundle install
COPY . /app
# COPY entrypoint.sh /usr/bin/
# RUN chmod +x /usr/bin/entrypoint.sh
# ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

version: "3.9"
services:
db:
image: postgres
volumes:
- db_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: app_development
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
volumes:
db_data:

database.yml

...
development:
<<: *default
database: app_development
# url: postgres://...
username: postgres
password: postgres

我做错了什么?我在网上什么都试过了,但还是没有成功。有渔获吗?提前谢谢。

我认为您需要通过TCP/IP而不是套接字进行通信,因此您应该能够使用数据库.yml文件中的主机设置对其进行排序

...
development:
<<: *default
username: postgres
database: postgres
host: 127.0.0.1

最新更新