首页 > 代码库 > 三、 Docker Image操作

三、 Docker Image操作

List Images

列出本地所有Docker image

 

[plain] view plain copy
 
 技术分享技术分享
  1. # docker images  
  2. REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE  
  3. centos              centos6             25c5298b1a36        2 weeks ago         215.8 MB  

 

可以看到,REPOSITORY是image来源库,此处是centos

TAG:每个image有自己的tag,此处是centos6

使用该image启动Docker Container的时候,名字为:centos:centos6

IMAGE ID:每个image有自己独立的ID

下载新的image

 

查找新的image

众多Docker的使用者会创建自己的image,有部分image就会上传到Docker Hub中,我们可以直接在Docker Hub中查找合适的image。

也可以使用docker search命令查找image

 

[plain] view plain copy
 
 技术分享技术分享
  1. # docker search centos | more  
  2. NAME                                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED  
  3. centos                                            The official build of CentOS.                   685       [OK]         
  4. tianon/centos                                     CentOS 5 and 6, created using rinse instea...   29                     
  5. ansible/centos7-ansible                           Ansible on Centos7                              22                   [OK]  
  6. ariya/centos6-teamcity-server                     TeamCity Server 8.1 on CentOS 6                 8                    [OK]  
  7. tutum/centos                                      Centos image with SSH access. For the root...   8                    [OK]  
  8. berngp/docker-zabbix                              Runs Zabbix Server and Zabbix Web UI on a ...   8                    [OK]  
  9. saltstack/centos-6-minimal                                                                        8                    [OK]  
  10. blalor/centos                                     Bare-bones base CentOS 6.5 image                7                    [OK]  
  11. centos/freeipa                                    FreeIPA in Docker on CentOS                     7                      
  12. steeef/graphite-centos                            CentOS 6.x with Graphite and Carbon via ng...   6                    [OK]  
  13. dockerfiles/centos-lamp                                                                           6                    [OK]  
  14. gluster/gluster                                   GlusterFS 3.5 - CentOS 6.5 Docker repo          6                    [OK]  
  15. ariya/centos6-teamcity-agent                      Build agent for TeamCity 8.1                    5                    [OK]  
  16. tutum/centos-6.4                                  DEPRECATED. Use tutum/centos:6.4 instead. ...   5                    [OK]  
  17. jdeathe/centos-ssh-apache-php                     CentOS-6 6.5 x86_64 / Apache / PHP / PHP m...   5                    [OK]  
  18. cern/centos-wlcg-wn                               CentosOS 6 image with pre-installed softwa...   4                      

创建新的image

更新现有的image

交互模式启动centos:centos6,安装MySQL

 

[plain] view plain copy
 
 技术分享技术分享
  1. # docker run -t -i centos:centos6 /bin/bash  
  2. bash-4.1#   
  3. bash-4.1# yum install -y mysql mysql-serve  

 

完成后,exit退出交互模式

 

[plain] view plain copy
 
 技术分享技术分享
  1. # docker ps -a  
  2. CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES  
  3. 2e9de4d9c350        centos:centos6      /bin/bash           2 minutes ago       Exited (0) 7 seconds ago                           mad_colden     

 

可以看到刚刚关掉的container ID

 

[plain] view plain copy
 
 技术分享技术分享
  1. # docker commit -m="install mysql" -a="Hongtu Zang" 2e9de4d9c350 centos6:hongtu  
  2. 5f3bca7952cac8900e08b5fac11cdd1a4941803edbb5442593a171fe574ed961  

 

-m Message,描述信息

-a author,image的创建者

 

[plain] view plain copy
 
 技术分享技术分享
  1. # docker images  
  2. REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE  
  3. centos6             hongtu              5f3bca7952ca        About a minute ago   320.3 MB  
  4. centos              centos6             25c5298b1a36        2 weeks ago          215.8 MB  

 

可以看到,多出来一个TAG为hongtu的image,是我们刚刚创建好的

从Dockerfile创建image

创建一个Dockerfile

 

