Sveltekit docker 容器启动时出错

发布于 2025-01-10 23:03:09 字数 1571 浏览 0 评论 0原文

我正在尝试从我的 sveltekit 应用程序获取 docker 容器。 我在 svelte.config.js 中使用节点适配器

import adapter from '@sveltejs/adapter-node'
adapter: adapter(),

,我的 Dockerfile 如下所示。

FROM node:16-alpine

WORKDIR /app
COPY package*.json ./

RUN npm install
COPY . ./

RUN npm run build

EXPOSE 3000

CMD [ "node", "build" ]

启动容器时出现此错误:

node:internal/errors:464
2022-03-02T09:51:03.597044200Z     ErrorCaptureStackTrace(err);
2022-03-02T09:51:03.597088700Z     ^
2022-03-02T09:51:03.597100400Z 
2022-03-02T09:51:03.597116300Z Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/build/server/index.js' imported from /app/build/index.js
2022-03-02T09:51:03.597144200Z     at new NodeError (node:internal/errors:371:5)
2022-03-02T09:51:03.597193600Z     at finalizeResolution (node:internal/modules/esm/resolve:418:11)
2022-03-02T09:51:03.597270100Z     at moduleResolve (node:internal/modules/esm/resolve:981:10)
2022-03-02T09:51:03.597285100Z     at defaultResolve (node:internal/modules/esm/resolve:1078:11)
2022-03-02T09:51:03.597298100Z     at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
2022-03-02T09:51:03.597311100Z     at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
2022-03-02T09:51:03.597324900Z     at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
2022-03-02T09:51:03.597336000Z     at link (node:internal/modules/esm/module_job:78:36) {
2022-03-02T09:51:03.597345300Z   code: 'ERR_MODULE_NOT_FOUND'
2022-03-02T09:51:03.597359500Z }

I'm trying to get a docker container from my sveltekit app.
I'm using the node adapter in my svelte.config.js

import adapter from '@sveltejs/adapter-node'
adapter: adapter(),

and my Dockerfile looks like this.

FROM node:16-alpine

WORKDIR /app
COPY package*.json ./

RUN npm install
COPY . ./

RUN npm run build

EXPOSE 3000

CMD [ "node", "build" ]

I'm getting this error when starting up the container:

node:internal/errors:464
2022-03-02T09:51:03.597044200Z     ErrorCaptureStackTrace(err);
2022-03-02T09:51:03.597088700Z     ^
2022-03-02T09:51:03.597100400Z 
2022-03-02T09:51:03.597116300Z Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/build/server/index.js' imported from /app/build/index.js
2022-03-02T09:51:03.597144200Z     at new NodeError (node:internal/errors:371:5)
2022-03-02T09:51:03.597193600Z     at finalizeResolution (node:internal/modules/esm/resolve:418:11)
2022-03-02T09:51:03.597270100Z     at moduleResolve (node:internal/modules/esm/resolve:981:10)
2022-03-02T09:51:03.597285100Z     at defaultResolve (node:internal/modules/esm/resolve:1078:11)
2022-03-02T09:51:03.597298100Z     at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
2022-03-02T09:51:03.597311100Z     at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
2022-03-02T09:51:03.597324900Z     at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
2022-03-02T09:51:03.597336000Z     at link (node:internal/modules/esm/module_job:78:36) {
2022-03-02T09:51:03.597345300Z   code: 'ERR_MODULE_NOT_FOUND'
2022-03-02T09:51:03.597359500Z }

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

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

发布评论

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

评论(1

落在眉间の轻吻 2025-01-17 23:03:09

您可以构建一个临时 docker 映像,然后首先在该映像中构建您的项目。之后,您可以使用之前的构建映像创建最终的生产 docker 映像。所以,dockerfile 应该如下所示。

FROM node:alpine as build
WORKDIR /my-project
COPY . .
RUN npm ci --development --silent

FROM node:alpine as prod
COPY ./package*.json ./
RUN npm ci --production --silent --ignore-scripts
COPY --from=build /my-project/build ./build
EXPOSE 3000
USER node
CMD ["node", "./build"]

You can build a temporary docker image and build your project inside that image first. After, you can create your final production docker image using the prior build image. So, dockerfile should be like the below.

FROM node:alpine as build
WORKDIR /my-project
COPY . .
RUN npm ci --development --silent

FROM node:alpine as prod
COPY ./package*.json ./
RUN npm ci --production --silent --ignore-scripts
COPY --from=build /my-project/build ./build
EXPOSE 3000
USER node
CMD ["node", "./build"]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文