构建简单的Rails应用失败,找不到rake(以及其他组件)。

huangapple go评论105阅读模式
英文:

Building simple rails app fails, rake (and others) not found

问题

我已经拉取了https://github.com/hahmed/cool_app_minimal.git上的最小Rails应用程序,并添加了一个最小的视图(在routes.rb中添加了一个控制器和一个带有root :to => 'main#index'的索引视图)。 (还将项目名称更改为'minirails'。)这在我的机器上运行得很完美。

我有一个Dockerfile,这是我的标准Dockerfile(如下所示),但我无法构建它。它在RUN bundle exec rake assets:precompile处失败,并显示奇怪的错误

  1. > [10/11] RUN bundle exec rake assets:precompile:
  2. #14 0.596 bundler: failed to load command: rake (/minirails/vendor/bundle/ruby/2.7.0/bin/rake)
  3. #14 0.597 Gem::Exception: can't find executable rake for gem rake. rake is not currently included in the bundle, perhaps you meant to add it to your Gemfile?
  4. #14 0.597 /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:374:in `block in replace_bin_path'
  5. #14 0.597 /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:402:in `block in replace_bin_path'
  6. #14 0.597 /minirails/vendor/bundle/ruby/2.7.0/bin/rake:23:in `<top (required)>'

如果我注释掉这个命令,镜像可以成功构建,但我无法启动它,错误消息如下:

  1. Marions-MacBook-Pro-2:minirails marion$ docker run --name minirails minirails
  2. bundler: failed to load command: rake (/minirails/vendor/bundle/ruby/2.7.0/bin/rake)
  3. Gem::Exception: can't find executable rake for gem rake. rake is not currently included in the bundle, perhaps you meant to add it to your Gemfile?
  4. /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:374:in `block in replace_bin_path'
  5. /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:402:in `block in replace_bin_path'
  6. /minirails/vendor/bundle/ruby/2.7.0/bin/rake:23:in `<top (required)>'
  7. bin/rails:4:in `require': cannot load such file -- rails/commands (LoadError)
  8. from bin/rails:4:in `<main>'

Gemfile(我从cool_app_minimal中的Gemfile稍微更改了它):

  1. source 'https://rubygems.org'
  2. git_source(:github) { |repo| "https://github.com/#{repo}.git" }
  3. ruby '2.7.6'
  4. gem 'rails', '~> 6.1'
  5. gem 'sqlite3', '~> 1.4'
  6. gem 'puma', '~> 4.1'
  7. gem 'sass-rails', '>= 6'
  8. group :development, :test do
  9. gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  10. end
  11. group :development do
  12. gem 'listen', '~> 3.2'
  13. end

Dockerfile:

  1. FROM ruby:2.7.6-bullseye
  2. LABEL maintainer="Me"
  3. EXPOSE 3000
  4. ENTRYPOINT ["/docker-entrypoint.sh"]
  5. ENV PATH=/root/.yarn/bin:$PATH RAILS_ENV=production SECRET_KEY_BASE=c1f89d98........d47bc66fdcd417
  6. ENV TZ=Europe/Berlin
  7. RUN apt-get update && apt-get upgrade -y && apt-get update && \
  8. apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7 && \
  9. apt-get install -y apt-transport-https ca-certificates && \
  10. sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger bullseye main > /etc/apt/sources.list.d/passenger.list' && \
  11. apt-get update && apt-get install -y nginx-extras passenger curl certbot python3-certbot-nginx && \
  12. curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add && \
  13. echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list &&\
  14. apt-get install -y yarn nano cron
  15. RUN apt-get -y install tzdata && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
  16. ADD Gemfile Gemfile.lock /minirails/
  17. WORKDIR minirails
  18. COPY ./ /minirails
  19. RUN mkdir /minirails/vendor
  20. RUN bundle config --global silence_root_warning 1
  21. RUN bundle config set deployment 'true'
  22. RUN bundle config set without 'development test'
  23. RUN bundle install
  24. RUN echo "Rails.application.config.app_version = '$(date +%d.%m.%Y)'" > ./config/initializers/version.rb
  25. #RUN bundle exec rake assets:precompile
  26. ADD ./docker-entrypoint.sh /

docker-entrypoint.sh:

  1. #! /usr/bin/env bash
  2. sleep 10 && bundle exec rake db:migrate
  3. bin/rails s

如果我将docker-entrypoint.sh文件更改为:

  1. #! /usr/bin/env bash
  2. sleep 10 && bundle exec rake db:migrate
  3. passenger start --port 3000

容器会启动,但passenger无法生成进程,给我以下错误:

  1. =============== Phusion Passenger(R) Standalone web server started ===============
  2. PID file: /minirails/tmp/pids/passenger.3000.pid
  3. Log file: /minirails/log/passenger.3000.log
  4. Environment: production
  5. Accessible via: http://0.0.0.0:3000/
  6. ...
  7. ===============================================================================
  8. [ N 2023-06-15 11:30:50.6347 58/T5 age/Cor/SecurityUpdateChecker.h:519 ]: Security update check: no update found (next check in 24 hours)
  9. App 97 output: Error: The application encountered the following error: cannot load such file -- rack (LoadError)
  10. ...
  11. [ E 2023-06-15 11:30:51.6135 58/Tj age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /minirails: The application encountered the following error: cannot load such file -- rack (LoadError)

这听起来好像没有正确安装与Rails相关的东西。出了什么问题,我如何调试它?

英文:

I've pulled the minimal Rails app on https://github.com/hahmed/cool_app_minimal.git and added one minimal view (added a controller and an index view with root :to =&gt; &#39;main#index&#39; in routes.rb). (Also changed the project name to 'minirails'.) This runs perfectly fine on my machine.
I have a Dockerfile, which is my standard Dockerfile (see below), and I can't get it to build. It fails at RUN bundle exec rake assets:precompile with the bizarre error

  1. &gt; [10/11] RUN bundle exec rake assets:precompile:
  2. #14 0.596 bundler: failed to load command: rake (/minirails/vendor/bundle/ruby/2.7.0/bin/rake)
  3. #14 0.597 Gem::Exception: can&#39;t find executable rake for gem rake. rake is not currently included in the bundle, perhaps you meant to add it to your Gemfile?
  4. #14 0.597 /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:374:in `block in replace_bin_path&#39;
  5. #14 0.597 /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:402:in `block in replace_bin_path&#39;
  6. #14 0.597 /minirails/vendor/bundle/ruby/2.7.0/bin/rake:23:in `&lt;top (required)&gt;&#39;

