Docker安装与镜像管理(一)

一、安装docker

yum install epel-resase

yum install docker-io  (RHEL6)

yum install docker (RHEL7)


系统:

[root@kvm ~]# uname -a

Linux kvm.huangming.org 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux


[root@kvm ~]# cat /etc/issue

CentOS release 6.7 (Final)

Kernel \r on an \m


二、启动docker

1
2
3
4
5
6
[root@kvm ~] # /etc/init.d/docker start
Starting docker:                                      [  OK  ]
[root@kvm ~] # ps -ef |grep docker
root       3900      1  1 19:28 pts /0     00:00:00  /usr/bin/docker  -d
root       3927   3900 34 19:28 pts /0     00:00:04 mkfs.ext4 -E nodiscard,lazy_itable_init=0  /dev/mapper/docker-8 :3-1573967-base
root       3938   3606  0 19:28 pts /0     00:00:00  grep  docker


三、镜像管理

  • docker pull centos

    #从docker.com获取centos镜像

  • docker images

    #查看本地镜像

  • docker tag centos test-img 

    #为centos镜像设置标签为test-img,在使用docker image查看时会多出一行,更改的image id和centos的一样

  • docker search [image-name]

    #搜索docker仓库里的docker镜像

  • docker run -t -i centos /bin/bash

    #用下载到的镜像开启容器,-i表示让容器的标准输入打开,-t表示分配一个伪终端,要把-i -t放到镜像名字的前面。当镜像发生修改后,我们可以把该镜像提交重新生成一个新版本运行在本地

  • docker ps

    #查看运行的容器,加-a选项可以查看没有运行的容器

  • docker rmi centos

    #用来删除指定的镜像,其中后面的参数可以是tag,如果是tag时,实际上是删除该tag,只要该镜像还有其他的tag,就不会删除该镜像。当后面的参数为ID时,则会彻底删除整个镜像,连同所有的标签。


1、下载centos镜像

1
2
3
4
5
6
7
8
[root@kvm ~] # docker pull centos
latest: Pulling from centos
 
47d44cb6f252: Already exists 
6fdebd7b0eb5: Already exists 
a63aae4d216f: Already exists 
Digest: sha256:381f21e4c7b3724c6f420b2bcfa6e13e47ed155192869a2a04fa10f944c78476
Status: Image is up to  date  for  centos:latest

2、查看下载到本地镜像

1
2
3
[root@kvm ~] # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              bb3d629a7cbc        3 weeks ago         196.6 MB

3、查看docker仓库镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@kvm ~] # docker search centos
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                          The official build of CentOS.                   2069      [OK]       
jdeathe /centos-ssh               CentOS-6 6.7 x86_64 / CentOS-7 7.2.1511 x8...   18                   [OK]
jdeathe /centos-ssh-apache-php    CentOS-6 6.7 x86_64 / Apache / PHP / PHP M...   14                   [OK]
million12 /centos-supervisor      Base CentOS-7 with supervisord launcher, h...   10                   [OK]
blalor /centos                    Bare-bones base CentOS 6.5 image                8                    [OK]
nimmis /java-centos               This is docker images of CentOS 7 with dif...   8                    [OK]
torusware /speedus-centos         Always updated official CentOS docker imag...   7                    [OK]
centos /mariadb55-centos7                                                         3                    [OK]
nathonfowlie /centos-jre          Latest CentOS image with the JRE pre-insta...   3                    [OK]
nickistre /centos-lamp            LAMP on centos setup                            3                    [OK]
consol /sakuli-centos-xfce        Sakuli end-2-end testing and monitoring co...   2                    [OK]
softvisio /centos                 Centos                                          1                    [OK]
layerworx /centos                 CentOS container with etcd, etcdctl, confd...   1                    [OK]
yajo /centos-epel                 CentOS with EPEL and fully updated              1                    [OK]
lighthopper /orientdb-centos      A Dockerfile  for  creating an OrientDB imag...   1                    [OK]
darksheer /centos                 Base Centos Image -- Updated hourly             1                    [OK]
timhughes /centos                 Centos with systemd installed and running       1                    [OK]
januswel /centos                  yum update-ed CentOS image                      0                    [OK]
drokar /centos-s6                 centOS combined with s6, a small process s...   0                    [OK]
lighthopper /openjdk-centos       A Dockerfile  for  creating an OpenJDK image...   0                    [OK]
ustclug /centos                    USTC centos                                    0                    [OK]
blacklabelops /centos             CentOS Base Image! Built and Updates Daily!     0                    [OK]
grayzone /centos                  auto build  for  centos.                          0                    [OK]
jsmigel /centos-epel              Docker base image of CentOS w/ EPEL installed   0                    [OK]
grossws /centos                   CentOS 6 and 7 base images with gosu and l...   0                    [OK]


