免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 35197 | 回复: 0
打印 上一主题 下一主题

如何在centos上安装镜像容器,适合Docker新手 [复制链接]

论坛徽章:
18
IT运维版块每日发帖之星
日期:2016-06-07 06:20:0015-16赛季CBA联赛之北控
日期:2016-06-30 21:19:06IT运维版块每日发帖之星
日期:2016-06-29 06:20:00每日论坛发贴之星
日期:2016-06-28 06:20:00IT运维版块每日发帖之星
日期:2016-06-28 06:20:00数据库技术版块每日发帖之星
日期:2016-06-23 06:20:00每日论坛发贴之星
日期:2016-06-22 06:20:00IT运维版块每日发帖之星
日期:2016-06-22 06:20:00IT运维版块每日发帖之星
日期:2016-06-21 06:20:00wusuopu
日期:2016-06-17 17:43:45IT运维版块每日发帖之星
日期:2016-06-16 06:20:00IT运维版块每日发帖之星
日期:2016-06-15 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-07-04 09:10 |只看该作者 |倒序浏览
学习docker前,理解以下几个概念有助于更好的使用docker。
镜像,容器,仓库。
镜像,就是一个操作系统环境,里面只有你需要的几个应用程序,如apache,mysql,php之类,只读模板。
容器,从镜像创建的运行实例。可视为一个简易环境中和其中运行的应用。
仓库,存放镜像的地方。学过git的同学可能更容易理解。

