zoukankan      html  css  js  c++  java
  • Dockerfile多阶段构建之from==0

    1.使用Docker node打包dist:

    Dockerfile内容如下:

    FROM node:12.13.1
    LABEL maintainer="a@abc.com"
    COPY . /app/
    WORKDIR /app
    RUN npm install --registry=https://registry.npm.taobao.org --cache=$HOME/.npm/.cache/cnpm --disturl=https://npm.taobao.org/dist --userconfig=$HOME/.cnpmrc
    RUN npm run build

    然后进行build:

     docker build -t node:v1 .

    启动node容器:

    docker run -itd --name nodetest node:v1

    进入node容器:

    docker exec -it nodetest /bin/sh

    可以看到dist文件夹已生成

    2.修改Dockerfile,增加第二个FROM,内容如下:

    FROM node:12.13.1
    LABEL maintainer="a@abc.com"
    COPY . /app/
    WORKDIR /app
    RUN npm install --registry=https://registry.npm.taobao.org --cache=$HOME/.npm/.cache/cnpm --disturl=https://npm.taobao.org/dist --userconfig=$HOME/.cnpmrc
    RUN npm run build
    
    FROM nginx
    LABEL maintainer="a@abc.com"
    COPY --from=0 /app/dist /usr/share/nginx/html

    最后面的 --from=0 参数,从前边的阶段中拷贝文件到当前阶段中,Dockerfile中包含多个FROM语句时,0代表第一个阶段。除了使用数字,我们还可以给阶段命名,比如:

    FROM node:12.13.1 as node01
    LABEL maintainer="a@abc.com"
    COPY . /app/
    WORKDIR /app
    RUN npm install --registry=https://registry.npm.taobao.org --cache=$HOME/.npm/.cache/cnpm --disturl=https://npm.taobao.org/dist --userconfig=$HOME/.cnpmrc
    RUN npm run build
    
    FROM nginx
    LABEL maintainer="a@abc.com"
    COPY --from=node01 /app/dist /usr/share/nginx/html

    这样就不必再使用较大的node容器,直接使用较小的nginx容器即可

    参考:https://my.oschina.net/u/3960163/blog/1944752

  • 相关阅读:
    RTMP命令亲自测试记录
    如何在 i5 上实现 20 倍的 Python 运行速度?
    百倍加速!Python量化策略的算法性能提升指南
    谷歌推出 Python 性能加速方案
    用Cython加速Python到“起飞”
    Python GPU加速
    金融数学太复杂?看完这10部电影会不会轻松点!
    金融数学攻略+书单
    耳朵如何保养
    DataOps Reading Notes
  • 原文地址:https://www.cnblogs.com/dreamer-fish/p/15317043.html
Copyright © 2011-2022 走看看