如何在不使用纱线工作空间的情况下运行工作空间

时间:2019-08-07 16:46:12

标签: dockerfile yarnpkg yarn-workspaces

我正在构建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

0 个答案:

没有答案