Heroku - Ruby on Rails - PG::ConnectionBad:无法将主机名转换为地址:名称或服务未知

Heroku - Ruby on Rails - PG::ConnectionBad: could not translate host name to address: Name or service not known

我正在尝试将我的 Docker 容器推送到 Heroku 中,但当我尝试这样做时我一直遇到此错误 heroku run rake db:migrate:

PG::ConnectionBad: could not translate host name "db" to address: Name or service not known

我一直在搜索并添加:POSTGRES_HOST_AUTH_METHOD: trust 到我的 docker-compose.yml 并且还在 docker-compose.ymldatabase.yml 中为环境保留了相同的名称,但是它一直报同样的错误,我很迷茫。

这是docker-compose.yml:

version: "3.8"

services:
  db:
    image: postgres
    environment:
      POSTGRES_HOST_AUTH_METHOD: trust
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  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
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_HOST: db
      RAILS_ENV: production

这是database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: user
  password: password
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: _rails_ecommerce_site_development

test:
  <<: *default
  database: _rails_ecommerce_site_test

production:
  <<: *default
  database: _rails_ecommerce_site_production

这些是来自 heroku logs --tail 的日志:

2022-04-19T11:07:38.574966+00:00 heroku[web.1]: Starting process with command `rails server -b 0.0.0.0`
2022-04-19T11:07:42.805650+00:00 app[web.1]: => Booting Puma
2022-04-19T11:07:42.805662+00:00 app[web.1]: => Rails 6.0.4.4 application starting in production
2022-04-19T11:07:42.805662+00:00 app[web.1]: => Run `rails server --help` for more startup options
2022-04-19T11:07:44.787345+00:00 app[web.1]: Puma starting in single mode...
2022-04-19T11:07:44.787369+00:00 app[web.1]: * Version 4.3.10 (ruby 2.6.6-p146), codename: Mysterious Traveller
2022-04-19T11:07:44.787370+00:00 app[web.1]: * Min threads: 5, max threads: 5
2022-04-19T11:07:44.787370+00:00 app[web.1]: * Environment: production
2022-04-19T11:07:44.787625+00:00 app[web.1]: * Listening on tcp://0.0.0.0:24849
2022-04-19T11:07:44.787902+00:00 app[web.1]: Use Ctrl-C to stop
2022-04-19T11:07:45.114352+00:00 heroku[web.1]: State changed from starting to up
2022-04-19T11:07:47.793790+00:00 heroku[router]: at=info method=GET path="/" host=xxxx.herokuapp.com request_id=0368ded8-ba57-474b-b540-4f23292881d1 fwd="119.105.11.94" dyno=web.1 connect=0ms service=109ms status=500 bytes=1827 protocol=https
2022-04-19T11:08:36.612997+00:00 app[api]: Starting process with command `rake db:migrate` by user xxxxx
2022-04-19T11:09:06.965267+00:00 heroku[run.7786]: State changed from starting to up
2022-04-19T11:09:07.057271+00:00 heroku[run.7786]: Awaiting client
2022-04-19T11:09:07.084913+00:00 heroku[run.7786]: Starting process with command `rake db:migrate`
2022-04-19T11:09:12.164888+00:00 heroku[run.7786]: State changed from up to complete
2022-04-19T11:09:11.892289+00:00 heroku[run.7786]: Process exited with status 1

这里有什么我遗漏的吗?我该如何解决?谢谢!

已解决!

所以,基本上我们需要在 Heroku 中添加 Postgres add-on。步骤在这里(https://betterprogramming.pub/how-to-containerize-and-deploy-apps-with-docker-and-heroku-b1c49e5bc070)但基本上:

  1. 导航到 Heroku 并单击您创建的应用程序。
  2. 在资源中,搜索“postgres”并单击 Heroku Postgres。
  3. 将其添加到您的应用中。单击提供。

而且非常重要,在项目的 database.ymlproduction 应该有这个:url: <%= ENV['DATABASE_URL'] %>。就我而言:

production:
  <<: *default
  database: _rails_ecommerce_site_production
  url: <%= ENV['DATABASE_URL'] %>

一旦我们添加 Postgresql add-on

,Heroku 将给出 <%= ENV['DATABASE_URL'] %>