当前位置:   article > 正文

4-Docker命令之docker commit

docker commit

1.docker commit介绍

docker commit命令是用于根据docker容器的改变创建一个新的docker镜像

2.docker commit用法

docker commit [参数] container [repository[:tag]]

  1. [root@centos79 ~]# docker commit --help
  2. Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
  3. Create a new image from a container's changes
  4. Aliases:
  5. docker container commit, docker commit
  6. Options:
  7. -a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")
  8. -c, --change list Apply Dockerfile instruction to the created image
  9. -m, --message string Commit message
  10. -p, --pause Pause container during commit (default true)
  11. [root@centos79 ~]#

3.实例

3.1.基于容器更改创建新镜像

-->进入运行中的docker容器

命令:

docker exec -it centos-ssh /bin/bash

  1. [root@centos79 ~]# docker ps -a | grep centos-ssh
  2. 803b6b35366a centos-ssh:1.0 "/bin/sh -c '/usr/sb…" 12 minutes ago Up 12 minutes 0.0.0.0:32768->22/tcp, :::32768->22/tcp centos-ssh
  3. [root@centos79 ~]# docker exec -it centos-ssh /bin/bash
  4. [root@803b6b35366a ansible]#

-->在容器内部使用vim命令

命令:

vim

  1. [root@803b6b35366a ansible]# vim
  2. bash: vim: command not found
  3. [root@803b6b35366a ansible]#

提示没有vim命令

-->docker容器内安装vim命令

命令:

