2

我正在构建一个Dockerfile用于部署基于 monorepo 节点的服务器,该服务器基于packages/server并依赖于其他一些包。

生成的图像为 750Mb,原因是大的 node_modules 文件夹。由于这是一个生产编译,我想:

  • 完全删除 node_modules
  • 基本上跑node packages/server/build/server.js

通常我会用yarn workspace server start:prod这个。

我有两个问题:

  1. 启动节点服务器包的最佳方法是什么Dockerfile?为了让所有相关的路径关系正常工作。
  2. 当我删除 node_modules 时,如何确保运行构建不会爆炸(当我这样做时会发生这种情况:它尝试从 node_modules 加载文件)

这是当前的:

FROM node:10.15.3-alpine AS builder

ENV NODE_VERSION 10.15.3
ENV YARN_VERSION 1.15.2

# Install installation environment
RUN apk add --no-cache --virtual .build-deps-yarn curl \
    && curl -fSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz" \
    && tar -xzf yarn-v$YARN_VERSION.tar.gz -C /opt/ \
    && ln -snf /opt/yarn-v$YARN_VERSION/bin/yarn /usr/local/bin/yarn \
    && ln -snf /opt/yarn-v$YARN_VERSION/bin/yarnpkg /usr/local/bin/yarnpkg \
    && rm yarn-v$YARN_VERSION.tar.gz \
    && apk del .build-deps-yarn
RUN apk add --no-cache --virtual build-dependencies \
        rsync \
        python \
        make \
        g++

RUN apk update && apk add git

# Create production node_modules only directory
WORKDIR /node_cache
RUN mkdir -p /node_cache/packages \
    && mkdir -p /node_cache/packages/server \
    && mkdir -p /node_cache/packages/social \
    && mkdir -p /node_cache/packages/common-lib
COPY package.json /node_cache
COPY lerna.json /node_cache
COPY yarn.lock /node_cache
COPY packages/server/package.json /node_cache/packages/server
COPY packages/social/package.json /node_cache/packages/social
COPY packages/common-lib/package.json /node_cache/packages/common-lib 

# Install optimized production version
ENV NODE_ENV production
RUN yarn install --ignore-scripts --frozen-lockfile --production && npm rebuild

# Install development version for compilation
WORKDIR /app
COPY . /app

ENV NODE_ENV development
RUN yarn install --ignore-scripts --frozen-lockfile && npm rebuild

# Build the environment
RUN NODE_OPTIONS="--max_old_space_size=$MEMORY_LIMIT" yarn workspace server razzle build

# Copy production node_modules to production environment
RUN rm -rf ./node_modules
RUN rm -rf ./packages/server/node_modules
RUN rm -rf ./packages/social/node_modules
RUN rm -rf ./packages/common-lib/node_modules
RUN rsync -a /node_cache/ /app/

RUN apk del build-dependencies && yarn cache clean

# Deployment image
FROM node:10.15.3-alpine

ENV HOST 0.0.0.0
ENV PORT 3005
ENV NODE_ENV production 

RUN apk add --no-cache --virtual .build-deps-yarn curl \
    && curl -fSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz" \
    && tar -xzf yarn-v$YARN_VERSION.tar.gz -C /opt/ \
    && ln -snf /opt/yarn-v$YARN_VERSION/bin/yarn /usr/local/bin/yarn \
    && ln -snf /opt/yarn-v$YARN_VERSION/bin/yarnpkg /usr/local/bin/yarnpkg \
    && rm yarn-v$YARN_VERSION.tar.gz \
    && apk del .build-deps-yarn

WORKDIR /app
COPY --from=builder /app .

# Start the server
USER node
ENTRYPOINT ["yarn"]
CMD ["workspace","server","start:prod"]

EXPOSE 3005

4

0 回答 0