简体   繁体   中英

Docker not installing node_modules with yarn

I am running a Ruby on Rails app with Webpacker using Yarn and trying to run this on Docker via Docker Compose. When I run docker compose build it looks like it runs successfully, but the node_modules folder is nowhere to be found and I can't use any node packages. I tried adding - /app/node_modules to the volumes as suggested in other posts but to no avail.

FROM ruby:3.0.0
RUN bundle config --global frozen 1

WORKDIR /app

COPY Gemfile Gemfile.lock ./
RUN bundle install


COPY . .

RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get install -y nodejs
RUN npm install --global yarn
RUN yarn install

docker-compose.yml

version: "3.9"
services:
  web:
    build: .
    ports:
      - "3000:3000"
    command: "rails server -b 0.0.0.0"
    volumes:
      - ./:/app
      - /app/node_modules
  db:
    image: "postgres:13"
    ports:
      - "5432:5432"
    volumes:
      - ./pgdata:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password

Remove volumes from web service or mount only what you exactly need, not everything in the directory.
If you're mounting you're local directory, then it overwrite everything inside the container.

A good practice is to create .dockerignore file as well. Put there what shouldn't be copied from host machine like node_modules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM