Docker: mounting volume and run node apps

I am adding this answer just in case the accepted solution does not work for others as it did not for me. Though I confess I am NOT a docker nor a node expert.

This is what I had: Dockerfile

FROM node

WORKDIR /usr/src/

RUN npm install

EXPOSE 4200

CMD npm start

docker-compose.yml

version: "3.2"
services:
  frontend:
    ports:
      - "4200:4200"
    volumes:
      - ./frontend/:/usr/src

To make it work for me I had to remove RUN npm install from the Dockerfile and change the cmd to CMD npm install && npm start

I think the issue is that the volume is not mounted when RUN executes, but is only mounted right before CMD. Also note that you will have to re-build your image after editing Dockerfile.

This did work for me but again, I am no expert. So if anyone has any thoughts or ideas please feel free to explain in the comments or edit to improve the answer.


You can't do this VOLUME .:/src/ in a dockerfile. It's not legal syntax to specify a host mount point in a dockerfile as it would render it host dependent. Move the volume mapping definition to your docker-compose.yml file.


This is not the right way to use the instruction VOLUME in dockerfile. As documentation says “The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers”, and I don't think is what you want to do.

You don’t need to specify the VOLUME instruction and you don’t need to create the src directoy. Modify the Dockerfile as below:

FROM node:6.0.0-slim
MAINTAINER pyprism

# Install dependencies
WORKDIR /src/

RUN npm install

# Expose the app port
EXPOSE 8000

# Start the app
CMD npm start

Now you can navigate to the Dockerfile directory and build the image:

$docker build -t node .

Then run the container using the –v option to mount your local directory, as below:

 $docker run -p 8000:8000 -v path/to/code/dir:/src/ <image-name>

this will mount your code directory in /src.

if you want to use docker compose simply specify the volumes in the docker-compose.yml file:

 web:
  build: .
  volumes:
    - path/to/code/dir:/src/ 
  ports:
   - '8000:8000'