Install node_modules inside Docker container and synchronize them with host

前端 未结 10 1376
旧巷少年郎
旧巷少年郎 2020-12-12 11:33

I have the problem with installing node_modules inside the Docker container and synchronize them with the host. My Docker\'s version is 18.03.1-ce, build

10条回答
  •  自闭症患者
    2020-12-12 11:53

    Having run into this issue and finding the accepted answer pretty slow to copy all node_modules to the host in every container run, I managed to solve it by installing the dependencies in the container, mirror the host volume, and skip installing again if a node_modules folder is present:

    Dockerfile:

    FROM node:12-alpine
    
    WORKDIR /usr/src/app
    
    CMD [ -d "node_modules" ] && npm run start || npm ci && npm run start
    

    docker-compose.yml:

    version: '3.8'
    
    services:
      service-1:
        build: ./
        volumes:
          - ./:/usr/src/app
    

    When you need to reinstall the dependencies just delete node_modules.

提交回复
热议问题