1、什么是Docker镜像
简单的说,Docker镜像是一个不包含Linux内核的精简的Linux操作系统。
2、镜像从哪里来
Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜像。
https://hub.docker.com/explore
默认是国外的源,下载会慢,可以国内的源提供下载速度。
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://04be47cf.m.daocloud.io
3、镜像工作原理
当我们启动一个新的容器时,Docker会只加载只读镜像,并在其上添加一个读写层,并将镜像中的目录复制一份到/var/lib/docker/aufs/mnt/
容器ID为目录下,我们可以使用chroot进入此目录。
如果运行中的容器已经存在此文件,那么会将该文件从下面的只读层复制到读写曾,只读层的这个文件就会被覆盖,但是还存在,这就实现了文件隔离,当删除容器后,读写层会将数据删除,只读镜像不变。
4、镜像文件存储结构
docker相关文件存放:/var/lib/docker
目录下
/var/lib/docker/aufs/diff
每层与其父层之间的问价差异
/var/lib/docker/aufs/layers/
每层一个文件,记录其父层一直到根层之间的ID
/var/lib/docker/aufs/mnt
联合挂载点,从只读层复制到最上层可读层的文件系统数据
建立镜像时,每次写操作,都会被视作为一种增量操作,即在原有的数据层上添加一个新层,所以一个镜像会有若干个层组成,每次commit提交就会产生一个ID,就相当于上一层又加了一层,可以通过这个ID对镜像回滚。
5、镜像管理命令
搜索镜像:
[root@localhost ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 5533 [OK]
ansible/centos7-ansible Ansible on Centos7 122 [OK]
#可以在docker hub网站上搜索镜像, 名字、描述、下载用户、官方、自动化
#第一个我已经下载过了,还有好多关于centos的镜像,我没有复制下来
下载镜像:
[root@localhost ~]# docker pull centos:6
6: Pulling from library/centos
Digest: sha256:9aae95c8043f4e401178d68006756dc68982ae6d0693b71a714754227ce0abc6
Status: Image is up to date for centos:6
docker.io/library/centos:6
#下载镜像centos,版本6
#已经下载过的提示
查看镜像仓库:
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
grafana/grafana latest 6a389498ea02 23 hours ago 256MB
<none> <none> db52567b20c9 24 hours ago 400MB
ssh v1 6d04af665075 24 hours ago 312MB
<none> <none> 6af8a7d3e8af 27 hours ago 460MB
centos latest 67fa590cfc1c 9 days ago 202MB
mysql latest 62a9f311b99c 2 weeks ago 445MB
richarvey/nginx-php-fpm latest 0ca466b9d3cd 3 weeks ago 330MB
rethinkdb latest d8636baa6e8e 7 weeks ago 185MB
192.168.140.100:5000/centos v1 d0957ffdf8a2 5 months ago 194MB
centos 6 d0957ffdf8a2 5 months ago 194MB
registry latest f32a97de94e1 5 months ago 25.8MB
hello-world latest fce289e99eb9 8 months ago 1.84kB
google/cadvisor latest eb1210707573 9 months ago 69.6MB
swarm latest ff454b4a0e84 15 months ago 12.7MB
ansible/centos7-ansible latest 688353a31fde 2 years ago 447MB
tutum/influxdb latest c061e5808198 2 years ago 290MB
shipyard/shipyard latest 36fb3dc0907d 2 years ago 58.8MB
shipyard/docker-proxy latest cfee14e5d6f2 3 years ago 9.47MB
microbox/etcd latest 6aef84b9ec5a 4 years ago 17.9MB
abh1nav/dockerui latest 6e4d05915b2a 4 years ago 470MB
#自己仓库中下载过的镜像
#存储库、版本、镜像ID、创建时间、大小
=========================================================================================================================
[root@localhost ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
#-a所有,-q打印镜像id
删除镜像:
[root@localhost ~]# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
#docker rmi 镜像ID
=======================================================================================
[root@localhost ~]# docker rm -f --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove the volumes associated with the container
#docker rm -f 镜像ID
=======================================================================================
[root@localhost ~]# docker images rm -f $(docker images -q -a)
#删除所有镜像
存入镜像:
[root@localhost ~]# docker save --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
[root@localhost ~]# docker save centos:latest -o /home/hahaha
#将centos最新镜像存入/home/目录下,名为hahaha
将镜像导入到本地镜像库:
[root@localhost ~]# docker load --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
=======================================================================================
[root@localhost ~]# docker load --input /home/hahaha
Loaded image: centos:latest
[root@localhost ~]# docker load < /home/hahaha
Loaded image: centos:latest
#将hahaha导入到本地镜像库中,hahaha是centos最新版本
上传镜像:
[root@localhost ~]# docker push --help
Usage: docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
--disable-content-trust Skip image signing (default true)
=======================================================================================
#用户在docker hub网站注册后可以上传自制镜像
#docker push name[:tag]
从容器的更改中创建一个新的镜像:
[root@localhost ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
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)