聊聊 scratch 这个镜像
scratch image
scratch 镜像是一个空的镜像,可以用于构建 busybox 等超小镜像,可以说是真正的从零开始构建属于自己的镜像。
scratch 专门用于构建最小镜像,直接 pull 会报以下错误,scratch 是一个保留名称。
Error response from daemon: 'scratch' is a reserved name创建本地 scratch 镜像:
$ tar cv --files-from /dev/null | docker import - scratch sha256:5b5dfc4c4c4836c7a763ff40e6694d6260a2be5a0c22015324b4ab6ccc4c19f1 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE scratch latest 5b5dfc4c4c48 7 seconds ago 0B ubuntu 22.04 c68f8d0eb3f0 5 hours ago 78.1MB nginx latest 605c77e624dd 8 days ago 141MB
练习 - 使用 scratch 构建 helloworld 镜像
编写
hello.c程序用于编译出helloworld可执行文件。#include <stdio.h> int main(){ printf("hello world, hello jaime\n"); }编译
hello.c程序(gcc 一定要加--static参数)$ gcc --static -o hello hello.c测试
hello可执行程序$ ./hello编写
Dockerfile文件FROM scratch COPY hello / CMD ["/hello"]构建镜像
$ docker image built -t jaimezeng/hello-jaime:0.0.3 .查看镜像大小
$ docker images $ ls -lah ./hello启动容器
$ docker container run -it -name hello-world jaimezeng/hello-jaime:0.0.3显示镜像 layer。
$ docker image history jaimezeng/hello-jaime:0.0.3

Backlinks
Docker Notes
- 聊聊 scratch 这个镜像 [[scratch-image]]