【问题标题】:db issues when dockerizing Ruby on Rails applicationdockerizing Ruby on Rails 应用程序时的 db 问题
【发布时间】:2021-07-03 07:01:15
【问题描述】:

我正在尝试对现有的 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"]

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:

数据库.yml

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

我做错了什么?我已经在网上尝试了所有方法,但仍然没有运气。有什么收获吗?提前致谢。

【问题讨论】:

  • 您的数据库 url 使用什么?您是否按名称引用容器?我已经让它在许多项目中工作,但我通常公开数据库的端口(postgres 为 5432)并使用localhost 将两者绑定在一起。我相信你可以只使用容器名称。

标签: ruby-on-rails postgresql docker docker-compose dockerfile


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    相关资源
    最近更新 更多