首页 > 解决方案 > 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

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

标签: ruby-on-railspostgresqldockerdocker-composedockerfile

解决方案


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

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

推荐阅读