0、参考文章:

两个奇技淫巧,将 Docker 镜像体积减小 99%

https://mp.weixin.qq.com/s/kyK6652kchtudZHhSsYx_Q

Docker Dockerfile

https://www.runoob.com/docker/docker-dockerfile.html

=========================================================

1、例子c文件

/* hello.c */

int main (){

puts("Hello, world!");

return 0;

}

========================================================

2、 Dockerfile

FROM gcc AS mybuildstage

COPY hello.c .

RUN gcc -o hello hello.c

FROM ubuntu

COPY --from=mybuildstage hello .

CMD ["./hello"]

========================================================

3、开始构建

docker build -t ubuntu:hello .

阶段一:

阶段二:

========================================================

运行:

========================================================

4、查看镜像

可以看到,gcc有1.19的大小

而阶段二的hello,因为基础镜像是从ubuntu开始的,所以只有73MB