设为首页 收藏本站
查看: 975|回复: 0

[经验分享] Docker安装与镜像管理

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-3-31 10:26:41 | 显示全部楼层 |阅读模式
一、安装docker

yum install epel-resase
yum install docker-io  (RHEL6)
yum install docker (RHEL7)

系统:
[iyunv@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

[iyunv@kvm ~]# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m

二、启动docker
1
2
3
4
5
6
[iyunv@kvm ~]# /etc/init.d/docker start
Starting docker:                                      [  OK  ]
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@kvm ~]# docker run -it centos /bin/bash
[iyunv@8c1fd812d079 /]#




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[iyunv@8c1fd812d079 /]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[iyunv@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

[iyunv@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
[iyunv@8c1fd812d079 /]# ps
   PID TTY          TIME CMD
     1 ?        00:00:00 bash
    16 ?        00:00:00 ps
[iyunv@8c1fd812d079 /]# uptime
11:55:21 up 42 min,  0 users,  load average: 0.00, 0.02, 0.01
[iyunv@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
[iyunv@8c1fd812d079 /]# exit





1
2
3
4
[iyunv@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
[iyunv@kvm ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              latest              bb3d629a7cbc        3 weeks ago         196.6 MB
[iyunv@kvm ~]# docker tag centos test-img
[iyunv@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
[iyunv@kvm ~]# docker tag test-img centos:test-img2
[iyunv@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
[iyunv@kvm ~]# docker rmi centos:test-img2
Untagged: centos:test-img2
[iyunv@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
[iyunv@kvm ~]# docker start 8c1fd
8c1fd
[iyunv@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
[iyunv@kvm ~]# docker exec -it 8c1fd /bin/bash
[iyunv@8c1fd812d079 /]#



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


[iyunv@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
[iyunv@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
[iyunv@kvm ~]# docker commit -m "centos_with_nettools_and_wget" -a "author" 8c1fd812d079 centos_new1
3e4be15719e03e909f39a7c03f3c7cd4fa6f2a8e282b2b88204d0224104b20e6
[iyunv@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


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-198148-1-1.html 上篇帖子: docker部署 下篇帖子: Docker:创建支持ssh服务的镜像
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表