dockerdocker-composeyarnpkgwebpacker

Docker Compose, Rails and Webpacker not perserving node_modules


TL;DR - yarn install installs node_modules in an 'intermediate container' and the packages disappear after the build step.

I'm trying to get webpacker going with our dockerized rails 5.0 app.

Dockerfile

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

When yarn install runs, it installs the packages, followed immediately by

Removing intermediate container 67bcd62926d2

Outside of the container, running ls node_modules shows an empty directory, and the docker-compose up process will eventually fail when running webpack_dev_server exits due to the modules not being present.

I've done various things link adding node_modules as a volume in docker-compose.yml to no effect.

The only thing that HAS worked is running yarn install locally to build the directory and then doing it again in the directory, but then I've got OS X versions of the packages which may eventually cause a problem.

What am I doing wrong here?

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"

Solution

  • The last step is to ADD . $APP_HOME. You also mention that node_modules folder is empty in your local tree. Does that mean node_modules exists still as an empty folder?

    If this is true, then the node_modules empty folder is likely getting copied over during the ADD step and overwriting everything that was done in the previous yarn step.