next.js在docker图像构建过程中生成静态页面时构建错误

发布于 2025-02-10 12:12:32 字数 3191 浏览 1 评论 0原文

Windows
[email  procected] [email  procected

]我正面临着当地主机的码头建筑问题。该站点利用getstaticProps,因此在构建时间内生成了HTML。 http:// localhost:7071有一个本地托管的无头CMS,该网站使用该网站来维护页面信息。

当不使用Docker时构建站点时,它可以正常工作,并且生成静态内容;但是,当构建Docker映像时,拒绝与CMS的连接。

我知道这与无法访问无头CM的Docker Build流程有关,但是我已经耗尽了知识,以了解如何获取构建过程以访问Localhost。

dockerfile:

FROM node:18-alpine as dependencies

WORKDIR /app
COPY package.json .
RUN npm install --legacy-peer-deps

FROM node:18-alpine as builder

WORKDIR /app

# Package files
COPY tsconfig.json .
COPY package.json .
COPY package-lock.json .

# Files required for next
COPY next-env.d.ts .
COPY next-sitemap.config.js .
COPY next.config.redirects.js .
COPY next.config.rewrites.js .
COPY next.config.js .

# Linter
COPY .eslintrc.json .

# Environment loader
COPY loadAppEnv.js .

# Program files
COPY src/ ./src/
COPY pages/ ./pages/
COPY public/ ./public/
COPY environments/ ./environments/

COPY --from=dependencies /app/node_modules ./node_modules
RUN npm run build:local
RUN npm run sitemap

FROM node:18-alpine as runner

WORKDIR /app
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/next.config.redirects.js ./
COPY --from=builder /app/next.config.rewrites.js ./
COPY --from=builder /app/loadAppEnv.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/sshd_config /etc/ssh/

# Install and configure SSH
RUN apk add openssh \
    && echo "root:Docker!" | chpasswd

# Configure running
EXPOSE 3000
CMD [ "npm", "run", "start:local"]

docker-compose.yml

services:
  web:
    build:
      network: "host"
      dockerfile: Dockerfile
      context: .
    network_mode: "host"

错误我正在遇到:

#26 21.25 info  - Collecting page data...
#26 21.75 (node:80) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
#26 21.75 (Use `node --trace-warnings ...` to show where the warning was created)
#26 21.77
#26 21.77 > Build error occurred
#26 21.77 TypeError: fetch failed
#26 21.77     at Object.processResponse (node:internal/deps/undici/undici:5555:34)
#26 21.77     at node:internal/deps/undici/undici:5877:42
#26 21.77     at node:internal/process/task_queues:140:7
#26 21.77     at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
#26 21.77     at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
#26 21.77     at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
#26 21.77   type: 'TypeError',
#26 21.77   cause: {
#26 21.77     errno: -111,
#26 21.77     code: 'ECONNREFUSED',
#26 21.77     syscall: 'connect',
#26 21.77     address: '127.0.0.1',
#26 21.77     port: 7071
#26 21.77   }
#26 21.77 }

Windows
[email protected]
[email protected]

I am in the process of finalizing a Next.js migration and I am being faced with a localhost docker build issue. The site makes use of getStaticProps and therefore generates the html during build time. There is a locally hosted headless cms at http://localhost:7071 that the site makes use of to maintain the page information.

When the site is built when not making use of docker it works fine and the static content is generated; however, when when building a docker image, the connection to the cms is refused.

I know it has to do with the docker build process not being able to access the headless cms, but I have exhausted my knowledge in finding out how to get the build process to access the localhost.

Dockerfile:

FROM node:18-alpine as dependencies

WORKDIR /app
COPY package.json .
RUN npm install --legacy-peer-deps

FROM node:18-alpine as builder

WORKDIR /app

# Package files
COPY tsconfig.json .
COPY package.json .
COPY package-lock.json .

# Files required for next
COPY next-env.d.ts .
COPY next-sitemap.config.js .
COPY next.config.redirects.js .
COPY next.config.rewrites.js .
COPY next.config.js .

# Linter
COPY .eslintrc.json .

# Environment loader
COPY loadAppEnv.js .

# Program files
COPY src/ ./src/
COPY pages/ ./pages/
COPY public/ ./public/
COPY environments/ ./environments/

COPY --from=dependencies /app/node_modules ./node_modules
RUN npm run build:local
RUN npm run sitemap

FROM node:18-alpine as runner

WORKDIR /app
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/next.config.redirects.js ./
COPY --from=builder /app/next.config.rewrites.js ./
COPY --from=builder /app/loadAppEnv.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/sshd_config /etc/ssh/

# Install and configure SSH
RUN apk add openssh \
    && echo "root:Docker!" | chpasswd

# Configure running
EXPOSE 3000
CMD [ "npm", "run", "start:local"]

docker-compose.yml

services:
  web:
    build:
      network: "host"
      dockerfile: Dockerfile
      context: .
    network_mode: "host"

Error I am getting:

#26 21.25 info  - Collecting page data...
#26 21.75 (node:80) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
#26 21.75 (Use `node --trace-warnings ...` to show where the warning was created)
#26 21.77
#26 21.77 > Build error occurred
#26 21.77 TypeError: fetch failed
#26 21.77     at Object.processResponse (node:internal/deps/undici/undici:5555:34)
#26 21.77     at node:internal/deps/undici/undici:5877:42
#26 21.77     at node:internal/process/task_queues:140:7
#26 21.77     at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
#26 21.77     at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
#26 21.77     at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
#26 21.77   type: 'TypeError',
#26 21.77   cause: {
#26 21.77     errno: -111,
#26 21.77     code: 'ECONNREFUSED',
#26 21.77     syscall: 'connect',
#26 21.77     address: '127.0.0.1',
#26 21.77     port: 7071
#26 21.77   }
#26 21.77 }

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

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

发布评论

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

评论(1

黑白记忆 2025-02-17 12:12:32

紧随汉斯·基利安(Hans Kilian)的答案,我需要从http:// localhost:7071 http://host.docker.internal.internal.internal.internal.70071 7071 7071 7071 7071 /代码>。

Following on from Hans Kilian's answer, I needed to change the reference url to the api from http://localhost:7071 to http://host.docker.internal:7071.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文