ruby-on-rails - dockerizing Ruby on Rails 应用程序时的 db 问题
问题描述
我正在尝试对现有的 rails 应用程序进行 dockerize 处理。但是在尝试调用时遇到了错误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"]
码头工人-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:
数据库.yml
...
development:
<<: *default
database: app_development
# url: postgres://...
username: postgres
password: postgres
我究竟做错了什么?我已经在网上尝试了所有方法,但仍然没有运气。有什么收获吗?提前致谢。
解决方案
我认为您将需要通过 TCP/IP 而不是套接字进行通信,因此您应该能够使用 database.yml 文件中的主机设置对其进行排序
...
development:
<<: *default
username: postgres
database: postgres
host: 127.0.0.1
推荐阅读
- android - 为什么每当我尝试将数据类的元素添加到 RecyclerView 时应用程序崩溃
- python - \在python中做了什么?
- c# - C#/XML/Parsing/比较不同程序保存的文件位置,一种在uri中使用localhost
- tensorflow - 训练SSD-MOBILENET V1,损失不减
- excel - read_excel 在 index_col=none 时使用 pandas 创建索引
- python-3.x - 如何使用 mapbox 创建一个等值线地图并在国家一级进行绘图?
- swift - 快速解码对象数组
- python - 循环时将数据框的每一列保存在新的数据框中
- javascript - 如何使用 styled-components 更改其他组件中单个组件的样式?
- javascript - 结果低于输入