运行容器错误时Docker错误:找不到模块' /home/app/server.js'

发布于 2025-02-01 18:05:27 字数 1223 浏览 3 评论 0原文

努力寻找问题

我会遇到以下错误,并在使用以下Dockerfile成功构建图像后 使用的命令

docker build -t my -app:1.0。

FROM node:13-alpine

# run something in the docker image
RUN mkdir -p /home/app

# this allows you to copy something from the host into docker image
COPY . /home/app

# inside the container it runs the command node server.js
# cmd is an entry point command, whereas run as used later
CMD ["node","/home/app/server.js"]

我已经运行以下命令来运行容器

Docker运行my-app:1.0

它会引发以下错误:

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
192:docker_compose_demo tech$ docker run my-app:1.0
internal/modules/cjs/loader.js:965
  throw err;
  ^

Error: Cannot find module '/home/app/server.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:962:15)
    at Function.Module._load (internal/modules/cjs/loader.js:838:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

我更改了Dockerfile的路径,但没有找到它。 有人可以告诉我在做什么错吗?

I'm getting the following error and I'm struggling to find the issue

After successfully building my image using the following Dockerfile
Command used

docker build -t my-app:1.0 .

FROM node:13-alpine

# run something in the docker image
RUN mkdir -p /home/app

# this allows you to copy something from the host into docker image
COPY . /home/app

# inside the container it runs the command node server.js
# cmd is an entry point command, whereas run as used later
CMD ["node","/home/app/server.js"]

I have run the following command to run the container

docker run my-app:1.0

it throws the following error:

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
192:docker_compose_demo tech$ docker run my-app:1.0
internal/modules/cjs/loader.js:965
  throw err;
  ^

Error: Cannot find module '/home/app/server.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:962:15)
    at Function.Module._load (internal/modules/cjs/loader.js:838:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

I changed the path in the Dockerfile and yet is not finding it.
Can someone tell what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吝吻 2025-02-08 18:05:27

首先是,使用workdir而不是运行mkdir
为什么使用workdir

WorkDir指令为任何运行CMD,
入门点,复制并添加以下说明
Dockerfile。如果不存在WorkDir,即使
它在随后的任何Dockerfile指令中均未使用

您可以参考下面的Dockerfile-

FROM node:13-alpine

# Creates directory and sets it as current working directory
WORKDIR /home/app

# Copy everything from local current directory to docker/image current directory
COPY . .

# inside the container it runs the command node server.js
# cmd is an entry point command, whereas run as used later
CMD ["node","server.js"]

The first thing is, to use the WORKDIR instead of running mkdir.
Why use WORKDIR ?

The WORKDIR instruction sets the working directory for any RUN, CMD,
ENTRYPOINT, COPY and ADD instructions that follow it in the
Dockerfile. If the WORKDIR doesn’t exist, it will be created even if
it’s not used in any subsequent Dockerfile instruction

You can refer the below Dockerfile -

FROM node:13-alpine

# Creates directory and sets it as current working directory
WORKDIR /home/app

# Copy everything from local current directory to docker/image current directory
COPY . .

# inside the container it runs the command node server.js
# cmd is an entry point command, whereas run as used later
CMD ["node","server.js"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文