我的 nextjs docker-compose 构建无法生成静态页面。 building.ECONNREFUSED 127.0.0.1:8080 时无法从另一个 hasura 容器获取数据
My nextjs docker-compose build fails to generate static pages. Can't get data from another hasura container while building.ECONNREFUSED 127.0.0.1:8080
我有一个依赖于 hasura 自托管实例的 nextjs 应用程序。两者都需要 运行 作为 docker 容器。我能够弥补 hasura 和 运行ning。当我手动构建 nextjs 时。有用。我能够从 hasure 渲染数据。当我尝试从 docker 文件或 docer 撰写时构建。它失败。我收到以下错误。
info - Checking validity of types...
info - Creating an optimized production build...
info - Compiled successfully
info - Collecting page data...
info - Generating static pages (0/14)
info - Generating static pages (3/14)
info - Generating static pages (6/14)
info - Generating static pages (10/14)
Error occurred prerendering page "/post/new". Read more: https://nextjs.org/docs/messages/prerender-error
FetchError: request to http://localhost:8080/v1/graphql failed, reason: connect ECONNREFUSED 127.0.0.1:8080
at ClientRequest.<anonymous> (/usr/src/app/1stkare/node_modules/.pnpm/node-fetch@2.6.1/node_modules/node-fetch/lib/index.js:1461:11)
at ClientRequest.emit (events.js:400:28)
at Socket.socketErrorListener (_http_client.js:475:9)
at Socket.emit (events.js:400:28)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:82:21)
info - Generating static pages (14/14)
> Build error occurred
Error: Export encountered errors on following paths:
/post/new
at /usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/export/index.js:473:19
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Span.traceAsyncFn (/usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/telemetry/trace/trace.js:60:20)
at async /usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/build/index.js:752:17
at async Span.traceAsyncFn (/usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/telemetry/trace/trace.js:60:20)
at async /usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/build/index.js:630:13
at async Span.traceAsyncFn (/usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/telemetry/trace/trace.js:60:20)
ERROR Command failed with exit code 1.
The command '/bin/sh -c pnpm build' returned a non-zero code: 1
我的 dockerhasura 的文件撰写
version: "3.8"
services:
postgres:
image: postgres:12
restart: always
ports:
- 5555:5432
expose:
- "5555"
volumes:
- db_data:/var/lib/postgresql/data
- ./db-script:/docker-entrypoint-initdb.d/
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
# POSTGRES_DB: 1stkare
APP_DB_USER: postgres
APP_DB_PASS: postgres
APP_DB_NAME: 1stkare
graphql-engine:
image: hasura/graphql-engine:v2.0.3
ports:
- "8080:8080"
depends_on:
- postgres
expose:
- "8080"
restart: always
environment:
DATABASE_URL: postgres://postgres:postgres@postgres:5432/1stkare
HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgres@postgres:5432/1stkare
HASURA_GRAPHQL_ENABLE_CONSOLE: "true"
HASURA_GRAPHQL_DEV_MODE: "true"
HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
HASURA_GRAPHQL_JWT_SECRET:wontsay
HASURA_GRAPHQL_UNAUTHORIZED_ROLE: anonymous
EVENT_WEBHOOK_SECRET: myeventwebhooksecret
EVENT_WEBHOOK_URL_ON_USER_UPDATE: http://host.docker.internal:3000/api/events/on-user-update
networks:
default:
external: true
name: dev_network
driver: bridge
volumes:
db_data:
我的 dockernextjs 应用文件
RUN mkdir -p /usr/src/app/1stkare
WORKDIR /usr/src/app/1stkare
RUN apk --no-cache add curl
RUN curl -f https://get.pnpm.io/v6.7.js | node - add --global pnpm
# Files required by pnpm install
COPY package.json pnpm-lock.yaml /usr/src/app/1stkare/
# RUN npm i -g pnpm
RUN pnpm install
COPY . /usr/src/app/1stkare
RUN pnpm build
EXPOSE 3000
CMD ["pnpm", "start"]
我的 dockernextjs 应用程序的文件撰写
version: '3.8'
services:
1stkare-ui:
build:
context: ./
environment:
- EVENT_WEBHOOK_SECRET=myeventwebhooksecret
- HASURA_ADMIN_SECRET=myadminsecretkey
- NEXT_PUBLIC_BASE_URL=http://localhost:3000
- NEXT_PUBLIC_HASURA_ENDPOINT=http://localhost:8080/v1/graphql
- SPACES_ENDPOINT=wontsay.wontsay.com
- SPACES_BUCKET=wontsay.wontsay
- SPACES_CDN_BASE_URL=https://wontsay.wontsay
ports:
- '3000:3000'
networks:
- default
expose:
- '3000'
container_name: 1stkare-ui
stdin_open: true
volumes:
- ./:/usr/src/app/1stkare
- /usr/src/app/1stkare/node_modules
networks:
default:
external: true
name: dev_network
driver: bridge
NEXT_PUBLIC_HASURA_ENDPOINT=http://localhost:8080/v1/graphql
的用法似乎是我假设的问题。当您在 Docker 中构建 Next.js 应用程序时,它无法访问 docker 网络之外的 http://localhost:8080
。
尝试将 Hasura 端点值替换为 http://host.docker.internal:8080/v1/graphql
。 (我假设您使用的是 macOS,因为之前在事件 webhook url env of hasura docker-compose 设置中使用了 host.docker.internal。
这样,Next.js docker build 将能够与 docker 沙箱外的网络 运行 通信。
我有一个依赖于 hasura 自托管实例的 nextjs 应用程序。两者都需要 运行 作为 docker 容器。我能够弥补 hasura 和 运行ning。当我手动构建 nextjs 时。有用。我能够从 hasure 渲染数据。当我尝试从 docker 文件或 docer 撰写时构建。它失败。我收到以下错误。
info - Checking validity of types...
info - Creating an optimized production build...
info - Compiled successfully
info - Collecting page data...
info - Generating static pages (0/14)
info - Generating static pages (3/14)
info - Generating static pages (6/14)
info - Generating static pages (10/14)
Error occurred prerendering page "/post/new". Read more: https://nextjs.org/docs/messages/prerender-error
FetchError: request to http://localhost:8080/v1/graphql failed, reason: connect ECONNREFUSED 127.0.0.1:8080
at ClientRequest.<anonymous> (/usr/src/app/1stkare/node_modules/.pnpm/node-fetch@2.6.1/node_modules/node-fetch/lib/index.js:1461:11)
at ClientRequest.emit (events.js:400:28)
at Socket.socketErrorListener (_http_client.js:475:9)
at Socket.emit (events.js:400:28)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
at processTicksAndRejections (internal/process/task_queues.js:82:21)
info - Generating static pages (14/14)
> Build error occurred
Error: Export encountered errors on following paths:
/post/new
at /usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/export/index.js:473:19
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Span.traceAsyncFn (/usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/telemetry/trace/trace.js:60:20)
at async /usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/build/index.js:752:17
at async Span.traceAsyncFn (/usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/telemetry/trace/trace.js:60:20)
at async /usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/build/index.js:630:13
at async Span.traceAsyncFn (/usr/src/app/1stkare/node_modules/.pnpm/next@11.0.2-canary.24_df9b789abc073be907b68727ab177fb5/node_modules/next/dist/telemetry/trace/trace.js:60:20)
ERROR Command failed with exit code 1.
The command '/bin/sh -c pnpm build' returned a non-zero code: 1
我的 dockerhasura 的文件撰写
version: "3.8"
services:
postgres:
image: postgres:12
restart: always
ports:
- 5555:5432
expose:
- "5555"
volumes:
- db_data:/var/lib/postgresql/data
- ./db-script:/docker-entrypoint-initdb.d/
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
# POSTGRES_DB: 1stkare
APP_DB_USER: postgres
APP_DB_PASS: postgres
APP_DB_NAME: 1stkare
graphql-engine:
image: hasura/graphql-engine:v2.0.3
ports:
- "8080:8080"
depends_on:
- postgres
expose:
- "8080"
restart: always
environment:
DATABASE_URL: postgres://postgres:postgres@postgres:5432/1stkare
HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgres@postgres:5432/1stkare
HASURA_GRAPHQL_ENABLE_CONSOLE: "true"
HASURA_GRAPHQL_DEV_MODE: "true"
HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
HASURA_GRAPHQL_JWT_SECRET:wontsay
HASURA_GRAPHQL_UNAUTHORIZED_ROLE: anonymous
EVENT_WEBHOOK_SECRET: myeventwebhooksecret
EVENT_WEBHOOK_URL_ON_USER_UPDATE: http://host.docker.internal:3000/api/events/on-user-update
networks:
default:
external: true
name: dev_network
driver: bridge
volumes:
db_data:
我的 dockernextjs 应用文件
RUN mkdir -p /usr/src/app/1stkare
WORKDIR /usr/src/app/1stkare
RUN apk --no-cache add curl
RUN curl -f https://get.pnpm.io/v6.7.js | node - add --global pnpm
# Files required by pnpm install
COPY package.json pnpm-lock.yaml /usr/src/app/1stkare/
# RUN npm i -g pnpm
RUN pnpm install
COPY . /usr/src/app/1stkare
RUN pnpm build
EXPOSE 3000
CMD ["pnpm", "start"]
我的 dockernextjs 应用程序的文件撰写
version: '3.8'
services:
1stkare-ui:
build:
context: ./
environment:
- EVENT_WEBHOOK_SECRET=myeventwebhooksecret
- HASURA_ADMIN_SECRET=myadminsecretkey
- NEXT_PUBLIC_BASE_URL=http://localhost:3000
- NEXT_PUBLIC_HASURA_ENDPOINT=http://localhost:8080/v1/graphql
- SPACES_ENDPOINT=wontsay.wontsay.com
- SPACES_BUCKET=wontsay.wontsay
- SPACES_CDN_BASE_URL=https://wontsay.wontsay
ports:
- '3000:3000'
networks:
- default
expose:
- '3000'
container_name: 1stkare-ui
stdin_open: true
volumes:
- ./:/usr/src/app/1stkare
- /usr/src/app/1stkare/node_modules
networks:
default:
external: true
name: dev_network
driver: bridge
NEXT_PUBLIC_HASURA_ENDPOINT=http://localhost:8080/v1/graphql
的用法似乎是我假设的问题。当您在 Docker 中构建 Next.js 应用程序时,它无法访问 docker 网络之外的 http://localhost:8080
。
尝试将 Hasura 端点值替换为 http://host.docker.internal:8080/v1/graphql
。 (我假设您使用的是 macOS,因为之前在事件 webhook url env of hasura docker-compose 设置中使用了 host.docker.internal。
这样,Next.js docker build 将能够与 docker 沙箱外的网络 运行 通信。