2017-06-17 32 views
0

我正在嘗試dockerize Rails應用程序。我得到的問題是,我得到這個錯誤:dockerize a Rails應用程序,錯誤Postgres

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"? 

泊塢窗容器保存Postgres的(順便說一句運行):

3e11664277b3  postgres:9.6.1  "/docker-entrypoint.s" 2 days ago   Up 33 minutes  0.0.0.0:15432->5432/tcp     mystore_prod_db_1 

是一個包含以下數據庫:

Name  | Owner | Encoding | Collate | Ctype | Access privileges 
-------------------+----------+----------+------------+------------+----------------------- 
myshop_prod_db | shopradu | UTF8  | en_US.utf8 | en_US.utf8 | 
myshop_production | shopradu | UTF8  | en_US.utf8 | en_US.utf8 | 
postgres   | postgres | UTF8  | en_US.utf8 | en_US.utf8 | 
shopradu   | postgres | UTF8  | en_US.utf8 | en_US.utf8 | 
template0   | postgres | UTF8  | en_US.utf8 | en_US.utf8 | =c/postgres   + 
        |   |   |   |   | postgres=CTc/postgres 
template1   | postgres | UTF8  | en_US.utf8 | en_US.utf8 | =c/postgres   + 
        |   |   |   |   | postgres=CTc/postgres 
(6 rows) 

我可以使用以下命令與用戶shopradu進行手動連接:

psql -d myshop_production -U shopradu 

我.env.prod文件:

POSTGRES_USER=shopradu 
POSTGRES_PASSWORD=somePassword 
POSTGRES_HOST=prod_db 
RAILS_ENV=production 
RAILS_SERVE_STATIC_FILES=true 
SECRET_KEY_BASE=someKeyBase 

的database.yml中:

default: &default 
    adapter: postgresql 
    encoding: unicode 
    host: db 
    username: <%= ENV["POSTGRES_USER"] %> 
    password: <%= ENV["POSTGRES_PASSWORD"] %> 

development: 
    <<: *default 
    host: localhost 
    username: radu 
    password: devPassword 
    database: mystore_dev 

# Warning: The database defined as "test" will be erased and 
# re-generated from your development database when you run "rake". 
# Do not set this db to the same as development or production. 
test: 
    <<: *default 
    database: db/test.sqlite3 

production: 
    <<: *default 
    host: <%= ENV["POSTGRES_HOST"] %> 
    database: myshop_production 

泊塢窗,compose.prod.yml文件:

version: "2" 

volumes: 
    assets: 
    external: false 
    configs: 
    external: false 
    db-data: 
    external: false 

services: 
    webserver: 
    image: "nginx:1.11.8" 
    env_file: .env.prod 
    ports: 
     - "80:80" 
     - "443:443" 
    volumes: 
     - assets:/usr/share/nginx/html 
     - configs:/etc/nginx/conf.d 

    prod_db:  
    image: postgres:9.6.1 
    ports: 
    - "15432:5432" 
    env_file: .env.prod 
    volumes: 
     - db-data:/var/lib/postgresql/data 

    prod_app: 
    build: 
     context: . 
     dockerfile: Dockerfile.prod 
    env_file: .env.prod 
    ports: 
     - "3000:3000" 
    volumes: 
     - assets:/usr/share/nginx/html 
     - configs:/etc/nginx/conf.d 
    depends_on: 
     - prod_db 
- webserver 

泊塢窗文件:

FROM ruby:2.2.5 

RUN apt-get update -yqq \ 
    && apt-get install -yqq --no-install-recommends \ 
    postgresql-client \ 
    nodejs \ 
    && apt-get -q clean 


# Pre-install gems with native extensions 
RUN gem install nokogiri -v "1.6.8.1" 

WORKDIR /usr/src/app 
COPY Gemfile* ./ 
RUN bundle install 
COPY . . 

# Pre-compile assets 
ENV RAILS_ENV production 
RUN rake assets:precompile 

CMD script/start 

Production.rb文件:

