我正在构建Dockerfile
来部署基于Monorepo节点的服务器,该服务器基于packages/server
,并依赖于其他软件包。
生成的图像为750Mb,原因是较大的node_modules文件夹。由于这是生产编译,因此我想:
node packages/server/build/server.js
通常我会为此使用yarn workspace server start:prod
。
我有两个问题:
Dockerfile
中启动节点服务器软件包的最佳方法是什么?为了使所有相关的路径关系正常工作。这是当前版本:
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