【问题标题】:Docker-compose rails postgresDocker-compose rails postgres
【发布时间】:2017-06-02 01:11:31
【问题描述】:

我一直在关注this tutorial 来“dockerize”我的 rails 应用程序,经过一番搜索后,我在连接数据库时遇到了麻烦,似乎没有解决方案有效。我也尝试了默认用户'postgres'并且没有密码,但仍然没有运气。我的错误表明我的密码不正确,但我尝试的一切都不会改变错误:

web_1       | I, [2017-06-02T00:58:29.217947 #7]  INFO -- : listening on addr=0.0.0.0:3000 fd=13
postgres_1  | FATAL:  password authentication failed for user "web"
postgres_1  | DETAIL:  Connection matched pg_hba.conf line 95: "host all all 0.0.0.0/0 md5"
web_1       | E, [2017-06-02T00:58:29.230868 #7] ERROR -- : FATAL:  password authentication failed for user "web"

这是我所拥有的:

.env

LISTEN_ON=0.0.0.0:3000
DATABASE_URL=postgresql://web:mypassword@postgres:5432/web?encoding=utf8&pool=5&timeout=5000

Dockerfile

FROM ruby:2.3.4
RUN apt-get update && apt-get install -qq -y build-essential nodejs libpq-dev postgresql-client-9.4 --fix-missing --no-install-recommends
ENV INSTALL_PATH /web
RUN mkdir -p $INSTALL_PATH
WORKDIR $INSTALL_PATH
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install
COPY . .

# precompile assets using dummy data
RUN bundle exec rake RAILS_ENV=production DATABASE_URL=postgresql://user:pass@127.0.0.1/dbname SECRET_TOKEN=pickasecuretoken assets:precompile
VOLUME ["$INSTALL_PATH/public"]
VOLUME /postgres
CMD RAILS_ENV=development bundle exec unicorn -c config/unicorn.rb

docker-compose.yml

postgres:
  image: postgres:9.4.5
  environment:
    POSTGRES_USER: web
    POSTGRES_PASSWORD: mypassword
  ports:
    - "5432:5432"
  volumes:
    - postgres:/var/lib/postgresql/data

web:
  build: .
  links:
    - postgres
  volumes:
    - .:/web
  ports:
    - "3000:3000"
  env_file:
    - .env

config/database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

database.yml 中的行从 .env 文件中获取存储在容器中的 DATABASE_URL 环境变量。

【问题讨论】:

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


    【解决方案1】:

    我花了一天的大部分时间来摆弄这个。最终对我有用的是回退到 Postgres 默认值。

    docker-compose.yml

    postgres:
     image: postgres:9.4.5
     ports:
       - "5432:5432"
     volumes:
       - postgres:/var/lib/postgresql/data
    

    .env

    DATABASE_URL=postgresql://web:@postgres:5432/web?encoding=utf8&pool=5&timeout=5000
    

    DATABASE_URL 中,将密码分隔符保留在 url 中但将密码留空终于可以正常工作了。

    【讨论】: