下一个 JS 应用程序在本地构建但在 Docker 内部失败

Next JS app builds in the local but failed inside Docker

我正在从事下一个 JS 项目。我可以在我的本地机器上成功构建它。但是,当我尝试 docker 化应用程序并在 docker 图像中构建应用程序时,我在构建应用程序时遇到错误。从错误我认为 docker 构建找不到我的静态图像文件。但是我可以浏览docker image.

里面的文件

我的文件结构

Dockerfile

FROM node:18-alpine3.14

RUN apk update && \
    apk add --no-cache libc6-compat

ENV NEXT_TELEMETRY_DISABLED 1
ENV NODE_ENV production
ENV PORT 3000
EXPOSE 3000

COPY . /app

WORKDIR /app


RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser


RUN yarn install
RUN yarn build


ENTRYPOINT [ "sh", "entrypoint.sh" ]

entrypoint.sh

yarn start

构建时出错

info  - Creating an optimized production build
Failed to compile.

./assets/company/employee/group.jpg
TypeError: Failed to parse URL from /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.wasm
    at new Request (node:internal/deps/undici/undici:4816:19)
    at Agent2.fetch2 (node:internal/deps/undici/undici:5544:29)
    at Object.fetch (node:internal/deps/undici/undici:6372:20)
    at fetch (node:internal/bootstrap/pre_execution:199:25)
    at instantiateAsync (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:424:28)
    at createWasm (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:447:13)
    at /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:1480:19
    at Object.instantiateEmscriptenWasm (/app/node_modules/next/dist/server/lib/squoosh/emscripten-utils.js:15:12)
    at Object.dec (/app/node_modules/next/dist/server/lib/squoosh/codecs.js:156:42)
    at Object.decodeBuffer (/app/node_modules/next/dist/server/lib/squoosh/impl.js:55:31)

Import trace for requested module:
./pages/ourcompany/OurStory.js

./assets/home/clientFeedBack/image/Ellipse 6-2.jpg
TypeError: Failed to parse URL from /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.wasm
    at new Request (node:internal/deps/undici/undici:4816:19)
    at Agent2.fetch2 (node:internal/deps/undici/undici:5544:29)
    at Object.fetch (node:internal/deps/undici/undici:6372:20)
    at fetch (node:internal/bootstrap/pre_execution:199:25)
    at instantiateAsync (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:424:28)
    at createWasm (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:447:13)
    at /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:1480:19
    at Object.instantiateEmscriptenWasm (/app/node_modules/next/dist/server/lib/squoosh/emscripten-utils.js:15:12)
    at Object.dec (/app/node_modules/next/dist/server/lib/squoosh/codecs.js:156:42)
    at Object.decodeBuffer (/app/node_modules/next/dist/server/lib/squoosh/impl.js:55:31)

Import trace for requested module:
./components/ClientFeedback.js
./pages/index.js

./assets/home/globalSection/Rectangle 32.jpg
TypeError: Failed to parse URL from /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.wasm
    at new Request (node:internal/deps/undici/undici:4816:19)
    at Agent2.fetch2 (node:internal/deps/undici/undici:5544:29)
    at Object.fetch (node:internal/deps/undici/undici:6372:20)
    at fetch (node:internal/bootstrap/pre_execution:199:25)
    at instantiateAsync (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:424:28)
    at createWasm (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:447:13)
    at /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:1480:19
    at Object.instantiateEmscriptenWasm (/app/node_modules/next/dist/server/lib/squoosh/emscripten-utils.js:15:12)
    at Object.dec (/app/node_modules/next/dist/server/lib/squoosh/codecs.js:156:42)
    at Object.decodeBuffer (/app/node_modules/next/dist/server/lib/squoosh/impl.js:55:31)

Import trace for requested module:
./pages/GlobalFamily.js

./assets/home/serviceSection/bg.jpg
TypeError: Failed to parse URL from /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.wasm
    at new Request (node:internal/deps/undici/undici:4816:19)
    at Agent2.fetch2 (node:internal/deps/undici/undici:5544:29)
    at Object.fetch (node:internal/deps/undici/undici:6372:20)
    at fetch (node:internal/bootstrap/pre_execution:199:25)
    at instantiateAsync (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:424:28)
    at createWasm (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:447:13)
    at /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:1480:19
    at Object.instantiateEmscriptenWasm (/app/node_modules/next/dist/server/lib/squoosh/emscripten-utils.js:15:12)
    at Object.dec (/app/node_modules/next/dist/server/lib/squoosh/codecs.js:156:42)
    at Object.decodeBuffer (/app/node_modules/next/dist/server/lib/squoosh/impl.js:55:31)

Import trace for requested module:
./pages/Services.js

./assets/home/serviceViewSection/Rectangle 420.jpg
TypeError: Failed to parse URL from /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.wasm
    at new Request (node:internal/deps/undici/undici:4816:19)
    at Agent2.fetch2 (node:internal/deps/undici/undici:5544:29)
    at Object.fetch (node:internal/deps/undici/undici:6372:20)
    at fetch (node:internal/bootstrap/pre_execution:199:25)
    at instantiateAsync (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:424:28)
    at createWasm (/app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:447:13)
    at /app/node_modules/next/dist/server/lib/squoosh/mozjpeg/mozjpeg_node_dec.js:1480:19
    at Object.instantiateEmscriptenWasm (/app/node_modules/next/dist/server/lib/squoosh/emscripten-utils.js:15:12)
    at Object.dec (/app/node_modules/next/dist/server/lib/squoosh/codecs.js:156:42)
    at Object.decodeBuffer (/app/node_modules/next/dist/server/lib/squoosh/impl.js:55:31)

Import trace for requested module:
./pages/ServiceView.js


> Build failed because of webpack errors

非常感谢您的帮助。

谢谢

问题出在 alpine 发行版上。它是一个基本的 linux 包,没有任何依赖性。我们需要在这里安装一些依赖,专门用来处理JPEG图片。

更新代码: Dockerfile

FROM node:lts-alpine3.14

RUN apk update && \
    apk add --no-cache libc6-compat autoconf automake libtool make tiff jpeg zlib zlib-dev pkgconf nasm file gcc musl-dev

# COPY package.json .

RUN npm install -g npm

ENV NEXT_TELEMETRY_DISABLED 1

ENV NODE_ENV production
ENV PORT 3000
EXPOSE 3000

COPY . /app

WORKDIR /app

RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser


RUN yarn install
RUN yarn build
ENTRYPOINT [ "sh", "entrypoint.sh" ]