Я создаю многоступенчатый образ докера для проекта, который использует nextjs и preact в lerna monorepo.

Многоступенчатая сборка завершается успешно, однако, когда я хочу запустить изображение, nextjs выдает ошибку, которая не может быть найдена «реагировать».

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. Сам модуль реакции просто использует псевдоним для использования 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}
0
bhr 26 Дек 2020 в 20:25

1 ответ

Лучший ответ

Починил это!

Это файл докеров, который я использую прямо сейчас, на случай, если кто-то наткнется на ту же проблему:

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}

0
bhr 8 Мар 2021 в 13:45