找不到模块“表达”(带有Docker的节点应用) [英] Cannot finde module 'express' (node app with docker)
问题描述
我是Docker的新手,我想从NodeJS开始,所以这是我的问题。
I'm a newbie with Docker and I'm trying to start with NodeJS so here is my question..
我的项目中有这个Dockerfile:
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"]
当我使用 docker run -d -p 49160:8080 node-container
运行容器时,它工作正常。
When I run a container with docker run -d -p 49160:8080 node-container
it works fine..
但是当我尝试使用容器目录映射我的宿主项目时( docker run -p 49160:8080 -v〜/ Documentos / nodeApp:/ home / Documents / node-app node-cont
)无效。
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.
我得到的错误是: Error :找不到模块'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)
谢谢!!
推荐答案
使用 -v 标志运行容器时,这意味着将Docker引擎主机中的目录装载到容器中,将覆盖您在 / home / Documents /节点应用,例如npm install。
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.
因此您看不到 n容器中的ode_modules目录。
$ docker run -d -P --name web -v / src / webapp: / webapp training / webapp python app.py
$ docker run -d -P --name web -v /src/webapp:/webapp training/webapp python app.py
此命令将主机目录/ src / webapp挂载到/ webapp的容器中。如果路径/ webapp已存在于容器的映像中,则/ src / webapp安装会覆盖,但不会删除先前存在的内容。除去安装后,即可再次访问内容。这与mount命令的预期行为一致。
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.
-
Dockerfile
Dockerfile
FROM alpine:latest
WORKDIR / usr / src / app
COPY。 。
我在Dockerfile的同一目录中创建了一个 test.t 文件。
I create a test.t file in the same directory of Dockerfile.
证明
- 运行命令
docker build -t test-1。
- 运行命令
docker run --name test-c-1 -it test-1 / bin / sh
,那么您的容器将打开bash。 - 在容器bash中运行命令
ls -l </ code>,它将显示test.t文件。
- 只使用同一张图片。
- 运行命令
docker run --name test-c -2 -v / home:/ usr / src / app -it test-1 / bin / sh
。您无法在 test-c-2 容器中找到文件 test.t 。
- Run command
docker build -t test-1 .
- Run command
docker run --name test-c-1 -it test-1 /bin/sh
,then your container will open bash. - Run command
ls -l
in your container bash,it will show test.t file. - Just use the same image.
- 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.
这篇关于找不到模块“表达”(带有Docker的节点应用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!