获取镜像
- 如果不显示指定镜像TAG,则下载最新镜像
$ docker pull ubuntu:14.04
14.04: Pulling from library/ubuntu
2e6e20c8e2e6: Pull complete
0551a797c01d: Pull complete
512123a864da: Pull complete
Digest: sha256:64483f3496c1373bfd55348e88694d1c4d0c9b660dee6bfef5e12f43b9933b30
Status: Downloaded newer image for ubuntu:14.04
docker.io/library/ubuntu:14.04
查看镜像
代码语言:javascript复制$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zookeeper latest 36c607e7b14d 11 months ago 278MB
rabbitmq latest 7006a3ccf896 13 months ago 220MB
mysql 5.6 8d06d2d16232 14 months ago 303MB
ubuntu 14.04 13b66b487594 20 months ago 197MB
使用tag命令添加镜像标签
- 可以看到多了一个镜像,但是和以前的镜像指向的是同一个镜像文件,docker tag命令添加的标签实际上起到了类似链接的作用
$ docker tag rabbitmq:latest myrabbitmq
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zookeeper latest 36c607e7b14d 11 months ago 278MB
myrabbitmq latest 7006a3ccf896 13 months ago 220MB
rabbitmq latest 7006a3ccf896 13 months ago 220MB
mysql 5.6 8d06d2d16232 14 months ago 303MB
ubuntu 14.04 13b66b487594 20 months ago 197MB
使用inspect查看详细信息
代码语言:javascript复制$ docker inspect ubuntu:14.0 //获取镜像的基本信息
$ docker inspect -f {{.Id}} ubuntu:14.04 //获取镜像属性id的值
sha256:13b66b487594a1f2b75396013bc05d29d9f527852d96c5577cc4f187559875d0
使用history查看镜像历史
代码语言:javascript复制$ docker history ubuntu:14.04
IMAGE CREATED CREATED BY SIZE COMMENT
13b66b487594 20 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 20 months ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 20 months ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B
<missing> 20 months ago /bin/sh -c set -xe && echo '
#!/bin/sh' > /… 195kB
<missing> 20 months ago /bin/sh -c #(nop) ADD file:276b5d943a4d284f8… 196M
搜索镜像
- -f 过滤大于指定星级数
$ docker search -f STARS=3 nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 17746 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 182
bitnami/nginx Bitnami nginx Docker Image 143 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 69
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 22 [OK]
rancher/nginx-ingress-controller 11
ibmcom/nginx-ingress-controller Docker Image for IBM Cloud Private-CE (Commu… 4
kasmweb/nginx An Nginx image based off nginx:alpine and in… 3
bitnami/nginx-exporter 3
bitnami/nginx-ldap-auth-daemon
删除镜像
- -f 强制删除镜像,不推荐,最好删除容器之后在删除镜像
$ docker search -f STARS=3 nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 17746 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 182
bitnami/nginx Bitnami nginx Docker Image 143 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 69
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 22 [OK]
rancher/nginx-ingress-controller 11
ibmcom/nginx-ingress-controller Docker Image for IBM Cloud Private-CE (Commu… 4
kasmweb/nginx An Nginx image based off nginx:alpine and in… 3
bitnami/nginx-exporter 3
bitnami/nginx-ldap-auth-daemon 3
创建镜像
- 基于已有镜像的容器创建
- 基于本地模板导入
- 基于Dockerfile创建(后面再说)
基于已有镜像容器创建 docker commit
- -a 作者信息
- -c 提交的时候执行Dockerfiler指令,如CMD|ENTRYPOINT|ENV|EXPOSE
- -m 提交的消息
- -p 提交时暂停容器运行
首先,运行一个镜像,并在其中进行修改操作,记住容器运行的id:dd6b1a3afc6f 如
代码语言:javascript复制$ docker run -it ubuntu:14.04 /bin/bash
root@dd6b1a3afc6f:/# touch test
root@dd6b1a3afc6f:/# exit
exit
其次,使用docker commit 提交生成新的镜像
代码语言:javascript复制$ docker commit -m "a new file" -a "jiepi" dd6b1a3afc6f test:0.1
sha256:a478750ee9fb32f7a91f69516a1a8a73e95eec28d0085c4c59b25ef91ced6140
最后,查看镜像,发现已经生成新的镜像
代码语言:javascript复制$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test 0.1 a478750ee9fb 8 seconds ago 197MB
zookeeper latest 36c607e7b14d 11 months ago 278MB
rabbitmq latest 7006a3ccf896 13 months ago 220MB
mysql 5.6 8d06d2d16232 14 months ago 303MB
ubuntu 14.04 13b66b487594 20 months ago 197MB
基本本地模板导入 docker import
下载一个镜像如centos-6-x86.tar.gz,模板地址如下
代码语言:javascript复制http://openvz.org/Download/templates/precreated
代码语言:javascript复制-rw-r--r-- 1 root root 98954220 Mar 17 17:02 centos-6-x86.tar.gz
使用下面命令导入镜像到本地
代码语言:javascript复制cat centos-6-x86.tar.gz | docker import - centos-6-x86-64(名字自己定义)
存出和载入镜像
存出镜像,如果要导出镜像到本地文件,可以使用docker save,这样就可以把该镜像分享给他人
代码语言:javascript复制$ docker save -o ubuntu_14.04.tar ubuntu:14.04
$ ls ubuntu_14.04.tar
ubuntu_14.04.tar
载入镜像,使用 docker load 将导出的tar文件,再导入到本地镜像库,如
代码语言:javascript复制$ docker load --input ubuntu_14.04.tar
Loaded image: ubuntu:14.04
$ docker load < ubuntu_14.04.tar
Loaded image: ubuntu:14.04
上传镜像
代码语言:javascript复制$ docker push user/test:0.1