问题描述
根据此parse-server docker image,最新的官方post不再包含S3 File Adapter。所以我正在创建一个新图像。我的 Dockerfile -
FROM parseplatform/parse-server
USER root
workdir /parse-server
RUN npm install --save @parse/s3-files-adapter
USER node
构建后 -
docker build -t my_parse .
生成的 docker 镜像是原始镜像的两倍。
# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
my_parse latest 763968cef685 21 seconds ago 565MB
<none> <none> 1e92933e370e 23 seconds ago 565MB
<none> <none> 8778c6ebc0f7 About a minute ago 247MB
<none> <none> 46a2805beb06 About a minute ago 247MB
parseplatform/parse-server latest 0492a55f2c59 24 hours ago 247MB
如何减小 my_parse 图像的大小并去掉 none 图像?
解决方法
最有可能的是,您的 npm install
命令导致下载所有依赖项,这意味着您的映像也包含所有 node_modules
,因此尺寸较大。
此问题的一种可能解决方案是使用 Multi-stage build。这样,在您的第一阶段,您将运行 npm install
,收集您需要的所有文件,以便运行您的应用程序,而在第二阶段,您将只复制那些需要的文件,这将使图像整体更亮。
要删除所有未使用的层,请尝试运行 docker rmi $(docker images --filter "dangling=true")
。您可以阅读有关此 here 的更多信息。
以下 Dockerfile 构建了一个较小的 docker 镜像 -
FROM parseplatform/parse-server as initial
USER root
RUN npm install --save @parse/s3-files-adapter
# Release stage
FROM parseplatform/parse-server
USER root
RUN rm package*.json && rm -rf node_modules
COPY --from=initial /parse-server/package*.json ./
RUN npm ci --production --ignore-scripts
USER node
但是,它并不像直接从源代码构建那么小。
$ git clone https://github.com/parse-community/parse-server
$ cd parse-server
$ npm install --save @parse/s3-files-adapter
$ docker build --tag parse-server .