【发布时间】:2021-07-23 09:09:03
【问题描述】:
我似乎无法获取我的 node:alpine Docker 映像,以便构建 react/next.js。
我可以让它在本地构建良好,但是当 Docker 构建到达 API 端点时,我从未在我的 API 日志中看到流量,该端点本身在 Docker 容器(nginx、headless craft cms 等)中运行。 )。
构建似乎根本不喜欢 localhost,因为我已经尝试过:
http://localhost:9000/api
...这是我在日志中收到的消息:
#15 13.76 > Build error occurred
#15 13.76 FetchError: request to http://localhost:9000/api failed, reason: connect ECONNREFUSED 127.0.0.1:9000
我听说我可以使用主机名代替“localhost”,所以在我的 MacOS 终端中,我输入了“主机名”,然后我将 localhost 换成了这个值。这不会完全出错,但构建会挂在构建的“生成页面...”步骤上。
如何让 Docker 构建识别 localhost,或者换句话说,如何将我的 API URL 设置为由本地 Docker 容器托管的端点?
这是我的 Dockerfile:
# Install dependencies only when needed
FROM node:alpine AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn --frozen-lockfile
# Rebuild the source code only when needed
FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn static
# Production image, copy all the files and run next
FROM node:alpine AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
# You only need to copy next.config.js if you are NOT using the default configuration
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
USER nextjs
EXPOSE 3000
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# ENV NEXT_TELEMETRY_DISABLED 1
CMD ["yarn", "start"]
【问题讨论】:
标签: node.js reactjs docker next.js alpine