尝试执行"docker up"时获取"FATAL: role "根" does not exist"



我开始与使用Docker的现有Rails项目一起工作。我使用了很长一段时间,但从未停放。

docker build .后,我尝试做docker-compose up,但我得到:

致命:角色"根"不存在 /USR/local/bundle/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql_adapter.rb:661:在连接中的救援':fatal:fatal:coole" root"不存在(acteverecord :: nodataTabaseError art)/p>

在我看来,Docker机器可能正在尝试将数据库作为root用户连接到数据库,但是没有角色称为root,因此连接正确。

我不知道的是为什么Docker显然试图以root的形式连接到数据库以及如何使其使用合适的用户。

这是我的database.yml

development:
  database: my_app_development
  adapter: postgresql
  encoding: unicode
  pool: 5

任何帮助都将不胜感激。

编辑:这是我的docker-compose.yml

web:
  build: .
  volumes:
    - .:/my_app
  ports:
    - "3000:3000"
  links:
    - postgres
    - redis
    - mailcatcher
  env_file:
    - 'config/application.yml'
postgres:
  image: postgres:9.4
  ports:
    - "5432"
  env_file:
    - 'config/database.yml'
redis:
  image: redis:3.0.6
mailcatcher:
  image: schickling/mailcatcher
  ports:
    - "1080:1080"

Postgres Image期望提供POSTGRES_USERPOSTGRES_PASSWORDPOSTGRES_DB。否则,它将使用默认值。在项目的根目录中创建一个.env文件:

# .env file
POSTGRES_USER=testuser
POSTGRES_PASSWORD=testpass
POSTGRES_DB=db_development

并将您的 docker-compose文件更改为:

  web:
    build: .
    volumes:
      - .:/my_app
    ports:
      - 3000:3000
    depends_on:
      - postgres
      - redis
      - mailcatcher
  postgres:
    image: postgres:9.4
    ports:
      - 5432:5432
    env_file: .env
  redis:
    image: redis:3.0.6
  mailcatcher:
    image: schickling/mailcatcher
    ports:
      - 1080:1080

您还可以在没有.env文件的情况下提供环境变量:

  web:
    build: .
    volumes:
      - .:/my_app
    ports:
      - 3000:3000
    depends_on:
      - postgres
      - redis
      - mailcatcher
  postgres:
    image: postgres:9.4
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=testuser
      - POSTGRES_PASSWORD=testpass
      - POSTGRES_DB=db_development
  redis:
    image: redis:3.0.6
  mailcatcher:
    image: schickling/mailcatcher
    ports:
      - 1080:1080

并更新您的database.yml文件:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['POSTGRES_USER'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>
  host: postgres
development:
  <<: *default
  database: db_development
test:
  <<: *default
  database: db_test
production:
  <<: *default
  database: db_production
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 25 } %>

警告:不要使用links,它将很快删除

您可能需要如下更新组合和数据库YML。使用database.yml中的预期数据库用户和密码。另外,您可以使其成为环境变量。但是请尝试以下的默认值DB Docker映像;

database.yml

development:
  database: my_app_development
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres
  password:
  host: postgres(db name in docker-compose.yml)

docker-compose.yml

      web:
        build: .
        command: bundle exec rails s -p 3000 -b '0.0.0.0'
        volumes:
          - .:/my_app
        ports:
          - "3000:3000"
        links:
          - postgres
          - redis
          - mailcatcher
      postgres:
        image: postgres:9.4
        ports:
          - "5432"
      redis:
        image: redis:3.0.6
      mailcatcher:
        image: schickling/mailcatcher
        ports:
            - "1080:1080"

我不认为你要保留

   env_file:
      - 'config/database.yml'

   env_file:
      - 'config/application.yml'

然后使用docker-compose run web rake db:create

创建数据库

我添加了命令说明,因为我不知道您的Dockerfile是什么样的。但是,如果您使用docker build -t app-name .成功构建了应用程序映像,则可以将其删除,只运行docker-compose up

database.yml中没有明确的用户设置的情况下,它将尝试使用root用户,因为这与容器中的用户Postgres在容器中运行。要解决此问题,请尝试将database.yml设置为:

development:
  database: my_app_development
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres

请注意添加用户名字段。

带有docker-compose,它试图在运行之间保留体积;由于用户仅在新数据库上创建,因此您可能需要docker-compose rm -v database删除容器和关联的卷。

再次尝试docker-compose up --build

来源:https://github.com/docker-library/postgres/issues/41#issuecomment-167603905

相关内容

最新更新