Docker实战之镜像操作:
获取镜像:
docker pull NAME[:TAG]
root@doc-vm:~# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:150f6d05b8898b79f55114991c01b89688a02dab6115e402b7855792a440caff
Status: Downloaded newer image for hello-world:latest
latest标签意味着这个镜像的内容会跟踪最新的非稳定版本而发步,内容是不稳定的。不要在生产环境中使用latest标签的镜像。
使用其他镜像源进行下载:
root@doc-vm:~# docker pull hub.c.163.com/public/ubuntu:14.04
14.04: Pulling from public/ubuntu
f4ab0b34ba6a: Pull complete
2f0787dc0bfe: Pull complete
54c427d77362: Pull complete
51e87d5424ff: Pull complete
5dfa314e7290: Pull complete
a3ed95caeb02: Pull complete
Digest: sha256:ffc2fc66f8e0bfa4b417b817054d3ebec130c8db44342b8fa394e25779633257
Status: Downloaded newer image for hub.c.163.com/public/ubuntu:14.04
pull命令支持的选项:
-a, --all-tags=true|false:是否获取仓库中的所有镜像,默认是否。
查看镜像信息:
docker images
root@doc-vm:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 9e7424e5dbae 5 days ago 108 MB
hello-world latest f2a91732366c 7 days ago 1.85 kB
ubuntu latest 20c44cd7596f 10 days ago 123 MB
tomcat latest 11df4b40749f 13 days ago 558 MB
mysql latest 5709795eeffa 3 weeks ago 408 MB
busybox latest 6ad733544a63 3 weeks ago 1.13 MB
centos latest d123f4e55e12 3 weeks ago 197 MB
hub.c.163.com/public/ubuntu 14.04 2fe5c4bba1f9 20 months ago 237 MB
images的子选项:
-a, --all=true|false:列出所有镜像文件,默认是否
--digest=true|false:列出镜像的数字摘要值,默认是否
-f,--filter=[]:过滤列出的镜像
--format="TEMPLATE":控制输出格式
--no-turnc=true|false:对输出结果中太长的部分是否进行截断,默认为是
-q,--quiet=true|false:仅仅输出ID信息,默认是否
使用tag命令添加镜像标签:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
root@doc-vm:~# docker tag ubuntu:latest mysystem:latest
root@doc-vm:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 9e7424e5dbae 5 days ago 108 MB
hello-world latest f2a91732366c 7 days ago 1.85 kB
mysystem latest 20c44cd7596f 10 days ago 123 MB
ubuntu latest 20c44cd7596f 10 days ago 123 MB
tomcat latest 11df4b40749f 13 days ago 558 MB
mysql latest 5709795eeffa 3 weeks ago 408 MB
busybox latest 6ad733544a63 3 weeks ago 1.13 MB
centos latest d123f4e55e12 3 weeks ago 197 MB
hub.c.163.com/public/ubuntu 14.04 2fe5c4bba1f9 20 months ago 237 MB
实际上指向的是同一个文件,只是别名而已。
使用inspect命令查看详细信息:
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
root@doc-vm:~# docker inspect ubuntu
[
{
"Id": "sha256:20c44cd7596ff4807aef84273c99588d22749e2a7e15a7545ac96347baa65eda",
"RepoTags": [
"mysystem:latest",
"ubuntu:latest"
],
"RepoDigests": [
"ubuntu@sha256:cb67b69a3b83dc209418cf15dace1a2fe336e37ef6a972a00fb9e02a608a8184"
],
"Parent": "",
"Comment": "",
"Created": "2017-11-17T21:59:25.014645802Z",
"Container": "e5f1a9df75b86a5d803eaf6f3fed6a0f8ef5fbf15a6c5039df087e4348ed8171",
"ContainerConfig": {
"Hostname": "e5f1a9df75b8",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/bash\"]"
],
"ArgsEscaped": true,
"Image": "sha256:b5771e7d8dcc594b886dbdd6a9c3de60d45252ca657dfdff6e1d996728dfa2cd",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"DockerVersion": "17.06.2-ce",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"ArgsEscaped": true,
"Image": "sha256:b5771e7d8dcc594b886dbdd6a9c3de60d45252ca657dfdff6e1d996728dfa2cd",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": null
},
"Architecture": "amd64",
"Os": "linux",
"Size": 122792927,
"VirtualSize": 122792927,
"GraphDriver": {
"Name": "aufs",
"Data": null
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:788ce2310e2fdbbf81fe21cbcc8a44da4cf648b0339b09c221abacb4cd5fd136",
"sha256:aa4e47c4511638484cd5d95eadd7a8e4da307375ba31ff50d47aa9065dce01e0",
"sha256:b3968bc26fbd527f214f895aeef940a6930c62d853fe8b12bd479f0b53518150",
"sha256:c9748fbf541d3e043521e165b015d45825de33c00a8acb037443cfbd0cb5e677",
"sha256:2f5b0990636a87f1557d64ba39808dcd64031328b2a159c5805115b8e725bbbc"
]
}
}
]
返回的是Josn格式的信息,指定其中一项可以是:
root@doc-vm:~# docker inspect -f {{".Os"}} ubuntu
linux
搜索镜像:
docker search [OPTIONS] TERM
--automated-true|false:仅仅显示自动创建的镜像,默认是否
--no-trunc=true|false:输出信息不截断显示,默认是否
-s,--stare=X:仅仅显示评价为指定星级以上的镜像,默认是0,也就是输出所有的镜像。
root@doc-vm:~# docker search ubuntu
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating s... 6868 [OK]
dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 146 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of of... 117 [OK]
ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 89 [OK]
ubuntu-upstart Upstart is an event-based replacement for ... 80 [OK]
neurodebian NeuroDebian provides neuroscience research... 40 [OK]
ubuntu-debootstrap debootstrap --variant=minbase --components... 32 [OK]
nuagebec/ubuntu Simple always updated Ubuntu docker images... 22 [OK]
tutum/ubuntu Simple Ubuntu docker images with SSH access 19
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 18 [OK]
... ...
使用docker history查看镜像历史:
镜像是由多个层组成的,查看每个层的具体内容。
root@doc-vm:~# docker history ubuntu
IMAGE CREATED CREATED BY SIZE COMMENT
20c44cd7596f 10 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 10 days ago /bin/sh -c mkdir -p /run/systemd && echo '... 7 B
<missing> 10 days ago /bin/sh -c sed -i 's/^#\s*\(deb.*universe\... 2.76 kB
<missing> 10 days ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0 B
<missing> 10 days ago /bin/sh -c set -xe && echo '#!/bin/sh' >... 745 B
<missing> 10 days ago /bin/sh -c #(nop) ADD file:280a445783f309c... 123 MB
删除镜像:
使用docker rmi删除镜像,命令格式:docker rmi IMAGE [IMAGE...]
如果镜像有多个标签,docker rmi命令只会删除该镜像的指定标签,并不会影响镜像文件,当只剩下一个标签的时候才会删除真正的镜像。
docker rmi ID [ID...]
首先会尝试删除所有指向该镜像的标签,然后删除该镜像的文件本身。
当基于该镜像的容器正在运行的时候,默认是无法删除的,可以使用-f参数强制删除。
创建镜像:
基于已经有的镜像创建镜像:
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
-a,--author="":作者信息
-c,--change=[]:提交的时候执行Dockerfile命令
-m,--message="":提交信息
-p,--pause=true:提交时暂停容器的运行
root@doc-vm:~# docker run -it ubuntu bash
root@6ea8cd52c3eb:/# touch test
root@6ea8cd52c3eb:/# exit
exit
root@doc-vm:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ea8cd52c3eb ubuntu "bash" 52 seconds ago Exited (0) 30 seconds ago nervous_hoover
root@doc-vm:~# docker commit -m "Added a Docker image" -a "deepking" 6ea8cd52c3eb ubuntutest:1.0
sha256:4ef2ad9d1aea15766c612d3c0090ad6128f803ebb8f4a4b75005d90001dbc893
root@doc-vm:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntutest 1.0 4ef2ad9d1aea 7 seconds ago 123 MB
基于本地模板导入:
主要使用docker import [OPTIONS] file|url|-[REPOSITORY[:TAG]]
使用OpenVZ提供的模板;来创建,下载地址是:https://openvz.org/Download/template/precreated
root@doc-vm:~# cd Downloads/
root@doc-vm:~/Downloads# ll
total 705496
drwxr-xr-x 2 doc doc 4096 11月 29 12:33 ./
drwxr-xr-x 16 doc doc 4096 11月 29 12:13 ../
-rw-rw-r-- 1 doc doc 111337720 11月 29 12:14 centos-6-x86-20170709.tar.xz
-rw-rw-r-- 1 doc doc 67229480 11月 29 12:13 centos-6-x86-minimal-20170709.tar.xz
-rw-rw-r-- 1 doc doc 69329732 11月 29 12:13 centos-7-x86_64-minimal-20170709.tar.xz
-rw-rw-r-- 1 doc doc 81103335 11月 29 12:13 debian-6.0-x86-minimal.tar.gz
-rw-rw-r-- 1 doc doc 155356791 11月 29 12:16 debian-6.0-x86.tar.gz
-rw-rw-r-- 1 doc doc 181654531 11月 29 12:18 ubuntu-15.10-x86_64.tar.gz
-rw-rw-r-- 1 doc doc 56375699 9月 14 17:13 VMwareTools-10.1.15-6627299.tar.gz
root@doc-vm:~# cat debian-6.0-x86-minimal.tar.gz | docker import - debian:6.0
存储和载入镜像:
root@doc-vm:~# docker save -o tomcat_latest.tar tomcat # 存储镜像
root@doc-vm:~# ll
drwxr-xr-x 16 doc doc 4096 11月 29 13:52 ./
drwxr-xr-x 3 root root 4096 11月 27 21:52 ../
-rw------- 1 root root 576676352 11月 29 13:52 tomcat_latest.tar
root@doc-vm:~# docker load --input tomcat_latest.tar # 载入镜像
Loaded image: tomcat:latest
上传镜像:
docker push NAME[:TAG] | [REGISTRY_HOST[:REGISTRY_PORT]/]NAME[:TAG]
第一次上传会提示登录。
本文转自 棋帅小七 51CTO博客,原文链接:http://blog.51cto.com/xvjunjie/2045609