0

这是我的 Dockerfile

# syntax=docker/dockerfile:1
FROM ruby:3.0.3
RUN apt-get update -qq && apt-get install -y nodejs npm yarn postgresql-client
RUN curl --compressed -o- -L https://yarnpkg.com/install.sh | bash
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
RUN rails assets:precompile
COPY . /myapp
RUN npm install -g yarn -y
RUN yarn install
RUN yarn build:css
RUN rails webpacker:install
RUN rails webpacker:clobber
RUN rails webpacker:compile

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

在我的 package.json 我有以下

"scripts": {
"build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules"

}

当我运行docker-compose build正常但命令RUN yarn build:css无效时,builds文件夹为空。

4

1 回答 1

0

我最近遇到了一个类似的问题,问题的根源实际上是 npm 和 yarn。

我能够在 docker 容器中启动并运行我的应用程序。

使用正确配置的应用程序cssbundling-rails不需要运行命令

"scripts": { "build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss ./app/assets/builds/application.css --no-source-map --load-path=node_modules" },

让应用程序生成build/application.css

npm 已知问题

检查你的 npm 版本,如果你没有使用 npm 7.1+ 会有一个问题,这基本上会阻止纱线正确设置。

运行npm -v,如有必要,升级 npmnpm install -g npm@latest

如果必须升级 npm,则需要关闭终端并启动一个新终端,验证 npm 版本是否大于 7.1。

升级后重新运行rails css:install:bootstrap

一切都应该正确配置。

捆绑观察者

cssbundling-rails 的文档中 “您可以使用这种方法进行开发,方法是在终端中以监视模式运行捆绑器yarn build:css --watch(如果您不使用类似puma-dev 的东西,则在另一个终端中运行您的 Rails 服务器)。您也可以使用./bin/dev,这将同时启动 Rails 服务器和 CSS 构建观察器(以及 JS 构建观察器,如果你也在使用的话jsbundling-rails)。”

因此,您可以yarn build:css --watch从 Rails 应用程序中的单独终端运行,也可以只使用./bin/dev任何一种方式都可以解决您遇到的问题。

于 2022-01-28T20:47:10.463 回答