Docker Notes
T

聊聊 scratch 这个镜像

Scratch - Official Image | Docker Hub

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 镜像

  1. 编写 hello.c 程序用于编译出 helloworld 可执行文件。

    #include <stdio.h>
    int main(){
        printf("hello world, hello jaime\n");
    }
  2. 编译 hello.c 程序(gcc 一定要加 --static 参数)

    $ gcc --static -o hello hello.c
  3. 测试 hello 可执行程序

    $ ./hello
  4. 编写 Dockerfile 文件

    FROM scratch
    COPY hello /
    CMD ["/hello"]
  5. 构建镜像

    $ docker image built -t jaimezeng/hello-jaime:0.0.3 .
  6. 查看镜像大小

    $ docker images
    $ ls -lah ./hello
  7. 启动容器

    $ docker container run -it -name hello-world jaimezeng/hello-jaime:0.0.3
  8. 显示镜像 layer。

    $ docker image history jaimezeng/hello-jaime:0.0.3

scratch image

Show Graph Visualisation