运行 multi-stacker docker built docker image fails – Nexts cannot find react module
Running multi-stacker docker built docker image fails – Nexts cannot find react module
我正在为使用 nextjs 并在 lerna monorepo 中进行预演的项目构建多阶段 docker 图像。
多阶段构建成功,但是,当我想要运行图像时,nextjs 抛出'react'找不到的错误。
多阶段构建的 Dockerfile:
FROM node:12 as builder
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
RUN npm -g config set user root && \
npm install -g lerna
WORKDIR /app
COPY . ./
ARG TARGET_APP_FOLDER=apps/app-1
# Install dependencies
COPY yarn.lock ./
WORKDIR /app/${TARGET_APP_FOLDER}
RUN lerna bootstrap \
-- --production
# Build
ARG PROJECT_ID
RUN test -n "$PROJECT_ID" || (echo "PROJECT_ID not set. Need to set PROJECT_ID to the GCP project ID you're deploying to" && false)
ENV PROJECT_ID=${PROJECT_ID}
RUN yarn run build:prod && \
/usr/local/bin/node-prune
################################################
# Our final image
FROM node:12-alpine
RUN yarn global add next
ARG TARGET_APP_FOLDER=apps/app-1
WORKDIR /app
# copy from build image
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/${TARGET_APP_FOLDER}/.next ./.next
COPY --from=builder /app/${TARGET_APP_FOLDER}/node_modules ./node_modules
COPY --from=builder /app/${TARGET_APP_FOLDER}/package.json ./package.json
ENV PORT=8080
CMD yarn run start -p ${PORT}
我最后验证了 react
模块已经复制到 node_modules 文件夹。 React 模块本身只是使用别名来使用 preact/compat
.
当 运行在单个构建器中整合所有内容时,它工作得非常好:
FROM node:12 as builder
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
RUN npm -g config set user root && \
npm install -g lerna
WORKDIR /app
COPY . ./
ARG TARGET_APP_FOLDER=apps/app-1
# Install dependencies
COPY yarn.lock ./
WORKDIR /app/${TARGET_APP_FOLDER}
RUN lerna bootstrap \
-- --production
# Build
ARG PROJECT_ID
RUN test -n "$PROJECT_ID" || (echo "PROJECT_ID not set. Need to set PROJECT_ID to the GCP project ID you're deploying to" && false)
ENV PROJECT_ID=${PROJECT_ID}
RUN yarn run build:prod && \
/usr/local/bin/node-prune
# Start the server
ENV PORT=8080
CMD yarn run start -p ${PORT}
已修复!
这是我现在正在使用的 dockerfile,以防有人遇到同样的问题:
FROM node:12-alpine AS base
# DEFAULT ARGS
ENV BUILD_FOLDER=/build
ENV TARGET_APP_FOLDER=/apps/my_app
# CHECK PROJECT ID IS SET
ARG PROJECT_ID
RUN test -n "$PROJECT_ID" || (echo "PROJECT_ID not set. Need to set PROJECT_ID to the GCP project ID you're deploying to" && false)
ENV PROJECT_ID=${PROJECT_ID}
###### BUILDER BASE PREPARING FILES ######
FROM base AS builder_base
WORKDIR /base
RUN apk update && \
apk add curl bash && \
apk --no-cache add g++ make libpng-dev && \
curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
# COPY YARN LOCK
COPY yarn.lock ./
# Copy source
COPY . ./
# Remove unused source
RUN bin/delete-unused-apps.sh ${TARGET_APP_FOLDER}
###### BUILDER ######
FROM builder_base AS build
WORKDIR ${BUILD_FOLDER}
COPY --from=builder_base /base ./
ENV PATH=${BUILD_FOLDER}/node_modules/.bin:$PATH
RUN yarn install --production
WORKDIR ${BUILD_FOLDER}/${TARGET_APP_FOLDER}
RUN yarn build:prod && \
/usr/local/bin/node-prune
###### RUNNER ######
FROM base AS runner
ARG PROJECT_ID
ENV PROJECT_ID=${PROJECT_ID}
ENV PATH=/app/node_modules/.bin:$PATH
WORKDIR /app
COPY --from=build ${BUILD_FOLDER}/node_modules ./node_modules
COPY --from=build ${BUILD_FOLDER}${TARGET_APP_FOLDER}/.next ./.next
COPY --from=build ${BUILD_FOLDER}${TARGET_APP_FOLDER}/public ./public
COPY --from=build ${BUILD_FOLDER}${TARGET_APP_FOLDER}/package*.json ./
# Start the server
ENV PORT=8080
CMD yarn run start -p ${PORT}
我正在为使用 nextjs 并在 lerna monorepo 中进行预演的项目构建多阶段 docker 图像。
多阶段构建成功,但是,当我想要运行图像时,nextjs 抛出'react'找不到的错误。
多阶段构建的 Dockerfile:
FROM node:12 as builder
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
RUN npm -g config set user root && \
npm install -g lerna
WORKDIR /app
COPY . ./
ARG TARGET_APP_FOLDER=apps/app-1
# Install dependencies
COPY yarn.lock ./
WORKDIR /app/${TARGET_APP_FOLDER}
RUN lerna bootstrap \
-- --production
# Build
ARG PROJECT_ID
RUN test -n "$PROJECT_ID" || (echo "PROJECT_ID not set. Need to set PROJECT_ID to the GCP project ID you're deploying to" && false)
ENV PROJECT_ID=${PROJECT_ID}
RUN yarn run build:prod && \
/usr/local/bin/node-prune
################################################
# Our final image
FROM node:12-alpine
RUN yarn global add next
ARG TARGET_APP_FOLDER=apps/app-1
WORKDIR /app
# copy from build image
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/${TARGET_APP_FOLDER}/.next ./.next
COPY --from=builder /app/${TARGET_APP_FOLDER}/node_modules ./node_modules
COPY --from=builder /app/${TARGET_APP_FOLDER}/package.json ./package.json
ENV PORT=8080
CMD yarn run start -p ${PORT}
我最后验证了 react
模块已经复制到 node_modules 文件夹。 React 模块本身只是使用别名来使用 preact/compat
.
当 运行在单个构建器中整合所有内容时,它工作得非常好:
FROM node:12 as builder
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
RUN npm -g config set user root && \
npm install -g lerna
WORKDIR /app
COPY . ./
ARG TARGET_APP_FOLDER=apps/app-1
# Install dependencies
COPY yarn.lock ./
WORKDIR /app/${TARGET_APP_FOLDER}
RUN lerna bootstrap \
-- --production
# Build
ARG PROJECT_ID
RUN test -n "$PROJECT_ID" || (echo "PROJECT_ID not set. Need to set PROJECT_ID to the GCP project ID you're deploying to" && false)
ENV PROJECT_ID=${PROJECT_ID}
RUN yarn run build:prod && \
/usr/local/bin/node-prune
# Start the server
ENV PORT=8080
CMD yarn run start -p ${PORT}
已修复!
这是我现在正在使用的 dockerfile,以防有人遇到同样的问题:
FROM node:12-alpine AS base
# DEFAULT ARGS
ENV BUILD_FOLDER=/build
ENV TARGET_APP_FOLDER=/apps/my_app
# CHECK PROJECT ID IS SET
ARG PROJECT_ID
RUN test -n "$PROJECT_ID" || (echo "PROJECT_ID not set. Need to set PROJECT_ID to the GCP project ID you're deploying to" && false)
ENV PROJECT_ID=${PROJECT_ID}
###### BUILDER BASE PREPARING FILES ######
FROM base AS builder_base
WORKDIR /base
RUN apk update && \
apk add curl bash && \
apk --no-cache add g++ make libpng-dev && \
curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
# COPY YARN LOCK
COPY yarn.lock ./
# Copy source
COPY . ./
# Remove unused source
RUN bin/delete-unused-apps.sh ${TARGET_APP_FOLDER}
###### BUILDER ######
FROM builder_base AS build
WORKDIR ${BUILD_FOLDER}
COPY --from=builder_base /base ./
ENV PATH=${BUILD_FOLDER}/node_modules/.bin:$PATH
RUN yarn install --production
WORKDIR ${BUILD_FOLDER}/${TARGET_APP_FOLDER}
RUN yarn build:prod && \
/usr/local/bin/node-prune
###### RUNNER ######
FROM base AS runner
ARG PROJECT_ID
ENV PROJECT_ID=${PROJECT_ID}
ENV PATH=/app/node_modules/.bin:$PATH
WORKDIR /app
COPY --from=build ${BUILD_FOLDER}/node_modules ./node_modules
COPY --from=build ${BUILD_FOLDER}${TARGET_APP_FOLDER}/.next ./.next
COPY --from=build ${BUILD_FOLDER}${TARGET_APP_FOLDER}/public ./public
COPY --from=build ${BUILD_FOLDER}${TARGET_APP_FOLDER}/package*.json ./
# Start the server
ENV PORT=8080
CMD yarn run start -p ${PORT}