[英]nodejs in docker ubuntu cannot find module /usr/src/app/index.js
I'm trying to deploy an application I wrote to my unraid server so I had to docker-ize it.我正在尝试将我写的应用程序部署到我的 unraid 服务器上,所以我不得不对其进行 docker 化。 It's written with nodejs and depends on imagemagick and ghostscript so I had to include a build step to install those dependencies.它是用 nodejs 编写的,并且依赖于 imagemagick 和 ghostscript,所以我必须包含一个构建步骤来安装这些依赖项。 I'm seeing an error when running this image though我在运行此图像时看到错误
Here's my dockerfile这是我的 dockerfile
FROM node
RUN mkdir -p /usr/src/app
RUN chmod -R 777 /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm i --only=production
FROM ubuntu
RUN apt-get update
RUN apt-get install -y imagemagick ghostscript nodejs
ENTRYPOINT node /usr/src/app/index.js
Console output控制台 output
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module '/usr/src/app/index.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Originally, my entrypoint was configured like ENTRYPOINT node./index.js
but I thought that I was in the wrong directory or something but switching to an absolute path didn't work either so here I am.最初,我的入口点配置为ENTRYPOINT node./index.js
但我认为我在错误的目录中,但切换到绝对路径也不起作用所以我在这里。
By using a second from instruction, you are instrucing a second stage.通过使用第二个指令,您正在指导第二个阶段。 Nothing from the first stage is availble to the second stage by default.默认情况下,第一阶段的任何内容都无法用于第二阶段。 If you need some artifacts you need to copy them explicitly.如果您需要一些工件,则需要显式复制它们。
FROM node as build
RUN mkdir -p /usr/src/app
RUN chmod -R 777 /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm i --only=production
# this is a new stage
FROM ubuntu
RUN apt-get update
RUN apt-get install -y imagemagick ghostscript nodejs
# you need to copy the things you need
COPY --from=builder /usr/src/app /usr/src/app
ENTRYPOINT node /usr/src/app/index.js
That said, it seems pointless for a node app to do that.也就是说,节点应用程序这样做似乎毫无意义。 I would suggest using a single stage.我建议使用单个阶段。 The node runtime is required to run your app.运行您的应用程序需要节点运行时。 Multi staging would make sense if you were to use node to build statics with something like webpack and then copy the produced statics into a second stage that doesn't need the node runtime.如果您要使用节点来构建具有 webpack 之类的静态数据,然后将生成的静态数据复制到不需要节点运行时的第二阶段,那么多阶段将是有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.