Cannot finde module 'express' (node app wi

2019-04-14 22:27发布

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条回答
一夜七次
2楼-- · 2019-04-14 22:46

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.

查看更多
登录 后发表回答