docker

Posted icat-510

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker相关的知识,希望对你有一定的参考价值。

docker镜像

 转自https://www.cnblogs.com/liuyongsheng/p/6724749.html

1、获取镜像

docker pull NAME[:TAG] 如果不显式地指定TAG,则默认会选择latest标签,即下载仓库中最新版本的镜像。
//获取最新镜像
docker pull ubuntu
//获取指定标签镜像
docker pull ubuntu:14.04
//获取指定仓库镜像
docker pull registry.hub.docker.com/ubuntu:latest
docker pull dl.dockerpool.com:5000/ubuntu

2、查看镜像

//查看本地镜像
docker images
//重命名本地镜像
docker tag ubuntu:latest ubuntu:16.04
//获取镜像详细信息,JSON格式
docker inspect 0ef2e08ed3fa
//获取镜像详细信息中某一项信息
docker inspect -f {{".Architecture"}} 0ef2e08ed3fa
docker inspect -f {{".RepoTags"}} 0ef2

3、搜索镜像

//搜索指定名称镜像
docker search centos
docker search perconal*
//限制搜索结果数量,默认25行
docker search centos --limit 3
//是否截断描述部分输出,默认截断
docker search centos --no-trunc
docker search centos --no-trunc=false
//搜索指定星级以上镜像
docker search mysql -f stars=10

4、删除镜像

docker rmi IMAGE [IMAGE...] 其中IMAGE可以为镜像标签或者ID

#当同一个镜像拥有多个标签时,docker rmi命令只是删除了该镜像多个标签中的指定标签,并不影响镜像文件本身,但当镜像只剩下一个标签时,此时就会彻底删除镜像。
#当docker rmi命令后面跟的是镜像ID时,会尝试删除所有指向该镜像的标签,然后删除镜像文件本身,如果该镜像存在多个标签,默认无法删除,需要加-f进行删除。
#如果有基于该镜像创建的容器,则默认无法删除镜像,需要先通过docker rm删除容器,然后再用docker rmi删除镜像,或者使用docker rmi -f强制删除(不推荐)。