4、开启一个容器docker run -it centos /bin/bash

1
2
[root@kvm ~] # docker run -it centos /bin/bash
[root@8c1fd812d079 /] #


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@8c1fd812d079 /] # cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
[root@8c1fd812d079 /] # uname -a                
Linux 8c1fd812d079 2.6.32-573.el6.x86_64  #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
 
[root@8c1fd812d079 /] # w
  11:55:12 up 42 min,  0  users ,  load average: 0.00, 0.03, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
[root@8c1fd812d079 /] # ps
    PID TTY          TIME CMD
      1 ?        00:00:00  bash
     16 ?        00:00:00  ps
[root@8c1fd812d079 /] # uptime
  11:55:21 up 42 min,  0  users ,  load average: 0.00, 0.02, 0.01
[root@8c1fd812d079 /] # df -Th
Filesystem                                                                                      Type   Size  Used Avail Use% Mounted on
/dev/mapper/docker-8 :3-1573967-8c1fd812d0790d1868bcec389a50c6f7a08c6c099167141393af0961e7b850be ext4   9.8G  230M  9.0G   3% /
tmpfs                                                                                           tmpfs  1.9G     0  1.9G   0%  /dev
shm                                                                                             tmpfs   64M     0   64M   0%  /dev/shm
/dev/sda3                                                                                        ext4    43G  4.6G   36G  12%  /etc/hosts
[root@8c1fd812d079 /] # exit


1
2
3
4
[root@kvm ~] # docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
f732106147eb        centos               "/bin/bash"          17 seconds ago      Up 15 seconds                                       high_heisenberg     
8c1fd812d079        centos               "/bin/bash"          3 minutes ago       Exited (0) About a minute ago                       grave_mcclintock


5、修改镜像名称

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@kvm ~] # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              bb3d629a7cbc        3 weeks ago         196.6 MB
[root@kvm ~] # docker tag centos test-img
[root@kvm ~] # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              bb3d629a7cbc        3 weeks ago         196.6 MB
test -img            latest              bb3d629a7cbc        3 weeks ago         196.6 MB
[root@kvm ~] # docker tag test-img centos:test-img2
[root@kvm ~] # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
test -img            latest              bb3d629a7cbc        3 weeks ago         196.6 MB
centos              latest              bb3d629a7cbc        3 weeks ago         196.6 MB
centos               test -img2           bb3d629a7cbc        3 weeks ago         196.6 MB


6、删除镜像

1
2
3
4
5
6
[root@kvm ~] # docker rmi centos:test-img2
Untagged: centos: test -img2
[root@kvm ~] # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              bb3d629a7cbc        3 weeks ago         196.6 MB
test -img            latest              bb3d629a7cbc        3 weeks ago         196.6 MB


四、docker镜像使用容器生成新的镜像

1、启动镜像

1
2
3
4
5
6
[root@kvm ~] # docker start 8c1fd
8c1fd
[root@kvm ~] # docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
f732106147eb        centos               "/bin/bash"          12 minutes ago      Exited (0) 43 seconds ago                       high_heisenberg     
8c1fd812d079        centos               "/bin/bash"          15 minutes ago      Up 3 seconds

2、进入已启动的容器docker exec -it 【镜像ID,可以简写】 /bin/bash

1
2
[root@kvm ~] # docker exec -it 8c1fd /bin/bash
[root@8c1fd812d079 /] #

