Docker 码头工人
Docker中镜像可理解为:应用程序的集装箱
Docker Images
Docker镜像含有启动容器所需要的文件系统及其内容,因此,其用于创建并启动docker容器
采用分层构建机制,最底层为bootfs,其之为rootfs
bootfs:用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载以节约内存资源
rootfs:位于bootfs之上,表现为docker容器的根文件系统;
传统模式中,系统启动之时,内核挂载rootfs时会首先将其挂载为“只读”模式,完整性自检完成后将其重新挂载为读写模式;
docker中,rootfs由内核挂载为“只读”模式,而后通过“联合挂载”技术额外挂载一个“可写”层;
注意:当删除容器时,这个容器自有的“可写”层会一起被删除
镜像层级
位于下层的镜像称为父镜像(parent image),最底层的称为基础镜像(base image)
最上层为“可读写层”,其下的均为“只读”层;
Aufs
Advanced multi-layered unification filessystem:高级多层统一文件系统
用于为Linux文件系统实现“联合挂载”;aufs是之前的UnionFS的重新实现,2006年由Junjiro Okajima开发;
Docker最初使用aufs作为容器文件系统层,它目前仍作为存储后端之一来支持;aufs竞争产品是overlayfs,后者自从3.18版本开始被合并到Linux内核;
Docker分层的镜像,除了aufs,docker还支持btrfs,devicemapper和vfs等
在ubuntu系统下:docker默认使用Ubuntu的aufs;而在CentOS7上,使用的是devicemapper;
Docker Registry
启动容器时,docker daemon会试图从本地获取相关的镜像;本地镜像不存在时,其将从Registry中下载该镜像并保存到本地;
注意:Registry如果没有指定,只给了仓库名和tag,则默认使用的是Docker hub;如果指向别的Registry需要修改配置给明服务器地址
除了Docker Hub还有其他镜像仓库,如:https://quay.io/
Registry用于保存docker镜像,包括镜像的层次结构和元数据
用户可自建Registry,也可使用官方的Docker Hub
Docker Registry分类
Sponsor Registry:第三方的registry,供客户和Docker社区使用
Mirror Registry:第三方的registry,只让客户使用
Vendor Registry:由发布Dcoker镜像的供应商提供的registry
Private Registry:通过设有防火墙和额外的安全层的私有实体提供的registry
Registry
由某特定的docker镜像的所有迭代版本组成的镜像仓库
一个Registry中可以存在多个Repository
Repository可分为“顶层仓库”和“用户仓库”
用户仓库名称格式为“用户名/仓库名”
每个仓库可以包含多个Tag(标签),每个标签对应一个镜像
Index
维护用户账户、镜像的检验以及公共命名空间的信息
相当于为Registry提供了一个完成用户认证等功能的检索接口
Docker Registry中的镜像通常由开发人员制作,而后推送至“公共”或“私有”Registry上保存,供其他人员使用,例如“部署”到生产环境
Docker Hub
功能:
镜像仓库
自动构建;web构子
组织
GitHub和Bitbucket
制作镜像
Dockerfile
基于容器制作
Docker Hub automated builds
基于容器制作容器
docker commit docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] --author, -a --pause, -p --message, -m --change, -c
简单示例1:让Busybox容器启动时就有index.html网页文件;
使用docker commit命令
[root@centos17 ~]#docker run --name b1 -it busybox WARNING: IPv4 forwarding is disabled. Networking will not work. / # / # mkdir -p /data/html / # echo "Busybox httpd server." > /data/html/index.html
注意:制作容器时让容器处理运行状态所以在另外一个终端执行:
[root@centos17 ~]#docker commit -p b1 sha256:db1dfc89367ed1899e85665d6c069e778d66a2cd590d969e761bed77b67cfa23 [root@centos17 ~]#docker image ls REPOSITORY TAG IMAGE ID CREATED SIZEdb1dfc89367e 7 seconds ago 1.16MB 制作好的容器 nginx alpine 36f3464a2197 10 days ago 18.6MB busybox latest 22c2dd5ee85d 2 weeks ago 1.16MB redis 4.0-alpine 80581db8c700 3 weeks ago 28.6MB centos latest 49f7960eb7e4 2 months ago 200MB
可使用docker tag命令来给镜像打标签;一个镜像可打多个标签
[[root@centos17 ~]#docker tag db1dfc89367e liuyutang/httpd:v0.1-1 [root@centos17 ~]#docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE liuyutang/httpd v0.1-1 db1dfc89367e 3 minutes ago 1.16MB nginx alpine 36f3464a2197 10 days ago 18.6MB busybox latest 22c2dd5ee85d 2 weeks ago 1.16MB redis 4.0-alpine 80581db8c700 3 weeks ago 28.6MB centos latest 49f7960eb7e4 2 months ago 200MB [root@centos17 ~]#docker tag liuyutang/httpd:v0.1-1 liuyutang/httpd:latest [root@centos17 ~]#docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE liuyutang/httpd latest db1dfc89367e 5 minutes ago 1.16MB liuyutang/httpd v0.1-1 db1dfc89367e 5 minutes ago 1.16MB nginx alpine 36f3464a2197 10 days ago 18.6MB busybox latest 22c2dd5ee85d 2 weeks ago 1.16MB redis 4.0-alpine 80581db8c700 3 weeks ago 28.6MB centos latest 49f7960eb7e4 2 months ago 200MB
删除标签
[root@centos17 ~]#docker image rm liuyutang/httpd:latest Untagged: liuyutang/httpd:latest
启动制作好的容器;可以看到我们刚才写的页面文件存在;容器制作成功
[root@centos17 ~]#docker run --name t1 -it liuyutang/httpd:v0.1-1 WARNING: IPv4 forwarding is disabled. Networking will not work. / # ls /data/ html / # ls /data/html/ index.html / # cat /data/html/index.html Busybox httpd server. / #
示例2:使用Busybox做为基础镜像,实现容器启动时默认运行httpd,并且在制作镜像时一起打标签;
制作镜像:
[root@centos17 ~]#docker commit -a "liuyutang" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 liuyutang/httpd:v0.2 sha256:f394f28026ff01f0bbdceb665b8e18a26c9a43a9c12cc57e4f895808f52e5f36 [root@centos17 ~]#docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE liuyutang/httpd v0.2 f394f28026ff 4 seconds ago 1.16MB liuyutang/httpd latest db1dfc89367e 21 minutes ago 1.16MB liuyutang/httpd v0.1-1 db1dfc89367e 21 minutes ago 1.16MB nginx alpine 36f3464a2197 10 days ago 18.6MB busybox latest 22c2dd5ee85d 2 weeks ago 1.16MB redis 4.0-alpine 80581db8c700 3 weeks ago 28.6MB centos latest 49f7960eb7e4 2 months ago 200MB
运行容器
[root@centos17 ~]#docker run --name t2 liuyutang/httpd:v0.2
查看容器
[root@centos17 ~]#docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 044d7516f77b liuyutang/httpd:v0.2 "/bin/httpd -f -h /d…" 37 seconds ago Up 36 seconds t2
我们可使用docker inspect命令来查看容器信息
[root@centos17 ~]#docker inspect t2
看到ip为172.17.0.2;可直接用curl命令来访问容器里的httpd服务;可以看到是我们自己做的页面
[root@centos17 ~]#curl 172.17.0.2 Busybox httpd server.
制作好的镜像可以共享到Docker Hub之上,前提是必须得有DockerHub账号,且镜像标签格式为
“$DOCKER_USER_ID/IMAGE" 使用docker login命令登录成功后,即可使用docker push命令进行推送
示例
[root@centos17 ~]#docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: liuyutang 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@centos17 ~]#docker image push liuyutang/httpd The push refers to repository [docker.io/liuyutang/httpd] e1b0032bd40b: Pushed 8e9a7d50b12c: Mounted from library/busybox latest: digest: sha256:2ba592789010f1349b8594c97015e10c49c7d75f1aeb36c7132b2525b1c64f82 size: 734 e1b0032bd40b: Layer already exists 8e9a7d50b12c: Layer already exists v0.1-1: digest: sha256:2ba592789010f1349b8594c97015e10c49c7d75f1aeb36c7132b2525b1c64f82 size: 734 c0a38dc06799: Pushed 8e9a7d50b12c: Layer already exists v0.2: digest: sha256:77fbb7f07a896404b232e879ef6b061d69a233d7388f37a39ba0ab4ea14ad722 size: 734
镜像的导入和导出
将镜像文件导出为tar文件:
docker save Save one or more images to a tar archive (streamed to STDOUT by default) docker save [OPTIONS] IMAGE [IMAGE...]
导出示例
[root@centos17 ~]#docker save -o myimages.gz liuyutang/httpd:v0.1-1 liuyutang/httpd:v0.2 [root@centos17 ~]#ls 192.168.30.27 elasticsearch-5.5.1.rpm kibana-5.5.1-x86_64.rpm myimages.gz anaconda-ks.cfg filebeat-5.5.1-x86_64.rpm logstash-5.5.1.rpm
从tar文件导入镜像 :
docker load Load an image from a tar archive or STDIN docker load [OPTIONS] --input, -i Read from tar archive file, instead of STDIN --quiet, -q false Suppress the load output
导入示例
[root@centos37 ~]#docker load -i myimages.gz 8e9a7d50b12c: Loading layer 1.378MB/1.378MB c0a38dc06799: Loading layer 5.12kB/5.12kB Loaded image: liuyutang/httpd:v0.2 e1b0032bd40b: Loading layer 5.12kB/5.12kB Loaded image: liuyutang/httpd:v0.1-1 [root@centos37 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE liuyutang/httpd v0.2 f394f28026ff 2 hours ago 1.16MB liuyutang/httpd v0.1-1 db1dfc89367e 2 hours ago 1.16MB
这种方式适用于测试环境