gpt4 book ai didi

node.js - 找不到模块 'express'(带有 docker 的 Node 应用程序)

转载 作者:搜寻专家 更新时间:2023-11-01 00:17:02 28 4
gpt4 key购买 nike

我是 Docker 的新手,我正在尝试从 NodeJS 开始,所以这是我的问题..

我的项目中有这个 Dockerfile:

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 运行容器时,它工作正常..

但是当我尝试将我的宿主项目映射到容器目录时(docker run -p 49160:8080 -v ~/Documentos/nodeApp:/home/Documents/node-app node-cont ) 它不起作用。

我得到的错误是:Error: Cannot find module 'express'

我已经尝试过相关问题的其他解决方案,但似乎对我没有任何作用(或者我知道..我只是太菜鸟了)

谢谢!!

最佳答案

当您使用 -v 标志运行您的容器时,这意味着将目录从您的 Docker 引擎的主机挂载到容器中,将覆盖您在 /home/Documents/node- 中所做的操作应用,例如 npm install。

所以在容器中是看不到node_modules目录的。

$ 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 .正如文档所说,主机目录的预先存在的内容不会被删除,但没有关于容器现有目录发生了什么的信息。

有一个例子可以支持我的观点。

  1. docker 文件

    来自 Alpine :最新
    WORKDIR/usr/src/应用程序
    复制 。 .

  2. 我在 Dockerfile 的同一目录中创建了一个 test.t 文件。

  3. 证明

    1. 运行命令 docker build -t test-1 .
    2. 运行命令docker run --name test-c-1 -it test-1/bin/sh,然后你的容器将打开bash。
    3. 在您的容器 bash 中运行命令 ls -l,它将显示 test.t 文件。
    4. 只需使用相同的图片即可。
    5. 运行命令 docker run --name test-c-2 -v/home:/usr/src/app -it test-1/bin/sh。您在 test-c-2 容器中找不到文件 test.t

就这些了,希望对你有帮助。

关于node.js - 找不到模块 'express'(带有 docker 的 Node 应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39419295/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com