yum install vim -y

  1. [root@803b6b35366a ansible]# yum install vim -y
  2. Loaded plugins: fastestmirror, ovl
  3. http://mirrors.163.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://mirrors.163.com/centos/7/os/x86_64/repodata/repomd.xml: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
  4. Trying other mirror.
  5. http://mirrors.163.com/centos/7/os/x86_64/repodata/repomd.xml: [Errno 12] Timeout on http://mirrors.163.com/centos/7/os/x86_64/repodata/repomd.xml: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
  6. Trying other mirror.
  7. base | 3.6 kB 00:00:00
  8. extras | 2.9 kB 00:00:00
  9. updates | 2.9 kB 00:00:00
  10. Loading mirror speeds from cached hostfile
  11. Resolving Dependencies
  12. --> Running transaction check
  13. ---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be installed
  14. --> Processing Dependency: vim-common = 2:7.4.629-8.el7_9 for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
  15. --> Processing Dependency: which for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
  16. --> Processing Dependency: libgpm.so.2()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
  17. --> Running transaction check
  18. ---> Package gpm-libs.x86_64 0:1.20.7-6.el7 will be installed
  19. ---> Package vim-common.x86_64 2:7.4.629-8.el7_9 will be installed
  20. --> Processing Dependency: vim-filesystem for package: 2:vim-common-7.4.629-8.el7_9.x86_64
  21. ---> Package which.x86_64 0:2.20-7.el7 will be installed
  22. --> Running transaction check
  23. ---> Package vim-filesystem.x86_64 2:7.4.629-8.el7_9 will be installed
  24. --> Finished Dependency Resolution
  25. Dependencies Resolved
  26. =======================================================================================================================================
  27. Package Arch Version Repository Size
  28. =======================================================================================================================================
  29. Installing:
  30. vim-enhanced x86_64 2:7.4.629-8.el7_9 updates 1.1 M
  31. Installing for dependencies:
  32. gpm-libs x86_64 1.20.7-6.el7 base 32 k
  33. vim-common x86_64 2:7.4.629-8.el7_9 updates 5.9 M
  34. vim-filesystem x86_64 2:7.4.629-8.el7_9 updates 11 k
  35. which x86_64 2.20-7.el7 base 41 k
  36. Transaction Summary
  37. =======================================================================================================================================
  38. Install 1 Package (+4 Dependent packages)
  39. Total download size: 7.1 M
  40. Installed size: 23 M
  41. Downloading packages:
  42. (1/5): gpm-libs-1.20.7-6.el7.x86_64.rpm | 32 kB 00:00:08
  43. (2/5): vim-enhanced-7.4.629-8.el7_9.x86_64.rpm | 1.1 MB 00:00:21
  44. (3/5): vim-filesystem-7.4.629-8.el7_9.x86_64.rpm | 11 kB 00:00:03
  45. (4/5): which-2.20-7.el7.x86_64.rpm | 41 kB 00:00:06
  46. vim-common-7.4.629-8.el7_9.x86 FAILED ] 99 B/s | 3.2 MB 11:12:56 ETA
  47. http://mirrors.163.com/centos/7/updates/x86_64/Packages/vim-common-7.4.629-8.el7_9.x86_64.rpm: [Errno 12] Timeout on http://mirrors.163.com/centos/7/updates/x86_64/Packages/vim-common-7.4.629-8.el7_9.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
  48. Trying other mirror.
  49. vim-common-7.4.629-8.el7_9.x86 FAILED
  50. http://mirrors.163.com/centos/7/updates/x86_64/Packages/vim-common-7.4.629-8.el7_9.x86_64.rpm: [Errno 12] Timeout on http://mirrors.163.com/centos/7/updates/x86_64/Packages/vim-common-7.4.629-8.el7_9.x86_64.rpm: (28, 'Operation too slow. Less than 1000 bytes/sec transferred the last 30 seconds')
  51. Trying other mirror.
  52. (5/5): vim-common-7.4.629-8.el7_9.x86_64.rpm | 5.9 MB 00:02:02
  53. ---------------------------------------------------------------------------------------------------------------------------------------
  54. Total 33 kB/s | 7.1 MB 00:03:35
  55. Running transaction check
  56. Running transaction test
  57. Transaction test succeeded
  58. Running transaction
  59. Installing : 2:vim-filesystem-7.4.629-8.el7_9.x86_64 1/5
  60. Installing : 2:vim-common-7.4.629-8.el7_9.x86_64 2/5
  61. Installing : which-2.20-7.el7.x86_64 3/5
  62. install-info: No such file or directory for /usr/share/info/which.info.gz
  63. Installing : gpm-libs-1.20.7-6.el7.x86_64 4/5
  64. Installing : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 5/5
  65. Verifying : gpm-libs-1.20.7-6.el7.x86_64 1/5
  66. Verifying : 2:vim-enhanced-7.4.629-8.el7_9.x86_64 2/5
  67. Verifying : which-2.20-7.el7.x86_64 3/5
  68. Verifying : 2:vim-common-7.4.629-8.el7_9.x86_64 4/5
  69. Verifying : 2:vim-filesystem-7.4.629-8.el7_9.x86_64 5/5
  70. Installed:
  71. vim-enhanced.x86_64 2:7.4.629-8.el7_9
  72. Dependency Installed:
  73. gpm-libs.x86_64 0:1.20.7-6.el7 vim-common.x86_64 2:7.4.629-8.el7_9 vim-filesystem.x86_64 2:7.4.629-8.el7_9 which.x86_64 0:2.20-7.el7
  74. Complete!
  75. [root@803b6b35366a ansible]# which vim
  76. /bin/vim
  77. [root@803b6b35366a ansible]#

-->退出docker容器,使用docker commit命令将此已经安装vim命令的docker容器进行commit,以便未来使用

命令:

exit

docker commit centos-ssh centos-vim:1.0

docker images | grep -i centos-vim

  1. [root@803b6b35366a ansible]# exit
  2. exit
  3. [root@centos79 ~]# docker commit centos-ssh centos-vim:1.0
  4. sha256:fc50e118f3914fc6e29c5727b42aaed8bd9589c18fda540bcaae30ee563a906a
  5. [root@centos79 ~]# docker images | grep centos-vim
  6. centos-vim 1.0 fc50e118f391 29 seconds ago 1.01GB
  7. [root@centos79 ~]#

-->基于新的镜像进行验证

命令:

docker run -itd -P --privileged --name centos-vim centos-vim:1.0

docker exec -it db944 /bin/bash

  1. [root@centos79 ~]# docker run -itd -P --privileged --name centos-vim centos-vim:1.0
  2. db944665b1a0596f208b1fa2442bbf9540e6d868a885fb05edc1100f4888aeef
  3. [root@centos79 ~]# docker exec -it db944 /bin/bash
  4. [root@db944665b1a0 ansible]# which vim
  5. /bin/vim
  6. [root@db944665b1a0 ansible]#

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/406367
推荐阅读
相关标签
  

闽ICP备14008679号