3、在容器中安装软件(安装网卡包net-tools)

[root@8c1fd812d079 /]# yum install net-tools wget -y

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@8c1fd812d079 /] # ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
         inet 172.17.0.3  netmask 255.255.0.0  broadcast 0.0.0.0
         inet6 fe80::42:acff:fe11:3  prefixlen 64  scopeid 0x20<link>
         ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
         RX packets 5419  bytes 10411538 (9.9 MiB)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 4127  bytes 268377 (262.0 KiB)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
         inet 127.0.0.1  netmask 255.0.0.0
         inet6 ::1  prefixlen 128  scopeid 0x10<host>
         loop  txqueuelen 0  (Local Loopback)
         RX packets 0  bytes 0 (0.0 B)
         RX errors 0  dropped 0  overruns 0  frame 0
         TX packets 0  bytes 0 (0.0 B)
         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

4、生成新的镜像

  • docker commit -m "change somth" -a "somebody info" container_id new-image

例如:docker commit -m "install httpd" -a "test-image1" 8c1fd812d079 test/centos


1
2
3
4
5
6
7
[root@kvm ~] # docker commit -m "centos_with_nettools_and_wget" -a "author" 8c1fd812d079 centos_new1
3e4be15719e03e909f39a7c03f3c7cd4fa6f2a8e282b2b88204d0224104b20e6
[root@kvm ~] # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos_new1         latest              3e4be15719e0        16 seconds ago      268 MB
centos              latest              bb3d629a7cbc        3 weeks ago         196.6 MB
test -img            latest              bb3d629a7cbc        3 weeks ago         196.6 MB


五、docker基于本地模版导入创建镜像

1、下载地址https://download.openvz.org/template/precreated/

2、下载一个镜像模版


  • 导入镜像

    #cat centos-6-x86_64-minimal.tar.gz | docker import - centos-6-x86_64

  • 导出镜像

    #docker save -o new-centos.tar new-image/centos

  • 恢复本地镜像

    #docker load --input new-centos.tar 或者 docker load < new-centos.tar

  • 上传镜像到dockerhub官网,前提需要注册一个用户

    #docker push image-name


3、导入镜像

1
2
[root@kvm src] # cat centos-6-x86_64-minimal.tar.gz | docker import - centos-6-x86_64
1014358cbf3b643cae13e830afc539cbf0e02aa40f6c77583898a0cbd33a9561
  • 查看镜像

1
2
3
4
5
6
[root@kvm src] # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
centos-6-x86_64     latest              1014358cbf3b        About a minute ago   326.4 MB
centos_new1         latest              3e4be15719e0        24 hours ago         268 MB
test -img            latest              bb3d629a7cbc        3 weeks ago          196.6 MB
centos              latest              bb3d629a7cbc        3 weeks ago          196.6 MB

4、导出镜像

1
2
3
[root@kvm src] # docker save -o centos_net_01.tar bb3d629a7cbc
[root@kvm src] # du -sh centos_net_01.tar 
195M    centos_net_01. tar

5、强制删除镜像

1
2
3
[root@kvm src] # docker rmi -f bb3d629a7cbc
Untagged: centos:latest
Untagged:  test -img:latest

6、恢复镜像

1
2
3
4
5
6
7
8
9
10
11
[root@kvm src] # docker load < centos_net_img2.tar 
[root@kvm src] # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos-6-x86_64     latest              1014358cbf3b        18 minutes ago      326.4 MB
<none>              <none>              3e4be15719e0        24 hours ago        268 MB
 
[root@kvm src] # docker tag 3e4be15719e0 centos_net_img:latest
[root@kvm src] # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos-6-x86_64     latest              1014358cbf3b        21 minutes ago      326.4 MB
centos_net_img      latest              3e4be15719e0        24 hours ago        268 MB




本文转自 HMLinux 51CTO博客,原文链接:http://blog.51cto.com/7424593/1758611
优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/560299

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Mario,低调大师唯一一个Java游戏作品

Mario,低调大师唯一一个Java游戏作品

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。