11

I'm a newbie with Docker and I'm trying to start with NodeJS so here is my question..

I have this Dockerfile inside my project:

FROM node:argon

# Create app directory
RUN mkdir -p /home/Documents/node-app
WORKDIR /home/Documents/node-app

# Install app dependencies
COPY package.json /home/Documents/node-app
RUN npm install

# Bundle app source
COPY . /home/Documents/node-app

EXPOSE 8080
CMD ["npm", "start"]

When I run a container with docker run -d -p 49160:8080 node-container it works fine..

But when I try to map my host project with the container directory (docker run -p 49160:8080 -v ~/Documentos/nodeApp:/home/Documents/node-app node-cont) it doesn't work.

The error I get is: Error: Cannot find module 'express'

I've tried with other solutions from related questions but nothing seems to work for me (or I know.. I'm just too rookie with this)

Thank you !!

1
  • 4
    I had this issue, I did npm install --save express and rebuilt: docker build -t <image name> . and it worked, hope this helps. Commented May 14, 2017 at 19:14

3 Answers 3

15

When you run your container with -v flag, which mean mount a directory from your Docker engine’s host into a container, will overwrite what you do in /home/Documents/node-app,such as npm install.

So you cannot see the node_modules directory in the container.

$ docker run -d -P --name web -v /src/webapp:/webapp training/webapp python app.py

This command mounts the host directory, /src/webapp, into the container at /webapp. If the path /webapp already exists inside the container’s image, the /src/webapp mount overlays but does not remove the pre-existing content. Once the mount is removed, the content is accessible again. This is consistent with the expected behavior of the mount command.

mount a host directory as a data volume.As what the docs said,the pre-existing content of host directory will not be removed, but no information about what's going on the exist directory of the container.

There is a example to support my opinion.

  1. Dockerfile

    FROM alpine:latest WORKDIR /usr/src/app COPY . .

  2. I create a test.t file in the same directory of Dockerfile.

  3. Proving

    1. Run command docker build -t test-1 .
    2. Run command docker run --name test-c-1 -it test-1 /bin/sh,then your container will open bash.
    3. Run command ls -l in your container bash,it will show test.t file.
    4. Just use the same image.
    5. Run command docker run --name test-c-2 -v /home:/usr/src/app -it test-1 /bin/sh. You cannot find the file test.t in your test-c-2 container.

That's all.I hope it will help you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. If the path /webapp already exists inside the container’s image, the /src/webapp mount overlays but does not remove the pre-existing content ----> So does the bind mount basically overwrite the COPY command? Your answer sounds ambiguous on that point.
You have saved me. In short, volume mount conflicts with COPY. So, the npm install modules cannot be found by the app!!
1

I recently faced the similar issue. Upon digging into docker docs I discovered that when you run the command

docker run -p 49160:8080 -v ~/Documentos/nodeApp:/home/Documents/node-app node-cont

the directory on your host machine ( left side of the ':' in the -v option argument ) will be mounted on the target directory ( in the container ) ##/home/Documents/node-app## and since your target directory is working directory and so non-empty, therefore

"the directory’s existing contents are obscured by the bind mount."

1 Comment

This is true when using docker for windows, I am not sure how the behaviour of bind mounts is when using MAC or linux.
0

I faced an alike problem recently. Turns out the problem was my package-lock.json, it was outdated in relation to the package.json and that was causing my packages not being downloaded while running npm install. I just deleted it and the build went ok.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.