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 9ee9f40 and Docker Compose's version is  1.21.2, build a133471.
My docker-compose.yml looks like:
# Frontend Container.
frontend:
  build: ./app/frontend
  volumes:
    - ./app/frontend:/usr/src/app
    - frontend-node-modules:/usr/src/app/node_modules
  ports:
    - 3000:3000
  environment:
    NODE_ENV: ${ENV}
  command: npm start
# Define all the external volumes.
volumes:
  frontend-node-modules: ~
My Dockerfile:
# Set the base image.
FROM node:10
# Create and define the working directory.
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
# Install the application's dependencies.
COPY package.json ./
COPY package-lock.json ./
RUN npm install
The trick with the external volume is described in a lot of blog posts and Stack Overflow answers. For example, this one.
The application works great. The source code is synchronized. The hot reloading works great too.
The only problem that I have is that node_modules folder is empty on the host. Is it possible to synchronize the node_modules folder that is inside Docker container with the host?
I've already read these answers:
- docker-compose volume on node_modules but is empty
- Accessing node_modules after npm install inside Docker
Unfortunately, they didn't help me a lot. I don't like the first one, because I don't want to run npm install on my host because of the possible cross-platform issues (e.g. the host is Windows or Mac and the Docker container is Debian 8 or Ubuntu 16.04). The second one is not good for me too, because I'd like to run npm install in my Dockerfile instead of running it after the Docker container is started.
Also, I've found this blog post. The author tries to solve the same problem I am faced with. The problem is that node_modules won't be synchronized because we're just copying them from the Docker container to the host.
I'd like my node_modules inside the Docker container to be synchronized with the host. Please, take into account that I want:
- to install node_modulesautomatically instead of manually
- to install node_modulesinside the Docker container instead of the host
- to have node_modulessynchronized with the host (if I install some new package inside the Docker container, it should be synchronized with the host automatically without any manual actions)
I need to have node_modules on the host, because:
- possibility to read the source code when I need
- the IDE needs node_modulesto be installed locally so that it could have access to thedevDependenciessuch aseslintorprettier. I don't want to install thesedevDependenciesglobally.
 
     
     
     
     
     
     
     
     
     
     
     
     
    