将旧版Rails应用程序(rails 3.2)移至docker。我可以在docker-compose本地正常工作。当我尝试构建用于暂存的容器时,问题就来了。
第RUN bin/rake assets:precompile
行失败了,因为尽管我将其设置为未初始化,但它在预编译事件中正在初始化应用。
任何帮助将不胜感激。
FROM ruby:2.3.8
ARG RAILS_SECRET_KEY
ARG RAILS_ENV=staging
ARG GITHUB_REPO_API_TOKEN
ARG POSTGRES_USER
ARG POSTGRES_PASSWORD
ARG POSTGRES_DB
ARG DATABASE_HOST
ARG FOG_PROVIDER
ARG FOG_REGION
ARG ASSET_FOG_DIRECTORY
ARG AWS_ACCESS_KEY_ID
ARG AWS_SECRET_ACCESS_KEY
# Most of these deps are for running JS tests. You can add whatever extra deps your project has (ffmpeg, imagemagick, etc)
RUN apt-get update
RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
nodejs \
qt5-default \
libqt5webkit5-dev \
xvfb \
postgresql \
postgresql-contrib \
imagemagick \
openjdk-8-jdk
# cache the gemfile seperate
COPY Gemfile* /usr/src/app/
# required at CLI
ENV BUNDLE_GITHUB__COM=${GITHUB_REPO_API_TOKEN}:x-oauth-basic
# You'll need something here. For development, you don't need anything super secret.
ENV SECRET_KEY_BASE=${RAILS_SECRET_KEY}
ENV RAILS_ENV=${RAILS_ENV}
# this tells container to cd into app directory
WORKDIR /usr/src/app
# so we don't have to reinstall all the gems when we add just one when we build a new container
ENV BUNDLE_PATH /gems
RUN bundle install
# copy app dir into /usr/src/app in container
COPY . /usr/src/app/
RUN bin/rake assets:precompile
# for the irritating times the server doesn't clean up when container shutsdown
ENTRYPOINT ["./docker-entrypoint.sh"]
CMD ["bin/rails", "s", "-b", "0.0.0.0"]
# Compress JavaScripts and CSS
config.assets.compress = true
config.assets.initialize_on_precompile = false
这一次错误输出是试图连接数据库的纸迹,问题出在。我正在构建docker映像,因此在编译资产时DB尚未启动。这是鸡和鸡蛋的错误。我是Docker的新手,所以不确定如何解决它。我的演示使用的是Rails 5.2,它使用Web Packer和yarn来管理资产,并且在预编译时没有出现此错误。
rake aborted!
PG::ConnectionBad: could not translate host name "database" to address: Name or service not known
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `initialize'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `new'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `connect'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/postgresql_adapter.rb:324:in `initialize'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `new'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `postgresql_connection'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `block (2 levels) in checkout'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `block in checkout'
/usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `block in connection'
/usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
/gems/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
/gems/gems/activerecord-3.2.22/lib/active_record/model_schema.rb:224:in `table_exists?'
/gems/gems/activerecord-3.2.22/lib/active_record/attribute_methods/primary_key.rb:75:in `get_primary_key'
/gems/gems/activerecord-3.2.22/lib/active_record/attribute_methods/primary_key.rb:60:in `reset_primary_key'
/gems/gems/activerecord-3.2.22/lib/active_record/attribute_methods/primary_key.rb:49:in `primary_key'
/gems/gems/paper_trail-3.0.9/lib/paper_trail/version_concern.rb:67:in `block in timestamp_sort_order'
/gems/gems/paper_trail-3.0.9/lib/paper_trail/version_concern.rb:66:in `tap'
/gems/gems/paper_trail-3.0.9/lib/paper_trail/version_concern.rb:66:in `timestamp_sort_order'
/gems/gems/paper_trail-3.0.9/lib/paper_trail/has_paper_trail.rb:72:in `has_paper_trail'
/usr/src/app/app/models/platform.rb:8:in `<class:Platform>'
/usr/src/app/app/models/platform.rb:1:in `<top (required)>'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/usr/src/app/lib/route_constraints.rb:2:in `<class:PlatformSite>'
/usr/src/app/lib/route_constraints.rb:1:in `<top (required)>'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/usr/src/app/lib/payments/providers/paypal_common/ipn.rb:105:in `<top (required)>'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/usr/src/app/lib/payments/providers/paypal_common/paypal_common.rb:3:in `<top (required)>'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/usr/src/app/lib/payments/providers/providers.rb:3:in `<top (required)>'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/usr/src/app/lib/payments/payments.rb:1:in `<top (required)>'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/usr/src/app/config/initializers/z_custom_modules.rb:21:in `<top (required)>'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:245:in `load'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:245:in `block in load'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:245:in `load'
/gems/gems/railties-3.2.22/lib/rails/engine.rb:593:in `block (2 levels) in <class:Engine>'
/gems/gems/railties-3.2.22/lib/rails/engine.rb:592:in `each'
/gems/gems/railties-3.2.22/lib/rails/engine.rb:592:in `block in <class:Engine>'
/gems/gems/railties-3.2.22/lib/rails/initializable.rb:30:in `instance_exec'
/gems/gems/railties-3.2.22/lib/rails/initializable.rb:30:in `run'
/gems/gems/railties-3.2.22/lib/rails/initializable.rb:55:in `block in run_initializers'
/gems/gems/railties-3.2.22/lib/rails/initializable.rb:54:in `each'
/gems/gems/railties-3.2.22/lib/rails/initializable.rb:54:in `run_initializers'
/gems/gems/railties-3.2.22/lib/rails/application.rb:136:in `initialize!'
/gems/gems/railties-3.2.22/lib/rails/railtie/configurable.rb:30:in `method_missing'
/usr/src/app/config/environment.rb:5:in `<top (required)>'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
/gems/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
/gems/gems/railties-3.2.22/lib/rails/application.rb:103:in `require_environment!'
/gems/gems/railties-3.2.22/lib/rails/application.rb:305:in `block (2 levels) in initialize_tasks'
/gems/gems/rake-12.3.1/lib/rake/task.rb:271:in `block in execute'
/gems/gems/rake-12.3.1/lib/rake/task.rb:271:in `each'
/gems/gems/rake-12.3.1/lib/rake/task.rb:271:in `execute'
/gems/gems/rake-12.3.1/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/gems/gems/rake-12.3.1/lib/rake/task.rb:193:in `invoke_with_call_chain'
/gems/gems/rake-12.3.1/lib/rake/task.rb:237:in `block in invoke_prerequisites'
/gems/gems/rake-12.3.1/lib/rake/task.rb:235:in `each'
/gems/gems/rake-12.3.1/lib/rake/task.rb:235:in `invoke_prerequisites'
/gems/gems/rake-12.3.1/lib/rake/task.rb:212:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/gems/gems/rake-12.3.1/lib/rake/task.rb:193:in `invoke_with_call_chain'
/gems/gems/rake-12.3.1/lib/rake/task.rb:182:in `invoke'
/gems/gems/ckeditor-4.1.6/lib/tasks/ckeditor.rake:24:in `block in <top (required)>'
/gems/gems/rake-12.3.1/lib/rake/task.rb:271:in `block in execute'
/gems/gems/rake-12.3.1/lib/rake/task.rb:271:in `each'
/gems/gems/rake-12.3.1/lib/rake/task.rb:271:in `execute'
/gems/gems/rake-12.3.1/lib/rake/task.rb:213:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/gems/gems/rake-12.3.1/lib/rake/task.rb:193:in `invoke_with_call_chain'
/gems/gems/rake-12.3.1/lib/rake/task.rb:182:in `invoke'
/gems/gems/rake-12.3.1/lib/rake/application.rb:160:in `invoke_task'
/gems/gems/rake-12.3.1/lib/rake/application.rb:116:in `block (2 levels) in top_level'
/gems/gems/rake-12.3.1/lib/rake/application.rb:116:in `each'
/gems/gems/rake-12.3.1/lib/rake/application.rb:116:in `block in top_level'
/gems/gems/rake-12.3.1/lib/rake/application.rb:125:in `run_with_threads'
/gems/gems/rake-12.3.1/lib/rake/application.rb:110:in `top_level'
/gems/gems/rake-12.3.1/lib/rake/application.rb:83:in `block in run'
/gems/gems/rake-12.3.1/lib/rake/application.rb:186:in `standard_exception_handling'
/gems/gems/rake-12.3.1/lib/rake/application.rb:80:in `run'
/gems/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
bin/rake:3:in `load'
bin/rake:3:in `<main>'
Tasks: TOP => ckeditor:nondigest => environment
因此,我找到了一种防止数据库连接的方法。但是现在尝试连接到Redis时出现错误。这是database.yml。 DB_ADAPTER
正在使用activerecord-nulldb-adapter
gem。我仍然不确定为什么config.assets.initialize_on_precompile = false
仍在初始化应用程序。运行Rails 3.2
default: &default
adapter: <%= ENV['DB_ADAPTER'] ||= 'postgresql' %>
encoding: unicode
host: <%= ENV['DATABASE_HOST'] ||= '' %>
username: <%= ENV['POSTGRES_USER'] ||= '' %>
password: <%= ENV['POSTGRES_PASSWORD'] ||= '' %>
database: <%= ENV['POSTGRES_DB'] ||= '' %>
pool: 5
staging:
<<: *default
development:
<<: *default
test:
<<: *default
database: ce_test2
rspec:
<<: *default
database: ce_test2