英文:
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 => 'main#index'
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
> [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)>'
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'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 (I changed it a bit from the one included in cool_app_minimal):
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
If I change the file docker-entrypoint.sh to
#! /usr/bin/env bash
sleep 10 && 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 'development test'
RUN bundle install
...
... and now it works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论