首页 > 解决方案 > 如何使用服务器端渲染为 ReactJS 应用程序设置多阶段构建

问题描述

如何将其配置为多阶段构建

纱线开始映射到

"start": "nodemon ./server-build/index.js",

Dockerfile

FROM node:14.17.3-alpine AS build

RUN apk add --no-cache build-base gcc autoconf automake libtool zlib-dev libpng-dev nasm

RUN npm i nodemon -g

WORKDIR /app/web-web

ENV PATH /app/web-web/node_modules/.bin:$PATH

COPY package.json /app/web-web/package.json

RUN yarn install --network-timeout 1000000000

COPY . /app/web-web

RUN yarn run build:staging

COPY . /app/web-web

EXPOSE 3006

CMD ["yarn", "start"]

标签: reactjsdockerfile

解决方案


警告未测试代码。

下面的 Dockerfile 未经测试,您必须将其视为起点。

缺少一些信息,这些apk包仅用于构建或运行?

我建议您阅读有关多阶段构建的信息

###############################################################
# This stage do:
# 1. Install required software
# 2. Copy package.json and install deps
###############################################################
FROM node:14.17.3-alpine AS build

# This packet are required ONLY for build ??? Otherwise add on final stage
RUN apk add --no-cache build-base gcc autoconf automake libtool zlib-dev libpng-dev nasm

WORKDIR /app/web-web

ENV PATH /app/web-web/node_modules/.bin:$PATH

COPY package.json /app/web-web/package.json

RUN yarn install --network-timeout 1000000000

###############################################################
# This stage do:
# 1. copy node_modules from build stage to this image
# 2. build app and run
###############################################################
FROM node:14.17.3-alpine AS final

WORKDIR /app/web-web
# Magic append here: I copy from prev stage (build) to this one
COPY --from=build /app/web-web .
COPY . /app/web-web

RUN npm i nodemon -g

RUN yarn run build:staging

EXPOSE 3006

CMD ["yarn", "start"]

推荐阅读