[plain] view plain copy
 
 技术分享技术分享
  1. # mkdir hongtu  
  2. # cd hongtu/  
  3. # touch Dockerfile  

 

编辑Dockerfile内容:

 

[plain] view plain copy
 
 技术分享技术分享
  1. # This is a comment  
  2. FROM centos:centos6  
  3. MAINTAINER Hongtu Zang <hongtu_zang@chinacloudly.com>  
  4. RUN yum install -y mysql mysql-server  

 

FROM 基础image

MAINTAINER 作者

RUN 需要执行的命令,此处是安装mysql-server

docker run创建新的模板

 

[plain] view plain copy
 
 技术分享技术分享
  1. # docker build -t="hongtu/centos6:v2" .  
  2. Sending build context to Docker daemon  2.56 kB  
  3. Sending build context to Docker daemon   
  4. Step 0 : FROM centos:centos6  
  5.  ---> 25c5298b1a36  
  6. Step 1 : MAINTAINER Hongtu Zang <hongtu_zang@chinacloudly.com>  
  7.  ---> Using cache  
  8.  ---> 52f35fcefd08  
  9. Step 2 : RUN yum install -y mysql mysql-server  
  10.  ---> Running in 35ab46137fe7  
  11. Loaded plugins: fastestmirror  
  12. Setting up Install Process  
  13. Resolving Dependencies  
  14. --> Running transaction check  
  15. ---> Package mysql.x86_64 0:5.1.73-3.el6_5 will be installed  
  16. --> Processing Dependency: mysql-libs = 5.1.73-3.el6_5 for package: mysql-5.1.73-3.el6_5.x86_64  
  17. --> Processing Dependency: perl(Sys::Hostname) for package: mysql-5.1.73-3.el6_5.x86_64  
  18. --> Processing Dependency: perl(IPC::Open3) for package: mysql-5.1.73-3.el6_5.x86_64  
  19. --> Processing Dependency: perl(Getopt::Long) for package: mysql-5.1.73-3.el6_5.x86_64  
  20. --> Processing Dependency: perl(File::Temp) for package: mysql-5.1.73-3.el6_5.x86_64  
  21. --> Processing Dependency: perl(Fcntl) for package: mysql-5.1.73-3.el6_5.x86_64  
  22. --> Processing Dependency: perl(Exporter) for package: mysql-5.1.73-3.el6_5.x86_64  
  23. --> Processing Dependency: libmysqlclient_r.so.16(libmysqlclient_16)(64bit) for package: mysql-5.1.73-3.el6_5.x86_64  
  24. --> Processing Dependency: libmysqlclient.so.16(libmysqlclient_16)(64bit) for package: mysql-5.1.73-3.el6_5.x86_64  
  25. --> Processing Dependency: /usr/bin/perl for package: mysql-5.1.73-3.el6_5.x86_64  
  26. --> Processing Dependency: libmysqlclient_r.so.16()(64bit) for package: mysql-5.1.73-3.el6_5.x86_64  
  27. --> Processing Dependency: libmysqlclient.so.16()(64bit) for package: mysql-5.1.73-3.el6_5.x86_64  
  28. ---> Package mysql-server.x86_64 0:5.1.73-3.el6_5 will be installed  
  29. --> Processing Dependency: perl-DBI for package: mysql-server-5.1.73-3.el6_5.x86_64  
  30. --> Processing Dependency: perl-DBD-MySQL for package: mysql-server-5.1.73-3.el6_5.x86_64  
  31. --> Processing Dependency: perl(DBI) for package: mysql-server-5.1.73-3.el6_5.x86_64  
  32. --> Running transaction check  
  33. ---> Package mysql-libs.x86_64 0:5.1.73-3.el6_5 will be installed  
  34. ---> Package perl.x86_64 4:5.10.1-136.el6_6.1 will be installed  
  35. --> Processing Dependency: perl-libs = 4:5.10.1-136.el6_6.1 for package: 4:perl-5.10.1-136.el6_6.1.x86_64  
  36. --> Processing Dependency: perl-libs for package: 4:perl-5.10.1-136.el6_6.1.x86_64  
  37. --> Processing Dependency: perl(version) for package: 4:perl-5.10.1-136.el6_6.1.x86_64  
  38. --> Processing Dependency: perl(Pod::Simple) for package: 4:perl-5.10.1-136.el6_6.1.x86_64  
  39. --> Processing Dependency: perl(Module::Pluggable) for package: 4:perl-5.10.1-136.el6_6.1.x86_64  
  40. --> Processing Dependency: libperl.so()(64bit) for package: 4:perl-5.10.1-136.el6_6.1.x86_64  
  41. ---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed  
  42. ---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed  
  43. --> Running transaction check  
  44. ---> Package perl-Module-Pluggable.x86_64 1:3.90-136.el6_6.1 will be installed  
  45. ---> Package perl-Pod-Simple.x86_64 1:3.13-136.el6_6.1 will be installed  
  46. --> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.13-136.el6_6.1.x86_64  
  47. ---> Package perl-libs.x86_64 4:5.10.1-136.el6_6.1 will be installed  
  48. ---> Package perl-version.x86_64 3:0.77-136.el6_6.1 will be installed  
  49. --> Running transaction check  
  50. ---> Package perl-Pod-Escapes.x86_64 1:1.04-136.el6_6.1 will be installed  
  51. --> Finished Dependency Resolution  
  52.   
  53. Dependencies Resolved  
  54.   
  55. ================================================================================  
  56.  Package                  Arch      Version                    Repository  Size  
  57. ================================================================================  
  58. Installing:  
  59.  mysql                    x86_64    5.1.73-3.el6_5             base       894 k  
  60.  mysql-server             x86_64    5.1.73-3.el6_5             base       8.6 M  
  61. Installing for dependencies:  
  62.  mysql-libs               x86_64    5.1.73-3.el6_5             base       1.2 M  
  63.  perl                     x86_64    4:5.10.1-136.el6_6.1       updates     10 M  
  64.  perl-DBD-MySQL           x86_64    4.013-3.el6                base       134 k  
  65.  perl-DBI                 x86_64    1.609-4.el6                base       705 k  
  66.  perl-Module-Pluggable    x86_64    1:3.90-136.el6_6.1         updates     40 k  
  67.  perl-Pod-Escapes         x86_64    1:1.04-136.el6_6.1         updates     32 k  
  68.  perl-Pod-Simple          x86_64    1:3.13-136.el6_6.1         updates    212 k  
  69.  perl-libs                x86_64    4:5.10.1-136.el6_6.1       updates    578 k  
  70.  perl-version             x86_64    3:0.77-136.el6_6.1         updates     51 k  
  71.   
  72. Transaction Summary  
  73. ================================================================================  
  74. Install      11 Package(s)  
  75.   
  76. Total download size: 23 M  
  77. Installed size: 69 M  
  78. Downloading Packages:  
  79. --------------------------------------------------------------------------------  
  80. Total                                           104 kB/s |  23 MB     03:42       
  81. warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY  
  82. Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6  
  83. Importing GPG key 0xC105B9DE:  
  84.  Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>  
  85.  Package: centos-release-6-6.el6.centos.12.2.x86_64 (@CentOS/$releasever)  
  86.  From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6  
  87. Running rpm_check_debug  
  88. Running Transaction Test  
  89. Transaction Test Succeeded  
  90. Running Transaction  
  91. Warning: RPMDB altered outside of yum.  
  92.   Installing : mysql-libs-5.1.73-3.el6_5.x86_64                            1/11   
  93.   Installing : 1:perl-Pod-Escapes-1.04-136.el6_6.1.x86_64                  2/11   
  94.   Installing : 4:perl-libs-5.10.1-136.el6_6.1.x86_64                       3/11   
  95.   Installing : 1:perl-Module-Pluggable-3.90-136.el6_6.1.x86_64             4/11   
  96.   Installing : 1:perl-Pod-Simple-3.13-136.el6_6.1.x86_64                   5/11   
  97.   Installing : 3:perl-version-0.77-136.el6_6.1.x86_64                      6/11   
  98.   Installing : 4:perl-5.10.1-136.el6_6.1.x86_64                            7/11   
  99.   Installing : perl-DBI-1.609-4.el6.x86_64                                 8/11   
  100.   Installing : perl-DBD-MySQL-4.013-3.el6.x86_64                           9/11   
  101.   Installing : mysql-5.1.73-3.el6_5.x86_64                                10/11   
  102.   Installing : mysql-server-5.1.73-3.el6_5.x86_64                         11/11   
  103.   Verifying  : 3:perl-version-0.77-136.el6_6.1.x86_64                      1/11   
  104.   Verifying  : perl-DBD-MySQL-4.013-3.el6.x86_64                           2/11   
  105.   Verifying  : mysql-libs-5.1.73-3.el6_5.x86_64                            3/11   
  106.   Verifying  : mysql-server-5.1.73-3.el6_5.x86_64                          4/11   
  107.   Verifying  : mysql-5.1.73-3.el6_5.x86_64                                 5/11   
  108.   Verifying  : perl-DBI-1.609-4.el6.x86_64                                 6/11   
  109.   Verifying  : 1:perl-Pod-Simple-3.13-136.el6_6.1.x86_64                   7/11   
  110.   Verifying  : 4:perl-5.10.1-136.el6_6.1.x86_64                            8/11   
  111.   Verifying  : 4:perl-libs-5.10.1-136.el6_6.1.x86_64                       9/11   
  112.   Verifying  : 1:perl-Module-Pluggable-3.90-136.el6_6.1.x86_64            10/11   
  113.   Verifying  : 1:perl-Pod-Escapes-1.04-136.el6_6.1.x86_64                 11/11   
  114.   
  115. Installed:  
  116.   mysql.x86_64 0:5.1.73-3.el6_5       mysql-server.x86_64 0:5.1.73-3.el6_5        
  117.   
  118. Dependency Installed:  
  119.   mysql-libs.x86_64 0:5.1.73-3.el6_5                                              
  120.   perl.x86_64 4:5.10.1-136.el6_6.1                                                
  121.   perl-DBD-MySQL.x86_64 0:4.013-3.el6                                             
  122.   perl-DBI.x86_64 0:1.609-4.el6                                                   
  123.   perl-Module-Pluggable.x86_64 1:3.90-136.el6_6.1                                 
  124.   perl-Pod-Escapes.x86_64 1:1.04-136.el6_6.1                                      
  125.   perl-Pod-Simple.x86_64 1:3.13-136.el6_6.1                                       
  126.   perl-libs.x86_64 4:5.10.1-136.el6_6.1                                           
  127.   perl-version.x86_64 3:0.77-136.el6_6.1                                          
  128.   
  129. Complete!  
  130.  ---> 4ee0c1aacb0e  
  131. Removing intermediate container 35ab46137fe7  
  132. Successfully built 4ee0c1aacb0  

 

成功创建新的image

-t 定义image属性,属于 hongtu 用户,repository 为 centos, tag指定为 v2

. 指定在当前目录寻找 Dockerfile

 

[plain] view plain copy
 
 技术分享技术分享
  1. # docker images  
  2. REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE  
  3. hongtu/centos6      v2                  4ee0c1aacb0e        2 minutes ago       323.1 MB  
  4. centos6             hongtu              5f3bca7952ca        31 minutes ago      320.3 MB  
  5. centos              centos6             25c5298b1a36        2 weeks ago         215.8 MB  

 

可以看到新建的image id 为 4ee0c1aacb0e

向Docker hub上传 image

 

[plain] view plain copy
 
 技术分享技术分享
  1. # docker push hongtu/centos6  

删除本地image

 

[plain] view plain copy
 
 技术分享技术分享
  1. # docker rmi -f hongtu/centos6  

 

-f 强制删除

 

三、 Docker Image操作