汇总
起因:根据官方文件内容可以看出,他在打包镜像时会拉取环境依赖然后重新编译代码。
问题:这一步耗时极长,且容易报错,在执行过程中一旦中途报错就会全功尽其,特别伤,而且即使每次打包都没问题,耗时也太长了,基本一个小时起步,不实用。
解决:根据上述情况,在尝试打包的过程中也在疑惑为什么他不直接将已经打包好的代码黏贴到镜像中而是要在镜像中重新打包呢?于是做对应尝试。
结果:成功,按个人整理的方式可以打包成功镜像,且运行无误。
kubesphere官方git的打包的DockerFile文件
FROM node:12-alpine3.14 as builder
ARG YARN_VERSION=1.22.4
WORKDIR /kubesphere
ADD . /kubesphere/
RUN apk add --no-cache --virtual .build-deps ca-certificates python2 python3 py3-pip make openssl g++ bash
RUN npm install yarn@${YARN_VERSION}
RUN yarn && yarn build
# Copy compiled files
RUN mkdir -p /out/server
RUN mv /kubesphere/dist/ /out/
RUN mv /kubesphere/server/locales \
/kubesphere/server/public \
/kubesphere/server/views \
/kubesphere/server/sample \
/kubesphere/server/config.yaml /out/server/
#RUN ["/bin/bash", "-c", "mv /kubesphere/server/{locales,public,sample,views,config.yaml} /out/server/"]
RUN mv /kubesphere/package.json /out/
##############
# Final Image
##############
FROM node:12-alpine3.14 as base_os_context
RUN adduser -D -g kubesphere -u 1002 kubesphere && \
mkdir -p /opt/kubesphere/console && \
chown -R kubesphere:kubesphere /opt/kubesphere/console
WORKDIR /opt/kubesphere/console
COPY --from=builder /out/ /opt/kubesphere/console/
RUN mv dist/server.js server/server.js
USER kubesphere
EXPOSE 8080
CMD ["npm", "run", "serve"]
修改后的Dockerfile文件
FROM node:12-alpine3.14 as builder
ARG YARN_VERSION=1.22.4
WORKDIR /kubesphere
ADD . /kubesphere/
COPY dist/ /kubesphere/dist
COPY server/ /kubesphere/server
COPY package.json /kubesphere
# Copy compiled files
RUN mkdir -p /out/server
RUN mv /kubesphere/dist/ /out/
RUN mv /kubesphere/server/locales \
/kubesphere/server/public \
/kubesphere/server/views \
/kubesphere/server/sample \
/kubesphere/server/config.yaml /out/server/
RUN mv /kubesphere/package.json /out/
##############
# Final Image
##############
FROM node:12-alpine3.14 as base_os_context
RUN adduser -D -g kubesphere -u 1002 kubesphere && \
mkdir -p /opt/kubesphere/console && \
chown -R kubesphere:kubesphere /opt/kubesphere/console
WORKDIR /opt/kubesphere/console
COPY --from=builder /out/ /opt/kubesphere/console/
RUN mv dist/server.js server/server.js
USER kubesphere
EXPOSE 8080
CMD ["npm", "run", "serve"]