1.什么是image
- image是文件和meta data的集合(root filesystem)
- 分层的,并且每一层都可以添加改变删除文件,成为一个新的image
- 不用的image可以共享相同的layer
-
image本身是read-only的
image
2.Image的获取(1)
-
Build from Dockerfile
Dockerfile
这是一个Dockerfile文件,这个Dockerfile有5行,其中第一行:选择一个baseImage
第二行:标明作者是谁
第三行:基于iamge之上去运行什么命令
第四行:暴露的端口
第五行:起点,程序的入口
docker build -t xiaopeng163/redis:last .
-t声明一个名字,最后面的.的意思是在当前目录下的Dockerfile文件去build

3.Image的获取(2)
-
Pull from Registry
有点模仿github,提出了一个registry,我们可以从registry中拉去image
Registry
registry是docker提供的一个免费image仓库,地址:https://hub.docker.com
4.DIY一个自己的image(hello world)
- 新建文件夹hello-world
- 用C语言写一个hello.c
#include<stdio.h>
int main(){
printf("hello docker");
}
我们知道如果要编译C语言的程序我们必须要有一个gcc,敲入命令:
sudo yum install gcc
sudo yum install glibc-static
- 编译C程序
gcc -static hello.c -o hello
然后我们可以看到在当前目录下面有了一个可执行文件hello,然后输入
./hello
就会打印出来hello docker
- 构建Dockerfile
FROM scratch
ADD hello /
CMD ["/hello"]
这是一个非常简单的Dockerfile
然后我们来构建这个镜像
docker build -t tinner/hello-tinner
至此,我们自定义的Dockerfile构建完毕。

我们可以看到,构建的这个镜像的大小是基于这个可执行文件大小的
然后我们可以看到这个image的分层:
docker history 9993e2f152b3

我们可以看到这个image就只有两层,第一层是添加文件,第二层是执行命令。其实第一层的FROM scratch是声明我们这个image不基于任何环境的。
然后运行
docekr run tinner/hello-tinner
可以看到输出了hello docker
网友评论