经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Docker » 查看文章
【Docker】第二篇 Docker镜像管理 - 旅行者-Travel
来源:cnblogs  作者:旅行者-Travel  时间:2018/9/25 19:13:50  对本文有异议

一、搜索镜像

  1. 1、下载一个docker镜像:我们可以通过登陆docker网站搜索自己需要的镜像,可以选择自己所需要的版本,然后通过详情也可以看到:
    网址:https://hub.docker.com/
    2、我们也可以通过命令行搜索:
    [root@web130 ~]# docker search centos:7 #如果不带版本号默认搜到是最新版本的:latest
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. centos The official build of CentOS. 4726 [OK]
  4. ansible/centos7-ansible Ansible on Centos7 118 [OK]
  5. *我们看输出内容可以很容易的理解,分别是:名称,描述,星(类似github上的stars),是否官方镜像,是否自动装配。
  6. *如下搜索自动创建,以及指定星级的镜像,星级默认为0,如果加--no-trunc输出信息不截断显示,默认是否--no-trunc=true|fales
  7. [root@web130 ~]# docker search --automated -s 3 nginx
  8. Flag --automated has been deprecated, use --filter=is-automated=true instead
  9. Flag --stars has been deprecated, use --filter=stars=3 instead
  10. NAME DESCRIPTION
  11. .....省略显示 ...

二、查看和获取镜像

  1. 1images列出镜像:关于images更多信息可以通过man docker-images来查看
  2. *此内容下显示:来自那个仓库、标签信息、镜像ID、创建时间、镜像大小
  3. [root@web130 ~]# docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. 2、下载一个镜像,如果后边不跟版本号,默认是最新非稳定版本:
  6. [root@web130 ~]# docker pull ubuntu #下载镜像
  7. *也可以通过上边搜索选择合适的版本号,如下:
  8. [root@web130 ~]# docker pull ubuntu:14.04
  9. [root@web130 ~]# docker pull centos:7.4.1708
  10. [root@web130 ~]# docker images #查看下载的镜像,可以看到刚刚下载的是三个镜像
  11. REPOSITORY TAG IMAGE ID CREATED SIZE
  12. ubuntu 14.04 c32fae490809 2 weeks ago 188MB
  13. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  14. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  15. [root@web130 ~]#
  16. *我们也可以跟上仓库地址下在,非官网镜像源仓库下载指定镜像:
  17. [root@web130 ~]# docker pull 仓库地址/ubuntu:14.04
  18.  
  19. 3、利用该镜像创建一个容器:
  20. [root@web130 ~]# docker run -it centos:7.4.1708 /bin/bash #后边详细说明参数,如果本地没有下载此镜像,自动先下载镜像并运行
  21. *如下由于没有指定标签,自动下载了最新版本的centos,并打开了一个shell
  22. [root@web130 ~]# docker run -it centos /bin/bash
  23. Unable to find image 'centos:latest' locally
  24. latest: Pulling from library/centos
  25. 256b176beaff: Pull complete
  26. Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
  27. Status: Downloaded newer image for centos:latest
  28. [root@3e876fdaaf89 /]# ls
  29. anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
  30. [root@3e876fdaaf89 /]#
  31. 4、使用tag命令为镜像添加标签
  32. [root@web130 ~]# docker images
  33. REPOSITORY TAG IMAGE ID CREATED SIZE
  34. ubuntu 14.04 c32fae490809 2 weeks ago 188MB
  35. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  36. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  37. centos latest 5182e96772bf 6 weeks ago 200MB
  38. [root@web130 ~]# docker tag centos:latest mycentos:latest #我们可以看到多出来一个镜像,tag的作用类似与添加链接的作用,他们指向同一个源镜像文件、
  39. [root@web130 ~]# docker images
  40. REPOSITORY TAG IMAGE ID CREATED SIZE
  41. ubuntu 14.04 c32fae490809 2 weeks ago 188MB
  42. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  43. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  44. centos latest 5182e96772bf 6 weeks ago 200MB
  45. mycentos latest 5182e96772bf 6 weeks ago 200MB
  46. 5、使用inspect命令查看详细信息:
  47. [root@web130 ~]# docker inspect mycentos
  48. [root@web130 ~]#
  49. 6、使用history命令查看镜像历史
  50. [root@web130 ~]# docker history mycentos #镜像使用多层组成,列出各层的创建信息,--no-trunc选项输出完整的命令
  51. IMAGE CREATED CREATED BY SIZE COMMENT
  52. 5182e96772bf 6 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
  53. <missing> 6 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
  54. <missing> 6 weeks ago /bin/sh -c #(nop) ADD file:6340c690b08865d7e… 200MB
  55. [root@web130 ~]#

