Docker 比较,Rails 和 Webpacker 不保留 node_modules

Docker Compose, Rails and Webpacker not perserving node_modules

TL;DR - yarn install 在 'intermediate container' 中安装 node_modules 并且包在构建步骤后消失。

我正在尝试让 webpacker 与我们的 docker 化 rails 5.0 应用程序一起使用。

Docker 文件

FROM our_company_centos_image:latest
RUN yum install wget -y
RUN wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
RUN yum install sqlite-devel yarn -y
RUN mkdir -p $APP_HOME/node_modules
COPY Gemfile Gemfile.lock package.json yarn.lock $APP_HOME/
RUN bundle install --path /bundle
RUN yarn install --pure-lockfile
ADD . $APP_HOME

当 yarn install 运行时,它会安装包,紧接着

Removing intermediate container 67bcd62926d2

在容器外,运行 ls node_modules显示一个空目录,docker-compose up过程最终会在运行 [=39=时失败] 由于模块不存在而退出。

我做了很多事情 link 添加 node_modules 作为 docker-compose.yml 中的卷但没有效果。

唯一有效的是 运行 yarn install 在本地构建目录,然后在目录中再次执行,但是我得到了 OS X 版本的包最终可能会导致问题。

我做错了什么?

docker-compose.yml

version: '2'
services:
  web:
    build: .
    network_mode: bridge
    environment:
      WEBPACK_DEV_SERVER_HOST: webpack_dev_server
    links:
      - webpack_dev_server
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - ./node_modules:/app/node_modules
      - .:/app
    ports:
      - "3000:3000"
    tty: true
    stdin_open: true
  webpack_dev_server:
    image: myapp_web
    network_mode: bridge
    command: bin/webpack-dev-server
    environment:
      NODE_ENV: development
      RAILS_ENV: development
      WEBPACK_DEV_SERVER_HOST: 0.0.0.0
    volumes:
      - .:/app
    ports:
      - "3035:3035"

最后一步是ADD . $APP_HOME。您还提到 node_modules 文件夹在您的本地树中是 。这是否意味着 node_modules 仍然作为一个空文件夹存在?

如果这是真的,那么 node_modules 空文件夹可能会在 ADD 步骤中被复制并覆盖在之前的 yarn 步骤中完成的所有内容。

我有一个 Rails 5.2.0.rc1 应用程序,webpacker 在 https://github.com/archonic/limestone 上工作。这还不是 100% 正确,但我发现 运行 docker-compose webpacker yarn install --pure-lockfiledocker-compose up --build 之前的新环境中 运行 可以正常工作。我还不完全确定为什么需要这样做,因为它在 Dockerfile 中。

另外据我所知,您的 web 卷应该只是 - '.:/app',带有 node_modules 的语句是多余的。

我找到的一个解决方案是将 node_modules 添加为一个卷。

例如,如果您的 node_modules 目录位于 /usr/src/app/node_modules,只需添加:

volumes:
- /usr/src/app/node_modules