Docker部署Ruby on Rails执行资源编译时提示Node.js未安装的问题
问题
执行docker-compose up启动服务时,entrypoint脚本中的bundle exec rake assets:precompile RAILS_ENV=production命令报错:
Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/
Dockerfile中已安装Node.js,但问题依旧,求解决。
附相关文件内容
Dockerfile
# syntax=docker/dockerfile:1 FROM ruby:3.0.4 ENV NODE_OPTIONS="--openssl-legacy-provider" RUN apt-get update -qq && \ apt-get install -y nodejs postgresql-client python curl apt-transport-https && \ curl -fsSL https://deb.nodesource.com/setup_14.x | bash - && \ apt-get install -y nodejs && \ 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 update && \ apt-get install -y yarn WORKDIR /eclipse COPY Gemfile /eclipse/Gemfile COPY Gemfile.lock /eclipse/Gemfile.lock RUN bundle install RUN echo "Install webpacker ----" RUN bundle exec rails webpacker:install \ rails webpacker:compile # Entrypoint.sh will be run every time the container starts. COPY /bin/entrypoint.sh /usr/bin/ RUN chmod +x /usr/bin/entrypoint.sh ENTRYPOINT ["entrypoint.sh"] EXPOSE 3000 # Configure the main process to run when running the image CMD ["rails", "server", "-b", "0.0.0.0"]
entrypoint.sh
#!/bin/bash set -e echo "Compile assets ----" bundle exec rake assets:precompile RAILS_ENV=production echo "Create database ----" bundle exec rake db:create echo "Rake database ----" bundle exec rake db:migrate # Remove a potentially pre-existing server.pid for Rails. rm -f /app/tmp/pids/server.pid # Then exec the container's main process (what's set as CMD in the Dockerfile). exec "$@"
解决办法
1. 修复Dockerfile中webpacker命令的语法错误
你的Dockerfile里,bundle exec rails webpacker:install \后面直接跟rails webpacker:compile,反斜杠会把第二个命令当作第一个命令的参数,导致webpacker安装失败,进而引发后续assets编译时找不到Node.js的错误。修正为:
RUN echo "Install webpacker ----" RUN bundle exec rails webpacker:install && \ bundle exec rails webpacker:compile
用&&连接两个命令,确保它们依次执行。
2. 清理重复的Node.js安装步骤
Dockerfile中先通过apt-get install -y nodejs安装了系统自带的旧版本Node.js,之后又通过nodesource安装Node.js 14,版本冲突可能导致路径混乱。删掉第一个nodejs的安装:
RUN apt-get update -qq && \ apt-get install -y postgresql-client python curl apt-transport-https && \ curl -fsSL https://deb.nodesource.com/setup_14.x | bash - && \ apt-get install -y nodejs && \ 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 update && \ apt-get install -y yarn
3. 验证entrypoint中的Node.js可用性
在entrypoint.sh开头添加检查命令,确认Node.js是否能被当前Shell访问:
#!/bin/bash set -e # 检查Node.js状态 echo "Node.js version:" node --version echo "Node.js path:" which node echo "Compile assets ----" bundle exec rake assets:precompile RAILS_ENV=production # ... 其余代码不变
如果输出显示找不到node命令,说明PATH有问题,可以手动添加路径:
export PATH="$PATH:/usr/bin" # 通常nodesource安装的node在/usr/bin,可根据实际情况调整
4. 确认环境变量传递
在entrypoint中执行assets编译前,显式指定完整环境变量:
bundle exec rake assets:precompile RAILS_ENV=production NODE_ENV=production
内容的提问来源于stack exchange,提问作者joeyk16
相关产品推荐
相关产品推荐