一、安装docker时,增加第三方源epel
如果是centos7,下载并安装这个软件包
#wget http://mirror.hust.edu.cn/epel/b ... se-7-0.2.noarch.rpm
#rpm -ivh epel-release-7-0.2.noarch.rpm
如果是centos6,下载并安装这个软件包
#wget http://mirrors.hustunique.com/ep ... ease-6-8.noarch.rpm    32位
#rpm -Uvh http://mirrors.sohu.com/fedora-e ... ease-6-8.noarch.rpm 64位
#rpm -ivh epel-release-6-8.noarch.rpm
二、安装docker
#yum install docker-io
如果之前的系统中存在docker这个软件,最好先删除掉这个包,一个老旧的包
#service docker start
#chkconfig docker on
[iyunv@localhost master]# docker -v
Docker version 1.5.0, build a8a31ef/1.5.0
三、镜像相关操作
3.1列出本地所有的镜像,显示出的依次是:镜像所属仓库  标签名 镜像id 创建日期 所占空间大小
[iyunv@localhost master]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              7                   fd44297e2ddb        6 weeks ago         215.7 MB
centos              centos7             fd44297e2ddb        6 weeks ago         215.7 MB
centos              latest              fd44297e2ddb        6 weeks ago         215.7 MB
四、容器相关操作
4.1查看容器
[iyunv@localhost master]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d4304fbe22a8        centos:7            "/bin/bash"         18 minutes ago      Up 17 minutes                           goofy_euclid
4.2 新建、查看容器
//新建,使用docker create命令新建一个docker容器,该命令新建的容器处于停止的状态
[iyunv@localhost master]# docker create -i -t centos:centos7 /bin/bash
72096e2a30802ce5b5e504cbc904e241da3fdb7ee11b8eef989e362b8953db2c
//查看刚创建的容器72096e2a3080
[iyunv@localhost master]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
72096e2a3080        centos:7            "/bin/bash"         5 seconds ago                                               elated_franklin     
d4304fbe22a8        centos:7            "/bin/bash"         19 minutes ago      Up 18 minutes                           goofy_euclid
4.3 启动新建的容器
//启动
[iyunv@localhost master]# docker start 72096e2a3080
72096e2a3080
//查看容器的状态
[iyunv@localhost master]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
72096e2a3080        centos:7            "/bin/bash"         4 minutes ago       Up 2 seconds                            elated_franklin     
d4304fbe22a8        centos:7            "/bin/bash"         23 minutes ago      Up 23 minutes                           goofy_euclid
4.4 进入新建的容器
[iyunv@localhost master]# docker exec -ti d4304fbe22a8 /bin/bash
[iyunv@d4304fbe22a8 /]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
4.5 退出容器,使用命令exit或ctrl+d,用exit后不关闭容器 而是在后台继续运行
[iyunv@d4304fbe22a8 /]# exit
exit
[iyunv@localhost master]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
72096e2a3080        centos:7            "/bin/bash"         4 minutes ago       Up 2 seconds                            elated_franklin     
d4304fbe22a8        centos:7            "/bin/bash"         23 minutes ago      Up 23 minutes                           goofy_euclid
4.6 停止新建的容器
//停止
[iyunv@localhost master]# docker stop 72096e2a3080
72096e2a3080
//重新启动
[iyunv@localhost master]# docker restart 72096e2a3080
//查看
[iyunv@localhost master]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                        PORTS               NAMES
72096e2a3080        centos:7            "/bin/bash"         35 minutes ago      Exited (137) 43 seconds ago                       elated_franklin     
d4304fbe22a8        centos:7            "/bin/bash"         55 minutes ago      Exited (0) 7 minutes ago                          goofy_euclid        
[iyunv@localhost master]#
4.7创建守护式容器
守护式容器:没有交互式会话,非常适合运行应用程序和服务。
//更多的时候,需要让docker容器在后台以守护态形式运行。用户可以通过添加-d参数来添加,
[iyunv@localhost master]# docker run --name daemon -d centos:centos7 /bin/bash  -c "while true;do echo hello world;sleep 1; done"
07369b21b6c0130b12af0ca2c689e5cd08181422475457d3e17519565159c818
[iyunv@localhost master]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                        PORTS               NAMES
07369b21b6c0        centos:7            "/bin/bash -c 'while   7 seconds ago       Up 5 seconds                                      daemon
//此时用docker run 只会返回一个容器id,必须用docker attach才能附着到新的shell会话,附着到容器会话,显示他一直在打印hello world
[iyunv@localhost master]# docker attach 07369b21b6c0
hello world
hello world
hello world
4.8停止守护式容器
[iyunv@localhost master]# docker stop 07369b21b6c0
4.9在容器内运行进程
我们可以通过docker exec命令在容器内部额外启动新进程。可以在容器内创建的进程有两种类型:后台任务和交互式任务
//后台任务在容器内运行,且没有交互需求,交互式任务则保持在前台运行。
[iyunv@localhost master]#docker exec -d daemon touch /tmp/test
-d: 表明需要运行一个后台进程。
//验证
[iyunv@localhost master]#docker exec -t -i daemon /bin/bash
root@2d39e9e78ae0:/# cd /tmp/
root@2d39e9e78ae0:/tmp# ls
test
4.10删除停止状态的容器
[iyunv@localhost master]# docker rm d4304fbe22a8
d4304fbe22a8
[iyunv@localhost master]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                           PORTS               NAMES
719f071a730d        centos:7            "/bin/bash -c 'while   46 minutes ago      Exited (0) 2 minutes ago                             daemon              
c43565344cb7        centos:7            "/bin/echo hello"      53 minutes ago      Exited (0) 53 minutes ago                            prickly_galileo     
07369b21b6c0        centos:7            "/bin/bash -c 'while   About an hour ago   Exited (137) 47 minutes ago                          clever_torvalds     
3cd2fa08c942        centos:7            "/bin/bash"            About an hour ago   Exited (0) 26 minutes ago                            ecstatic_pare      
b8db23a1c3e6        centos:7            "/bin/bash"            About an hour ago   Exited (0) About an hour ago                         happy_jones         
72096e2a3080        centos:7            "/bin/bash"            2 hours ago         Exited (137) About an hour ago                       elated_franklin
4.11删除运行中的容器
[iyunv@localhost master]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                         PORTS               NAMES
719f071a730d        centos:7            "/bin/bash -c 'while   55 minutes ago      Exited (0) 12 minutes ago                          daemon              
c43565344cb7        centos:7            "/bin/echo hello"      About an hour ago   Exited (0) 8 minutes ago                           prickly_galileo     
07369b21b6c0        centos:7            "/bin/bash -c 'while   About an hour ago   Exited (137) 56 minutes ago                        clever_torvalds     
3cd2fa08c942        centos:7            "/bin/bash"            About an hour ago   Exited (0) 36 minutes ago                          ecstatic_pare      
b8db23a1c3e6        centos:7            "/bin/bash"            About an hour ago   Exited (0) About an hour ago                       happy_jones         
72096e2a3080        centos:7            "/bin/bash"            2 hours ago         Up 12 seconds                                      elated_franklin     
[iyunv@localhost master]# docker rm -f 72096e2a3080
72096e2a3080
[iyunv@localhost master]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                           PORTS               NAMES
719f071a730d        centos:7            "/bin/bash -c 'while   About an hour ago   Exited (0) 16 minutes ago                            daemon              
c43565344cb7        centos:7            "/bin/echo hello"      About an hour ago   Exited (0) 13 minutes ago                            prickly_galileo     
07369b21b6c0        centos:7            "/bin/bash -c 'while   About an hour ago   Exited (137) About an hour ago                       clever_torvalds     
3cd2fa08c942        centos:7            "/bin/bash"            About an hour ago   Exited (0) 40 minutes ago                            ecstatic_pare      
b8db23a1c3e6        centos:7            "/bin/bash"            About an hour ago   Exited (0) About an hour ago                         happy_jones
4.12导出和导入容器
//docker export命令导出
[iyunv@localhost master]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                           PORTS               NAMES
719f071a730d        centos:7            "/bin/bash -c 'while   About an hour ago   Exited (0) 16 minutes ago                            daemon              
c43565344cb7        centos:7            "/bin/echo hello"      About an hour ago   Exited (0) 13 minutes ago                            prickly_galileo     
07369b21b6c0        centos:7            "/bin/bash -c 'while   About an hour ago   Exited (137) About an hour ago                       clever_torvalds     
3cd2fa08c942        centos:7            "/bin/bash"            About an hour ago   Exited (0) 40 minutes ago                            ecstatic_pare      
b8db23a1c3e6        centos:7            "/bin/bash"            About an hour ago   Exited (0) About an hour ago                         happy_jones         
[iyunv@localhost master]# docker export b8db23a1c3e6 > test.tar
[iyunv@localhost master]# ls
rc.tomcat.tar.gz  software  test.tar  ttp_admin-150525.tar.gz  www.ttpai.cn-150525.tar.gz
//docker import命令导入,成为镜像
[iyunv@localhost master]# cat test.tar | docker import - test/centos:v1
fc9df930147ad57f2ea516748375864a612dc6fe645f0e32f5992ac19088adf6
[iyunv@localhost master]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
test/centos         v1                  fc9df930147a        15 seconds ago      215.7 MB
centos              7                   fd44297e2ddb        6 weeks ago         215.7 MB
centos              centos7             fd44297e2ddb        6 weeks ago         215.7 MB
centos              latest              fd44297e2ddb        6 weeks ago         215.7 MB
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP