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

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

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处失败,并显示奇怪的错误

 > [10/11] RUN bundle exec rake assets:precompile:                                                     
#14 0.596 bundler: failed to load command: rake (/minirails/vendor/bundle/ruby/2.7.0/bin/rake)         
#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?
#14 0.597   /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:374:in `block in replace_bin_path'
#14 0.597   /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:402:in `block in replace_bin_path'
#14 0.597   /minirails/vendor/bundle/ruby/2.7.0/bin/rake:23:in `<top (required)>'

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

Marions-MacBook-Pro-2:minirails marion$ docker run --name minirails minirails
bundler: failed to load command: rake (/minirails/vendor/bundle/ruby/2.7.0/bin/rake)
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?
  /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:374:in `block in replace_bin_path'
  /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:402:in `block in replace_bin_path'
  /minirails/vendor/bundle/ruby/2.7.0/bin/rake:23:in `<top (required)>'
bin/rails:4:in `require': cannot load such file -- rails/commands (LoadError)
	from bin/rails:4:in `<main>'

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

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.6'

gem 'rails', '~> 6.1'
gem 'sqlite3', '~> 1.4'
gem 'puma', '~> 4.1'
gem 'sass-rails', '>= 6'

group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'listen', '~> 3.2'
end

Dockerfile:

FROM ruby:2.7.6-bullseye
LABEL maintainer="Me"

EXPOSE 3000
ENTRYPOINT ["/docker-entrypoint.sh"]

ENV PATH=/root/.yarn/bin:$PATH RAILS_ENV=production SECRET_KEY_BASE=c1f89d98........d47bc66fdcd417
ENV TZ=Europe/Berlin

RUN apt-get update && apt-get upgrade -y && apt-get update && \
    apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7 && \
    apt-get install -y apt-transport-https ca-certificates && \
    sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger bullseye main > /etc/apt/sources.list.d/passenger.list' && \
    apt-get update && apt-get install -y nginx-extras passenger curl certbot python3-certbot-nginx && \
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list &&\
    apt-get install -y yarn nano cron

RUN apt-get -y install tzdata && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

ADD Gemfile Gemfile.lock /minirails/

WORKDIR minirails

COPY ./ /minirails

RUN mkdir /minirails/vendor
RUN bundle config --global silence_root_warning 1 
RUN bundle config set deployment 'true'
RUN bundle config set without 'development test'
RUN bundle install

RUN echo "Rails.application.config.app_version = '$(date +%d.%m.%Y)'" > ./config/initializers/version.rb

#RUN bundle exec rake assets:precompile

ADD ./docker-entrypoint.sh /

docker-entrypoint.sh:

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

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

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

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

=============== Phusion Passenger(R) Standalone web server started ===============
PID file: /minirails/tmp/pids/passenger.3000.pid
Log file: /minirails/log/passenger.3000.log
Environment: production
Accessible via: http://0.0.0.0:3000/
...
===============================================================================
[ 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)
App 97 output: Error: The application encountered the following error: cannot load such file -- rack (LoadError)
...
[ 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

 &gt; [10/11] RUN bundle exec rake assets:precompile:                                                     
#14 0.596 bundler: failed to load command: rake (/minirails/vendor/bundle/ruby/2.7.0/bin/rake)         
#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?
#14 0.597   /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:374:in `block in replace_bin_path&#39;
#14 0.597   /usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:402:in `block in replace_bin_path&#39;
#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

Marions-MacBook-Pro-2:minirails marion$ docker run --name minirails minirails
bundler: failed to load command: rake (/minirails/vendor/bundle/ruby/2.7.0/bin/rake)
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?
/usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:374:in `block in replace_bin_path&#39;
/usr/local/lib/ruby/2.7.0/bundler/rubygems_integration.rb:402:in `block in replace_bin_path&#39;
/minirails/vendor/bundle/ruby/2.7.0/bin/rake:23:in `&lt;top (required)&gt;&#39;
bin/rails:4:in `require&#39;: cannot load such file -- rails/commands (LoadError)
from bin/rails:4:in `&lt;main&gt;&#39;

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

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

Dockerfile:

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

docker-entrypoint.sh:

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

If I change the file docker-entrypoint.sh to

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

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

=============== Phusion Passenger(R) Standalone web server started ===============
PID file: /minirails/tmp/pids/passenger.3000.pid
Log file: /minirails/log/passenger.3000.log
Environment: production
Accessible via: http://0.0.0.0:3000/
...
===============================================================================
[ 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)
App 97 output: Error: The application encountered the following error: cannot load such file -- rack (LoadError)
...
[ 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缺少以下命令:

ENV RAILS_ENV production

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

...
ADD Gemfile Gemfile.lock /minirails/
RUN mkdir /minirails/vendor
WORKDIR minirails
COPY ./ /minirails
ENV RAILS_ENV production
RUN bundle config --global silence_root_warning 1 
RUN bundle config set without 'development test'
RUN bundle install
...

...现在它可以工作。

英文:

The Dockerfile is missing the command

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

...
ADD Gemfile Gemfile.lock /minirails/
RUN mkdir /minirails/vendor
WORKDIR minirails
COPY ./ /minirails
ENV RAILS_ENV production
RUN bundle config --global silence_root_warning 1 
RUN bundle config set without &#39;development test&#39;
RUN bundle install
...

... 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:

确定