Rails.application.configure do 
    # Settings specified here will take precedence over those in config/application.rb. 

    # Code is not reloaded between requests. 
    config.cache_classes = true 
    config.serve_static_files = true 
    config.assets.compile = true 
    config.assets.digest = true 
    config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' 

    # Eager load code on boot. This eager loads most of Rails and 
    # your application in memory, allowing both threaded web servers 
    # and those relying on copy on write to perform better. 
    # Rake tasks automatically ignore this option for performance. 
    config.eager_load = true 

    # Full error reports are disabled and caching is turned on. 
    config.consider_all_requests_local  = false 
    config.action_controller.perform_caching = true 

    # Enable Rack::Cache to put a simple HTTP cache in front of your application 
    # Add `rack-cache` to your Gemfile before enabling this. 
    # For large-scale production use, consider using a caching reverse proxy like 
    # NGINX, varnish or squid. 
    # config.action_dispatch.rack_cache = true 

    # Disable Rails's static asset server (Apache or NGINX will already do this). 
    config.serve_static_assets = false 

    # Compress JavaScripts and CSS. 
    config.assets.js_compressor = :uglifier 
    # config.assets.css_compressor = :sass 

    # Do not fallback to assets pipeline if a precompiled asset is missed. 
    config.assets.compile = false 

    # Asset digests allow you to set far-future HTTP expiration dates on all assets, 
    # yet still be able to expire them through the digest params. 
    config.assets.digest = true 

    # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb 

    # Specifies the header that your server uses for sending files. 
    # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache 
    # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX 

    # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 
    config.force_ssl = !!ENV["RAILS_FORCE_SSL"].presence 

    # Decrease the log volume. 
    # config.log_level = :info 

    # Prepend all log lines with the following tags. 
    # config.log_tags = [ :subdomain, :uuid ] 

    # Use a different logger for distributed setups. 
    # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) 

    # Use a different cache store in production. 
    # config.cache_store = :mem_cache_store 

    # Enable serving of images, stylesheets, and JavaScripts from an asset server. 
    # config.action_controller.asset_host = 'http://assets.example.com' 

    # Ignore bad email addresses and do not raise email delivery errors. 
    # Set this to true and configure the email server for immediate delivery to raise delivery errors. 
    # config.action_mailer.raise_delivery_errors = false 

    # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 
    # the I18n.default_locale when a translation cannot be found). 
    config.i18n.fallbacks = true 

    # Send deprecation notices to registered listeners. 
    config.active_support.deprecation = :notify 

    # Use default logging formatter so that PID and timestamp are not suppressed. 
    config.log_formatter = ::Logger::Formatter.new 

    # Do not dump schema after migrations. 
    config.active_record.dump_schema_after_migration = false 
end 

什麼可以在這裏錯? 謝謝!

+0

db容器的公共端口不是默認的:' - 「15432:5432」',所以你必須在rails databse.yml文件中指定它。 – wesley6j

+0

與psql連接時不提主機通過Unix套接字連接。這是不同的權限。 –

+0

指定端口的sintax是端口:15432:5432?如果我沒有在docker.compose.prod.yml中指定端口,這意味着我不應該使用端口修改database.yml文件? –

回答

0

docker-compose.yml中,您需要在prod_appprod_db之間添加一個鏈接。不包括當前的配置,這就是丟失:

prod_app: 
    links: 
    - prod_db 

這樣做是添加prod_db/etc/hostsprod_app圖像內,這意味着你的使用prod_dbPOSTGRES_HOST值。

順便說一下,您不需要更改database.yml中的端口。 docker-compose.yml中的ports指令將主機上的端口映射到容器上的端口。語法爲HOST:CONTAINER,即15672:5672將主機(即您的計算機)上的端口15672映射到容器上的端口5672。只有當你想連接到主機的容器時纔有必要。容器不需要這個端口映射來互相通信。只要鏈接兩個容器,他們就可以在彼此的所有端口上進行通信。

+0

它仍然無法正常工作。當我嘗試手動連接到數據庫時,它會詢問密碼,我提供.env.prod somePassword中的密碼。比我得到的錯誤psql:無效的端口號:「5432」我確保用戶shopradu有在.env.prod文件中提供的密碼,使用ALTER USER shopradu WITH PASSWORD'somePassword';來自postgres控制檯。 –