If I comment out this command the image builds successfully, but I can't start it, here the error message is

  1. Marions-MacBook-Pro-2:minirails marion$ docker run --name minirails minirails
  2. bundler: failed to load command: rake (/minirails/vendor/bundle/ruby/2.7.0/bin/rake)
  3. Gem::Exception: can&#39;t find executable rake for gem rake. rake is not currently included in the bundle, perhaps you meant to add it to your Gemfile?
  4. /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:374:in `block in replace_bin_path&#39;
  5. /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:402:in `block in replace_bin_path&#39;
  6. /minirails/vendor/bundle/ruby/2.7.0/bin/rake:23:in `&lt;top (required)&gt;&#39;
  7. bin/rails:4:in `require&#39;: cannot load such file -- rails/commands (LoadError)
  8. from bin/rails:4:in `&lt;main&gt;&#39;

Gemfile (I changed it a bit from the one included in cool_app_minimal):

  1. source &#39;https://rubygems.org&#39;
  2. git_source(:github) { |repo| &quot;https://github.com/#{repo}.git&quot; }
  3. ruby &#39;2.7.6&#39;
  4. gem &#39;rails&#39;, &#39;~&gt; 6.1&#39;
  5. gem &#39;sqlite3&#39;, &#39;~&gt; 1.4&#39;
  6. gem &#39;puma&#39;, &#39;~&gt; 4.1&#39;
  7. gem &#39;sass-rails&#39;, &#39;&gt;= 6&#39;
  8. group :development, :test do
  9. gem &#39;byebug&#39;, platforms: [:mri, :mingw, :x64_mingw]
  10. end
  11. group :development do
  12. gem &#39;listen&#39;, &#39;~&gt; 3.2&#39;
  13. end

Dockerfile:

  1. FROM ruby:2.7.6-bullseye
  2. LABEL maintainer=&quot;Me&quot;
  3. EXPOSE 3000
  4. ENTRYPOINT [&quot;/docker-entrypoint.sh&quot;]
  5. ENV PATH=/root/.yarn/bin:$PATH RAILS_ENV=production SECRET_KEY_BASE=c1f89d98........d47bc66fdcd417
  6. ENV TZ=Europe/Berlin
  7. RUN apt-get update &amp;&amp; apt-get upgrade -y &amp;&amp; apt-get update &amp;&amp; \
  8. apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7 &amp;&amp; \
  9. apt-get install -y apt-transport-https ca-certificates &amp;&amp; \
  10. sh -c &#39;echo deb https://oss-binaries.phusionpassenger.com/apt/passenger bullseye main &gt; /etc/apt/sources.list.d/passenger.list&#39; &amp;&amp; \
  11. apt-get update &amp;&amp; apt-get install -y nginx-extras passenger curl certbot python3-certbot-nginx &amp;&amp; \
  12. curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add &amp;&amp; \
  13. echo &quot;deb https://dl.yarnpkg.com/debian/ stable main&quot; | tee /etc/apt/sources.list.d/yarn.list &amp;&amp;\
  14. apt-get install -y yarn nano cron
  15. RUN apt-get -y install tzdata &amp;&amp; ln -snf /usr/share/zoneinfo/$TZ /etc/localtime &amp;&amp; echo $TZ &gt; /etc/timezone
  16. ADD Gemfile Gemfile.lock /minirails/
  17. WORKDIR minirails
  18. COPY ./ /minirails
  19. RUN mkdir /minirails/vendor
  20. RUN bundle config --global silence_root_warning 1
  21. RUN bundle config set deployment &#39;true&#39;
  22. RUN bundle config set without &#39;development test&#39;
  23. RUN bundle install
  24. RUN echo &quot;Rails.application.config.app_version = &#39;$(date +%d.%m.%Y)&#39;&quot; &gt; ./config/initializers/version.rb
  25. #RUN bundle exec rake assets:precompile
  26. ADD ./docker-entrypoint.sh /

docker-entrypoint.sh:

  1. #! /usr/bin/env bash
  2. sleep 10 &amp;&amp; bundle exec rake db:migrate
  3. bin/rails s

If I change the file docker-entrypoint.sh to

  1. #! /usr/bin/env bash
  2. sleep 10 &amp;&amp; bundle exec rake db:migrate
  3. passenger start --port 3000

the container starts up, but passenger can't spawn a process, giving me the error

  1. =============== Phusion Passenger(R) Standalone web server started ===============
  2. PID file: /minirails/tmp/pids/passenger.3000.pid
  3. Log file: /minirails/log/passenger.3000.log
  4. Environment: production
  5. Accessible via: http://0.0.0.0:3000/
  6. ...
  7. ===============================================================================
  8. [ N 2023-06-15 11:30:50.6347 58/T5 age/Cor/SecurityUpdateChecker.h:519 ]: Security update check: no update found (next check in 24 hours)
  9. App 97 output: Error: The application encountered the following error: cannot load such file -- rack (LoadError)
  10. ...
  11. [ E 2023-06-15 11:30:51.6135 58/Tj age/Cor/App/Implementation.cpp:221 ]: Could not spawn process for application /minirails: The application encountered the following error: cannot load such file -- rack (LoadError)

This sounds as if nothing Rails-related was installed properly. What is wrong, how can I debug it?

答案1

得分: 0

Dockerfile缺少以下命令:

  1. ENV RAILS_ENV production

在执行bundle install之前。看起来我的Dockerfile第7行的命令不足够(或不正确)。我最后所做的唯一更改是:

  1. ...
  2. ADD Gemfile Gemfile.lock /minirails/
  3. RUN mkdir /minirails/vendor
  4. WORKDIR minirails
  5. COPY ./ /minirails
  6. ENV RAILS_ENV production
  7. RUN bundle config --global silence_root_warning 1
  8. RUN bundle config set without 'development test'
  9. RUN bundle install
  10. ...

...现在它可以工作。

英文:

The Dockerfile is missing the command

  1. ENV RAILS_ENV production

before executing bundle install. It seems that the command in line 7 of my Dockerfile was not enough (or incorrect). The only changes I made in the end were

  1. ...
  2. ADD Gemfile Gemfile.lock /minirails/
  3. RUN mkdir /minirails/vendor
  4. WORKDIR minirails
  5. COPY ./ /minirails
  6. ENV RAILS_ENV production
  7. RUN bundle config --global silence_root_warning 1
  8. RUN bundle config set without &#39;development test&#39;
  9. RUN bundle install
  10. ...

... and now it works.

huangapple
  • 本文由 发表于 2023年6月15日 17:39:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定