技术图片
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              98d35105a391        12 days ago         192 MB
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB
ubuntu              14.04               7c09e61e9035        4 weeks ago         188 MB
hello-world         latest              48b5124b2768        2 months ago        1.84 kB
helloworld          latest              48b5124b2768        2 months ago        1.84 kB
[[email protected] ~]# docker rmi 48b5124b2768 
Error response from daemon: conflict: unable to delete 48b5124b2768 (must be forced) - image is referenced in multiple repositories
[[email protected] ~]# docker rmi -f 48b5124b2768
Untagged: hello-world:latest
Untagged: hello-[email protected]:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
Untagged: helloworld:latest
Deleted: sha256:48b5124b2768d2b917edcb640435044a97967015485e812545546cbed5cf0233
Deleted: sha256:98c944e98de8d35097100ff70a31083ec57704be0991a92c51700465e4544d08
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              98d35105a391        12 days ago         192 MB
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB
ubuntu              14.04               7c09e61e9035        4 weeks ago         188 MB
[[email protected] ~]# docker rmi ubuntu:14.04
Error response from daemon: conflict: unable to remove repository reference "ubuntu:14.04" (must force) - container c7d73bda72fc is using its referenced image 7c09e61e9035
[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
00a42ce9d621        centos              "cat /etc/redhat-r..."   10 minutes ago      Exited (0) 10 minutes ago                       trusting_wescoff
c7d73bda72fc        ubuntu:14.04        "cat /etc/issue"         2 hours ago         Exited (0) 2 hours ago                          admiring_davinci
0a277f4e0f09        ubuntu              "/bin/bash"              2 hours ago         Exited (0) 2 hours ago                          tender_galileo
[[email protected] ~]# docker rm c7d73bda72fc
c7d73bda72fc
[[email protected] ~]# docker rmi ubuntu:14.04
Untagged: ubuntu:14.04
Untagged: [email protected]:62a5dce5ceccd7f1cb2672a571ebee52cad1f08eec9b57fe4965fb0968a9602e
Deleted: sha256:7c09e61e90350e8f5c0cba2979003bdfe32c2d027b68b4f0cf9063cdd7b4bafd
Deleted: sha256:304aecb5e13929f85d3ce2e9d83d0212866c8e55a460c94cf24bd75da1c7c153
Deleted: sha256:f302be18d46a45c0edbbd9b4bc02db764a4b0b8cd9bd0490f33dfaff039a3b62
Deleted: sha256:c523f3173f6028e5329fd401331c375f7b9b9e831d915fafaf358f55e36e3747
Deleted: sha256:94e631422130dc414878fd05efe3d59de44c9d8904696a7c299a83f378a92845
Deleted: sha256:c29b5eadf94a90a2abda13e765d4fad4825fd15621dea1d9a98b60b89b835c2a
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              98d35105a391        12 days ago         192 MB
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB
[[email protected] ~]# docker tag ubuntu:latest ubuntu:16.04
[[email protected] ~]# docker tag ubuntu:latest ubuntu:16
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16                  0ef2e08ed3fa        4 weeks ago         130 MB
ubuntu              16.04               0ef2e08ed3fa        4 weeks ago         130 MB
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB
[[email protected] ~]# docker rmi ubuntu:16 ubuntu:16.04
Untagged: ubuntu:16
Untagged: ubuntu:16.04
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              98d35105a391        12 days ago         192 MB
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB
技术图片

5、创建镜像

创建镜像的方式有:基于已有镜像的容器创建、基于本地模版导入、基于Dockerfile创建

5.1、基于已有镜像的容器创建:

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

  主要选项:
  -a, --author=" "    //作者信息
  -m, --message=" "   //提交信息
  -p, --pause             //提交时暂停容器运行(默认)

技术图片
[[email protected] ~]# docker run -ti ubuntu /bin/bash
[email protected]:/# ls   
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[email protected]:/# touch test
[email protected]:/# exit
exit
[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
29acd66b0898        ubuntu              "/bin/bash"         53 seconds ago      Exited (0) 35 seconds ago                       musing_bardeen
[[email protected] ~]# docker commit -m "Add a new file" -a "james" 29acd66b0898 test
sha256:545d60ae0415b0fb11591b37644e05f05afc535afd573891017b47cffdd769b0
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test                latest              545d60ae0415        12 seconds ago      130 MB
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB
技术图片

5.2、基于本地模版导入

推荐使用OpenVZ提供的模版来创建,下载地址:https://openvz.org/Download/template/precreated

待补充!!!

6、导出和导入镜像

可以使用docker save和docker load命令来导出和导入镜像

技术图片
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB
[[email protected] ~]# docker save -o /data/ubuntu_16.04.tar ubuntu:latest
[[email protected] ~]# du -sh /data/ubuntu_16.04.tar 
130M    /data/ubuntu_16.04.tar
[[email protected] ~]# docker rmi ubuntu:latest
Untagged: ubuntu:latest
Deleted: sha256:0ef2e08ed3fabfc44002ccb846c4f2416a2135affc3ce39538834059606f32dd
Deleted: sha256:0d58a35162057295d273c5fb8b7e26124a31588cdadad125f4bce63b638dddb5
Deleted: sha256:cb7f997e049c07cdd872b8354052c808499937645f6164912c4126015df036cc
Deleted: sha256:fcb4581c4f016b2e9761f8f69239433e1e123d6f5234ca9c30c33eba698487cc
Deleted: sha256:b53cd3273b78f7f9e7059231fe0a7ed52e0f8e3657363eb015c61b2a6942af87
Deleted: sha256:745f5be9952c1a22dd4225ed6c8d7b760fe0d3583efd52f91992463b53f7aea3
[[email protected] ~]# docker load -q -i /data/ubuntu_16.04.tar 
Loaded image: ubuntu:latest
[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              0ef2e08ed3fa        4 weeks ago         130 MB
[[email protected] ~]# docker rmi ubuntu:latest
Untagged: ubuntu:latest
Deleted: sha256:0ef2e08ed3fabfc44002ccb846c4f2416a2135affc3ce39538834059606f32dd
Deleted: sha256:0d58a35162057295d273c5fb8b7e26124a31588cdadad125f4bce63b638dddb5
Deleted: sha256:cb7f997e049c07cdd872b8354052c808499937645f6164912c4126015df036cc
Deleted: sha256:fcb4581c4f016b2e9761f8f69239433e1e123d6f5234ca9c30c33eba698487cc
Deleted: sha256:b53cd3273b78f7f9e7059231fe0a7ed52e0f8e3657363eb015c61b2a6942af87
Deleted: sha256:745f5be9952c1a22dd4225ed6c8d7b760fe0d3583efd52f91992463b53f7aea3
[[email protected] ~]# docker load < /data/ubuntu_16.04.tar 
745f5be9952c: Loading layer [==================================================>] 135.7 MB/135.7 MB
85782553e37a: Loading layer [==================================================>] 15.87 kB/15.87 kB
29660d0e5bb2: Loading layer [==================================================>] 11.78 kB/11.78 kB
440e02c3dcde: Loading layer [==================================================>] 4.608 kB/4.608 kB
56827159aa8b: Loading layer [==================================================>] 3.072 kB/3.072 kB
Loaded image: ubuntu:latest
技术图片

7、上传镜像

docker push NAME[:TAG]

待补充!!!

以上是关于docker的主要内容,如果未能解决你的问题,请参考以下文章

markdown docker-compose片段

sh Docker片段

《Docker 源码分析》全球首发啦!

docker 部署 coredns(内部域名解析)

docker 部署 coredns(内部域名解析)

将 Docker 容器限制为单个 cpu 核心