Ruby on rails 将Rails应用程序dockerize,错误Postgres
我正在尝试对Rails应用程序进行dockerize。我遇到的问题是,我遇到了以下错误:Ruby on rails 将Rails应用程序dockerize,错误Postgres,ruby-on-rails,postgresql,docker,Ruby On Rails,Postgresql,Docker,我正在尝试对Rails应用程序进行dockerize。我遇到的问题是,我遇到了以下错误: 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"? 容纳Postg
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的docker容器(运行btw):
包含以下数据库:
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
my.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
Docker文件:
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
这里怎么了?
谢谢大家! 在
docker compose.yml
中,您需要在prod\u app
和prod\u db
之间添加链接。除当前配置外,缺少以下内容:
prod_app:
links:
- prod_db
这样做的目的是将prod\u db
添加到/etc/hosts
的prod\u app
图像中,这意味着您可以使用prod\u db
作为POSTGRES\u HOST
的值
顺便说一下,您不需要更改
database.yml
中的端口。docker compose.yml
中的ports
指令将主机上的端口映射到容器上的端口。语法为HOST:CONTAINER
,意思是15672:5672
将主机(即您的机器)上的端口15672映射到容器上的端口5672。仅当您要从主机连接到容器时,才需要此选项。容器之间的通信不需要此端口映射。只要链接两个容器,它们就可以在所有端口上相互通信。db容器的公共端口不是默认端口:-“15432:5432”
,因此必须在rails database.yml文件中指定它。连接psql时不必提及主机通过Unix套接字连接。这是不同的权限。指定端口的sintax是端口:15432:5432?如果我没有在docker.compose.prod.yml中指定端口,这意味着我不应该使用该端口修改database.yml文件?我认为您必须使用数据库和RAILS应用程序之间的链接。它仍然不起作用。当我试图以shopradu用户psql-h prod_db-p 5432-U shopradu-W somePassword myshop_production>>的身份手动连接到数据库时,它要求输入密码,我在.env.prod somePassword中提供了密码。然后我得到了错误psql:invalid port number:“5432”我确保用户shopradu拥有.env.prod文件中提供的密码,使用带有密码'somePassword'的ALTER user shopradu;从postgres控制台。
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
prod_app:
links:
- prod_db