yuanfnadi
V2EX  ›  问与答

Docker 的多步构建挺好用的

  •  
  •   yuanfnadi · Aug 31, 2017 · 2525 views
    This topic created in 3192 days ago, the information mentioned may be changed or developed.

    之前我都是用 daoclou 进行多步构建,先一个镜像 build 好,然后提取出文件,再复制到另外一个镜像里

      compile:
        build_dir: /
        cache: true
        dockerfile_path: /Dockerfile/Dockerfile.build
      extract:
      - /blog
      package:
        build_dir: /
        cache: true
        dockerfile_path: /Dockerfile/Dockerfile.deploy
    

    然后搜索了一下 发现 docker 原生支持多步构建了。 然后在 daocloud 里试了一下啊也支持。

    FROM gradle:4.0.1-alpine as build
    MAINTAINER FandiYuan  <[email protected]>
    
    ADD ./ /tmp/
    
    RUN cd /tmp && \
        gradle build
    
    FROM java:8-jre-alpine
    
    COPY --from=build /tmp/build/libs/diamond-yuan-fast-airport-0.01.jar /
    
    ENTRYPOINT ["java", "-server", "-jar", "/diamond-yuan-fast-airport-0.01.jar"]
    

    在采用多步构建之前,镜像大小在 150m 左右,采用了多步构建以后,只有 70m、基本上就是 jre 的大小了。

    3 replies    2017-09-01 07:45:31 +08:00
    yuanfnadi
        1
    yuanfnadi  
    OP
       Aug 31, 2017
    hexo 的博客这样就能构建出一个很小的镜像了。

    ```
    FROM daocloud.io/diamondyuan/blog-base:latest as buiild

    MAINTAINER FandiYuan <[email protected]>
    ADD / /blog
    RUN mv /temp/node_modules /blog/node_modules && \
    hexo g

    FROM nginx:stable-alpine

    copy COPY --from=build /blog/public/ /usr/share/nginx/html/

    ```


    https://blog.diamondyuan.com/2017/08/10/Continuous_Deployment_for_Hexo/
    terrawu
        2
    terrawu  
       Aug 31, 2017
    alpine 又要受益于这个特性了。
    whileFalse
        3
    whileFalse  
       Sep 1, 2017
    daocloud 的使用原生多部构建时缓存友好吗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1557 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:37 · PVG 00:37 · LAX 09:37 · JFK 12:37
    ♥ Do have faith in what you're doing.