Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot finde module 'express' (node app with docker)

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 !!

like image 306
Gerardo Tarragona Avatar asked Sep 09 '16 20:09

Gerardo Tarragona


People also ask

What is Docker Workdir?

The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.

Does node Docker image include NPM?

No, it doesn't come with npm.

What is Node JS in Docker?

Node. js is a JavaScript framework that is used for developing server-side applications. It is an open source framework that is developed to run on a variety of operating systems. Since Node. js is a popular framework for development, Docker has also ensured it has support for Node.


Video Answer


2 Answers

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.

like image 126
Mervyn Zhan Avatar answered Nov 16 '22 04:11

Mervyn Zhan


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."

like image 43
Yuvraj Jagga Avatar answered Nov 16 '22 03:11

Yuvraj Jagga