三、删除镜像

  1. 1、通过标签删除镜像
  2. [root@web130 ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. ubuntu 14.04 c32fae490809 2 weeks ago 188MB
  5. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  6. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  7. centos latest 5182e96772bf 6 weeks ago 200MB
  8. mycentos latest 5182e96772bf 6 weeks ago 200MB
  9. [root@web130 ~]# docker rmi mycentos:latest
  10. Untagged: mycentos:latest
  11. [root@web130 ~]# docker images 删除。
  12. REPOSITORY TAG IMAGE ID CREATED SIZE
  13. ubuntu 14.04 c32fae490809 2 weeks ago 188MB
  14. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  15. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  16. centos latest 5182e96772bf 6 weeks ago 200MB
  17. #我们可以看到mycentos标签已经被删除,我们可以看到源镜像centos依然存在,所以删除标签不影响镜像文件,但是仅剩一个标签的时候要小心,会将镜像文件
  18. 2、通过镜像ID删除镜像
  19. [root@web130 ~]# docker rmi centos:latest
  20. Error response from daemon: conflict: unable to remove repository reference "centos:latest" (must force) - container 3e876fdaaf89 is using its referenced image 5182e96772bf
  21. [root@web130 ~]# docker ps -a
  22. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  23. 3e876fdaaf89 centos "/bin/bash" 40 minutes ago Exited (0) 35 minutes ago friendly_haibt
  24. [root@web130 ~]#
  25. #显示无法删除,因为有一个基于它的容器存在,我们也可以强制删除 -f:docker rmi -f centos,但是不建议这样删除,所有我们如下操作先删除依赖的容器
  26. [root@web130 ~]# docker rm 3e876fdaaf89 #通过容器id删除镜像
  27. 3e876fdaaf89
  28. [root@web130 ~]# docker ps -a
  29. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  30. [root@web130 ~]# docker rmi centos:latest #可以看到它删除了这个镜像文件的所有层
  31. Untagged: centos:latest
  32. Untagged: centos@sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
  33. Deleted: sha256:5182e96772bf11f4b912658e265dfe0db8bd314475443b6434ea708784192892
  34. Deleted: sha256:1d31b5806ba40b5f67bde96f18a181668348934a44c9253b420d5f04cfb4e37a
  35. [root@web130 ~]#

四、创建镜像

创建镜像的方法:
*基于已有的镜像容器创建
*基于本地模板导入
*基于Dockerfile创建 (后文重点学习Dockerfile)

  1. 1、基于已有镜像的容器创建
  2. [root@web130 ~]# docker run -it ubuntu:14.04 /bin/bash
  3. root@5b117c5dffe1:/# touch yanglt
  4. root@5b117c5dffe1:/# exit
  5. exit
  6. [root@web130 ~]# docker images;
  7. REPOSITORY TAG IMAGE ID CREATED SIZE
  8. ubuntu 14.04 c32fae490809 2 weeks ago 188MB
  9. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  10. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  11. [root@web130 ~]# docker commit -m "a new file" -a "Yanglt" 5b117c5dffe1 test:0.1
  12. sha256:40925d6bc8025b8ca6ec68249f4cb371844896a39817f9e9bf80eda4119e8d6f
  13. [root@web130 ~]# docker images
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. test 0.1 40925d6bc802 9 seconds ago 188MB
  16. ubuntu 14.04 c32fae490809 2 weeks ago 188MB
  17. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  18. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  19. #可以看到一个新的镜像test
  20. 2、基于本地模板导入创建
  21. 下载地址:https://download.openvz.org/template/precreated/
  22. 选择要下载镜像模板:
  23. [root@web130 ~]# wget https://download.openvz.org/template/precreated/centos-7-x86_64-minimal.tar.gz
  24. [root@web130 ~]# cat centos-7-x86_64-minimal.tar.gz |docker import - centos:7
  25. [root@web130 ~]# cat centos-7-x86_64-minimal.tar.gz |docker import - centos:7
  26. sha256:3458fe08f84de52d5354a80793e5f806420a2330fc2c2425bb4878bcb8b0efd8
  27. [root@web130 ~]# docker images #可以看到已经出现新的镜像
  28. REPOSITORY TAG IMAGE ID CREATED SIZE
  29. centos 7 3458fe08f84d 54 seconds ago 435MB
  30. test 0.1 40925d6bc802 About an hour ago 188MB
  31. ubuntu 14.04 c32fae490809 2 weeks ago 188MB
  32. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  33. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  34. [root@web130 ~]#

五、导出和载入镜像

  1. 1、导出镜像
  2. -o + 要导出最后的文件名 + 要导出的镜像的标签(名) id
  3. [root@localhost ~]# docker images
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. test 0.1 40925d6bc802 20 minutes ago 188MB
  6. ubuntu 14.04 c32fae490809 2 weeks ago 188MB
  7. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  8. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  9. [root@localhost ~]#
  10. [root@localhost ~]# docker save -o myubuntu_14.04.tar ubuntu:14.04
  11. [root@localhost ~]# ll |grep my*
  12. -rw------- 1 root root 197191680 9 20 20:03 myubuntu_14.04.tar
  13. [root@localhost ~]#
  14. 2、导入镜像(恢复原有镜像)
  15. *我们可以将导出的源镜像删除,然后在导入
  16. [root@localhost ~]# docker rmi ubuntu:14.04
  17. [root@localhost ~]# docker images
  18. REPOSITORY TAG IMAGE ID CREATED SIZE
  19. test 0.1 40925d6bc802 29 minutes ago 188MB
  20. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  21. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  22. [root@localhost ~]# docker load < myubuntu_14.04.tar
  23. Loaded image: ubuntu:14.04
  24. [root@localhost ~]# docker images
  25. REPOSITORY TAG IMAGE ID CREATED SIZE
  26. test 0.1 40925d6bc802 29 minutes ago 188MB
  27. ubuntu 14.04 c32fae490809 2 weeks ago 188MB
  28. ubuntu latest cd6d8154f1e1 2 weeks ago 84.1MB
  29. centos 7.4.1708 d3949e34634c 6 weeks ago 197MB
  30. [root@localhost ~]#

六、上传镜像

  1. #我们可以把自己的镜像传到dockerhub官网上,前提是注册一个用户
  2. [root@localhost ~]# docker push image_name

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号