镜像的简要概念:
1,一个操作系统可以做成一个镜像。
2,一个进程可以做成一个镜像
docker镜像简要说明:
1,docker镜像含有启动容器所需要的文件系统及其内容,因此,其用与创建并启动docker容器。
2,镜像采用分层机制,最底层为bootfs,其之为rootfs
docker镜像的简要构建:
1,位于下层的镜像称之为父镜像,最底层的称之为基础镜像
2,最上层为“可读写”层,其下的均为“只读层”
3,镜像都是一层一层构建起来,每一层都是独立的,若删除一个容器,writable也会被删除。
docker registry分类
1,启动容器时,docker daemon会试图从本地获取相关镜像,若本地镜像不存在时,其将从registry中下载该镜像并保存到本地
2,registry用于保存docker镜像,包括镜像的层次结构和元数据,用户可以自己建registry,也可以使用官方的docker hub
3,分类
sponsor registry:第三方的registry,供客户和docker社区使用
mirror registry:第三方的registry,只让客户使用
vendor registry:由发布docker镜像供应商提供
private registry:通过设有防火墙和额外的安全层的私有实体提供的registry
repository(一大堆仓库)
1,由某特定的docker镜像的所有迭代版本呢组成的镜像仓库
2,一个registry中可以存在多个repository
repository可分为“顶级仓库”和“用户仓库”
用户仓库名称格式为“用户名/仓库名”
3,每个仓库可以包含多个tag标签,每个标签对应一的镜像
4,修改dockerfile文件,推入到github仓库中,使得dockerhub拉去github仓库文件自动构成镜像,保存到自己的仓库名中
2,如何制作镜像
镜像的生成途径
1,基于dockerfile----->buid
2, 基于容器制作 ----->docker commit ;是将镜像的最上层打包做成镜像
3,基于docker hub automated builds
做法一:以下步骤都是根据容器制作镜像
例如:将一个busbox镜像中添加/data/html/index.html文件,将这个结果保存为一个镜像,下回使用这个镜像将有这个文件
[root@server ~]# docker pull busybox #先从docker镜像仓库中拉去busybox镜像
Using default tag: latest
latest: Pulling from library/busybox
7c9d20b9b6cd: Pull complete
Digest: sha256:fe301db49df08c384001ed752dff6d52b4305a73a7f608f21528048e8a08b51e
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest
[root@server ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 19485c79a9bb 4 days ago 1.22MB
httpd latest 7d85cc3b2d80 2 weeks ago 154MB
httpd v0.1 7d85cc3b2d80 2 weeks ago 154MB
[root@server ~]# docker run --name b1 -it busybox #使用busybox镜像启动一个容器,容器名为b1
/ #
/ # ls
bin dev etc home proc root sys tmp usr var
/ # vi /data/html/index.html #进入这个进程中,创建/data/html/ 编辑一个网页文件,将网页文件结果做成镜像。
/ #
/ # ls
bin data dev etc home proc root sys tmp usr var
/ # cd data/
/data # ls
/data # mkdir html
/data # ls
html
/data # cd html/
/data/html # ls
/data/html # vim index.html
sh: vim: not found
/data/html # vi index.html
/data/html #
/data/html #
/data/html # cat index.html #在这里注意不要关闭这个容器进程,让容器处于运行状态,然后复制一个终端制作镜像
[root@server ~]# docker commit -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG] #在这里表示基于那个容器做镜像,做成的惊喜镜像属于哪个仓库有什么标签
Create a new image from a container's change
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
[root@server ~]# docker commit -p b1 #-p:为了防止容器进程还在运行,做镜像时暂停容器, b1:指明使用那个容器。
sha256:dcab0dd293fc7a1582a3439b26adce9e410aceaa3128f171e0ac96073e728746
[root@server ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> dcab0dd293fc 9 seconds ago 1.22MB
busybox latest 19485c79a9bb 4 days ago 1.22MB
httpd latest 7d85cc3b2d80 2 weeks ago 154MB
httpd v0.1 7d85cc3b2d80 2 weeks ago 154MB
[root@server ~]# docker tag dcab0dd293fc yanss/httpd:v0.1-1 #这里表示给这个镜像打上标签,指明是dokcerhub仓库下yanss账户下httpd仓库下的镜像 标签为v0.1-1
[root@server ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
yanss/httpd v0.1-1 dcab0dd293fc About a minute ago 1.22MB
busybox latest 19485c79a9bb 4 days ago 1.22MB
httpd latest 7d85cc3b2d80 2 weeks ago 154MB
httpd v0.1 7d85cc3b2d80 2 weeks ago 154MB
[root@server ~]# docker tag yanss/httpd:v0.1-1 yanss/httpd:latest #可以在给镜像打个标签,一个镜像可以有多个标签
[root@server ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
yanss/httpd latest dcab0dd293fc 2 minutes ago 1.22MB
yanss/httpd v0.1-1 dcab0dd293fc 2 minutes ago 1.22MB
busybox latest 19485c79a9bb 4 days ago 1.22MB
httpd latest 7d85cc3b2d80 2 weeks ago 154MB
httpd v0.1 7d85cc3b2d80 2 weeks ago 154MB
#验证做的镜像是否能够成功,是否有网站页面文件
[root@server ~]# docker run --name b2 -it yanss/httpd:v0.1-1 #运行一个容器,使用刚做的镜像看是否制作成功。
/ #
/ # ls
bin data dev etc home proc root sys tmp usr var
/ # cd data/
/data # ls
html
/data # cd html/
/data/html # cat index.html
<h1>busybox httpd server.</h1> #根上述所显示文件一样,说明这个镜像制作成功
/data/html #
#我们都知道容器启动,默认使用了镜像的默认启用命令,我们上述所制作的镜像,并没有改变它的默认启用命令。
[root@server ~]# docker inspect busybox:latest
"Cmd": [
"sh" #我们可以看出默认启用命令是一样的,都是shell
],
[root@server ~]# docker inspect yanss/httpd:v0.1-1
],
"Cmd": [
"sh"
],
"Image": "bu
#第二种做法:我们就是将默认启用的命令不再是shell,而是httpd一个进程。如何制作请看下述。
[root@server ~]# docker commit -a "yanss <yan@yanss.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 yanss/httpd:v0.2
-a:指作者 -c:指明默认运行的命令,通过which httpd查看 -f:指明在前台 -h:指明主资源目录
-p:暂停容器 +制作镜像的名称
sha256:7fa95ebf41389473a53b7de93a135d94454c6dfe54fce2cf28ff14dcd191af25
[root@server ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
yanss/httpd v0.2 7fa95ebf4138 10 seconds ago 1.22MB
yanss/httpd latest dcab0dd293fc 16 hours ago 1.22MB
yanss/httpd v0.1-1 dcab0dd293fc 16 hours ago 1.22MB
busybox latest 19485c79a9bb 5 days ago 1.22MB
httpd latest 7d85cc3b2d80 2 weeks ago 154MB
httpd v0.1 7d85cc3b2d80 2 weeks ago 154MB
# 那么使用这个镜像,运行一个容器,没有交互式。没有任何信息。
[root@server ~]# docker run --name t2 yanss/httpd:v0.2
[root@server ~]# docker container ls :查看容器时,有一个容器正在运行这个镜像。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
112504db311f yanss/httpd:v0.2 "/bin/httpd -f -h /d…" About a minute ago Up 56 seconds t2
2189ae14af28 httpd:v0.1 "httpd-foreground" 24 hours ago Up 24 hours 80/tcp web3
[root@server ~]# docker inspect t2 #查看这个容器的相关信息
bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "55f39ca698d2aedf9324ccfe1f71ada199a4852d3f7d457802e4bcafe9445607",
"EndpointID": "245d54e6722ea0bd8728d23a785322f7e8a84f671e2d9f12cb84cf6b32d0a95b",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
[root@server ~]# curl 172.17.0.3
<h1>busybox httpd server.</h1>
上述如何制作镜像已经完成,现在将讲述如何将自己的镜像放到docker hub中。
第一步:创建docker hub账号
https://hub.docker.com //登陆到docker hub官方网站,创建一个自己的账号。
创建好远程镜像仓库后,我们就可以推镜像了
第二步:将自己创建的镜像传送到远程仓库(docker hub)
注意:传送镜像之前,首先要登陆到远程仓库,否则没有权限
[root@server ~]# docker container ls //查看所有容器运行情况
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@server ~]# docker image ls //查看本地所有镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
yyr01/httpd v0.1-1 7988af251f89 22 hours ago 1.22MB
mohan/httpd v0.2 cd92c93a1134 22 hours ago 1.22MB
mohan/httpd v0.1-1 8f77d71e5b28 23 hours ago 1.22MB
yanss/httpd v0.2 7fa95ebf4138 6 weeks ago 1.22MB
yanss/httpd latest dcab0dd293fc 6 weeks ago 1.22MB
yanss/httpd v0.1-1 dcab0dd293fc 6 weeks ago 1.22MB
busybox latest 19485c79a9bb 7 weeks ago 1.22MB
httpd latest 7d85cc3b2d80 2 months ago 154MB
httpd v0.1 7d85cc3b2d80 2 months ago 154MB
[root@server ~]# docker log
login logout logs
[root@server ~]# docker login -u yyr01 //登陆到docker hub中,使用自己账户登陆
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@server ~]# docker push yyr01/httpd:v0.1-1 //将自己创建的镜像推送到docker hub 自己账户中
The push refers to repository [docker.io/yyr01/httpd]
851d32f9f13c: Pushed
6c0ea40aef9d: Mounted from library/busybox
v0.1-1: digest: sha256:22b8482e0a782c2bd142dc723d4dd4b9dca40b0c05d1d8ed89d73a373d6f4ca4 size: 734
[root@server ~]#
//注意:推送的镜像必须与创建账户,镜像仓库保持一致,否则推不上去
第三步:查看远端镜像仓库,别人就可以拿来使用了