DIY 一个 Docker Base Image

1. 我们先使用C语言写一个hello-world程序

vim hello.c

# include 

int main()
{
    print("hello docker\n");
}

2. 将hello.c文件编译成二进制文件, 需要安装工具

yum install gcc
yum install glibc-static


开始编译

gcc -static hello.c -o hello

编译后的结果

此时hello文件是一个可执行文件,执行结果如下

3. 创建Dockerfile文件

FROM scratch
ADD hello /
CMD ["/hello"]

参数解析

1. FROM 告诉Docker从哪个镜像创建新的镜像

2. ADD 添加 hello 文件 到 / 根目录

3. CMD 执行命令, 中括号格式首个参数为路劲, ["/bin/bash", "-c", "echo 'hello cmd!'"]

4. 通过Dockerfile构建image

docker build -t gwl/hello-world .


1. build 根据dockerfile进行打包

2. -t gwl/hello-world 给镜像打标签取名

3. . 从当前文件夹获取dockerfile
 

DIY 一个 Docker Base Image_第1张图片

打包执行了三步,对应docker的三条指令。

查看镜像 docker image ls 

5.  查看镜像历史执行记录 doceer history 镜像id



FROM scratch
ADD hello /
CMD ["/hello"]

根据dockerfile内容能得知 from scratch 没有用到任何base镜像。

所以查看历史执行只有两条记录,第一层添加文件,第二层执行命令

6. 运行镜像 docker run gwl/hello-world

输出hello docker 

查看启动过的镜像, gwl/hello-world 15秒前启动过

你可能感兴趣的:(docker,docker,容器,运维)