Search code examples
node.jsdockerdocker-composedocker-volume

docker-compose volume on node_modules but is empty


I'm pretty new with Docker and i wanted to map the node_modules folder on my computer (for debugging purpose).

This is my docker-compose.yml

web:
  build: .
  ports:
    - "3000:3000"
  links:
    - db
  environment:
    PORT: 3000
  volumes:
    - .:/usr/src/app
    - /usr/src/app/node_modules
db:
  image: mongo:3.3
  ports:
    - "27017:27017"
  command: "--smallfiles --logpath=/dev/null"

I'm with Docker for Mac. When i run docker-compose up -d all go right, but it create a node_modules folder on my computer but it's empty. I go into the bash of my container and ls node_modules, all the packages was there.

How can i get the content on the container on my computer too?

Thank you


Solution

  • TL;DR Working example, clone and try: https://github.com/xbx/base-server


    You need a node_modules in your computer (outside image) for debugging purposes first (before run the container).

    If you want debug only node_modules:

    volumes:
        - /path/to/node_modules:/usr/src/app/node_modules
    

    If you want debug both your code and the node_modules:

    volumes:
        - .:/usr/src/app/
    

    Remember that you will need run npm install at least one time outside the container (or copy the node_modules directory that the docker build generates). Let me now if you need more details.


    Edit. So, without the need of npm in OSX, you can:

    1. docker build and then docker cp <container-id>:/path/to/node-modules ./local-node-modules/. Then in your docker-compose.yml mount those files and troubleshot whatever you want.
    2. Or, docker build and there (Dockerfile) do the npm install in another directory. Then in your command (CMD or docker-compose command) do the copy (cp) to the right directory, but this directory is mounted empty from your computer (a volume in the docker-compose.yml) and then troubleshot whatever you want.

    Edit 2. (Option 2) Working example, clone and try: https://github.com/xbx/base-server I did it all automatically in this repo forked from the yours.

    Dockerfile

    FROM node:6.3
    
    # Install app dependencies
    RUN mkdir /build-dir
    WORKDIR /build-dir
    COPY package.json /build-dir
    RUN npm install -g babel babel-runtime babel-register mocha nodemon
    RUN npm install
    
    # Create app directory
    RUN mkdir -p /usr/src/app
    WORKDIR /usr/src/app
    RUN ln -s /build-dir/node_modules node_modules
    
    # Bundle app source
    COPY . /usr/src/app
    
    EXPOSE 1234
    CMD [ "npm", "start" ]
    

    docker-compose.yml

    web:
      build: .
      ports:
        - "1234:1234"
      links:
        - db # liaison avec la DB
      environment:
        PORT: 1234
      command: /command.sh
      volumes:
        - ./src/:/usr/src/app/src/
        - ./node_modules:/usr/src/app/node_modules
        - ./command.sh:/command.sh
    db:
      image: mongo:3.3
      ports:
        - "27017:27017"
      command: "--smallfiles --logpath=/dev/null"
    

    command.sh

    #!/bin/bash
    
    cp -r /build-dir/node_modules/ /usr/src/app/
    
    exec npm start
    

    Please, clone my repo and do docker-compose up. It does what you want. PS: It can be improved to do the same in a better way (ie best practices, etc)

    I'm in OSX and it works for me.