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

[经验分享] CentOS7 安装 使用 Docker

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-10-13 07:22:10 | 显示全部楼层 |阅读模式
CentOS 7 中 Docker 的安装
[iyunv@localhost ~]# yum install docker
[iyunv@localhost ~]# systemctl  start docker
  [iyunv@localhost ~]# systemctl  enable docker
  

  docker很多官方源:http://www.open-open.com/lib/view/open1435671611966.html#_label4
  但使用起来很多限制:系统版本,命令,


下载官方的 CentOS 镜像到本地
docker pull centos
Pulling repository centos
192178b11d36: Download complete
70441cac1ed5: Download complete
ae0c2d0bdc10: Download complete
511136ea3c5a: Download complete
5b12ef8fd570: Download complete




[iyunv@localhost ~]# docker images centos
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker.io/centos    latest              7322fbe74aa5        9 weeks ago         172.2 MB


运行一个centos  Docker 容器:
[iyunv@localhost ~]# docker run -i -t centos /bin/bash
[iyunv@dbf66395436d /]#
  我们可以看到,CentOS 容器已经被启动,并且我们得到了 bash 提示符。在 docker 命令中我们使用了 “-i 捕获标准输入输出”和 “-t 分配一个终端或控制台”选项。若要断开与容器的连接,输入 exit。
  

  运行一个ubuntu Docker 容器:
[iyunv@localhost ~]# docker run -i -t ubuntu /bin/bash


使用第一个容器:
[iyunv@45020b088fcd /]# cat /etc/redhat-release
  CentOS Linux release 7.1.1503 (Core)
  

  [iyunv@45020b088fcd /]# hostname
45020b088fcd
  
[iyunv@45020b088fcd /]# cat /etc/hosts
172.17.0.1      45020b088fcd
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
  
[iyunv@45020b088fcd /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
4: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:11:00:01 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:1/64 scope link
       valid_lft forever preferred_lft forever
  
[iyunv@45020b088fcd /]# ps -aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.1  11744  1912 ?        Ss   11:05   0:00 /bin/bash
root         79  0.0  0.0  19764  1244 ?        R&#43;   13:24   0:00 ps -aux

  

  退出容器
[iyunv@cd05639b3f5c /]# exit
exit
[iyunv@localhost ~]#


显示所有容器的列表(停止或运行的)
[iyunv@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
45020b088fcd        centos              &quot;/bin/bash&quot;         19 minutes ago      Up 19 minutes                                 nostalgic_ptolemy   
1a172adbc4ae        centos              &quot;/bin/bash&quot;         2 days ago          Exited (0) 2 days ago                         cocky_brattain      
e7a9a701b2ae        ubuntu              &quot;/bin/bash&quot;         2 days ago          Exited (0) 2 days ago                         thirsty_hopper      
a691456feccd        ubuntu              &quot;/bin/bash&quot;         2 days ago          Exited (0) 2 days ago                         cranky_noyce        
2e795b2ea988        ubuntu              &quot;/bin/bash&quot;         2 days ago          Exited (0) 2 days ago                         thirsty_elion      
94ab5a5ac359        centos              &quot;/bin/bash&quot;         2 days ago          Exited (137) 2 days ago                       serene_babbage      
  6e485094bfdb        centos              &quot;/bin/bash&quot;         2 days ago          Exited (0) 2 days ago                         happy_davinci   
  显示最后一个容器(运行或停止的)
[iyunv@localhost ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  45020b088fcd        centos              &quot;/bin/bash&quot;         20 minutes ago      Up 20 minutes                           nostalgic_ptolemy   
  显示当前正在运行容器的列表

[iyunv@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
45020b088fcd        centos              &quot;/bin/bash&quot;         20 minutes ago      Up 20 minutes                           nostalgic_ptolemy   
  

  容器命名:
  [iyunv@localhost ~]# docker run --name phpapp_comtainer -i -t centos /bin/bash

  重启启动已停止的容器:
  [iyunv@localhost ~]# docker start phpapp_comtainer   容器名或者重启ID
  phpapp_comtainer
[iyunv@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
e94f6675975f        centos              &quot;/bin/bash&quot;         16 minutes ago      Up 8 seconds                            phpapp_comtainer   
45020b088fcd        centos              &quot;/bin/bash&quot;         2 hours ago         Up 2 hours                              nostalgic_ptolemy   
  进入shell:
  [iyunv@localhost ~]# docker attach e94f6675975f
  回车
  exit
  

  查看日志:
  [iyunv@localhost ~]# docker logs e94f6675975f

  监控日志:
  [iyunv@localhost ~]# docker logs -f e94f6675975f

  查看最后10条:
  [iyunv@localhost ~]# docker logs --tail 10 45020b088fcd

  查看最新日志:
  [iyunv@localhost ~]# docker logs --tail 0 -f 45020b088fcd

  查看容器内的进程:
  [iyunv@localhost ~]# docker top 45020b088fcd
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                2823                1380                0                   19:05               pts/1               00:00:00            /bin/bash


  深入容器:
  [iyunv@localhost ~]# docker inspect 45020b088fcd
[
{
    &quot;Id&quot;: &quot;45020b088fcd383b43f223d481958f63b17a5c4f781c0ce0f571f0579f1acb72&quot;,
    &quot;Created&quot;: &quot;2015-08-24T11:05:01.708076873Z&quot;,
    &quot;Path&quot;: &quot;/bin/bash&quot;,
    &quot;Args&quot;: [],
    &quot;State&quot;: {
        &quot;Running&quot;: true,
        &quot;Paused&quot;: false,
        &quot;Restarting&quot;: false,
        &quot;OOMKilled&quot;: false,
        &quot;Dead&quot;: false,
        &quot;Pid&quot;: 2823,
        &quot;ExitCode&quot;: 0,
        &quot;Error&quot;: &quot;&quot;,
        &quot;StartedAt&quot;: &quot;2015-08-24T11:05:02.631860391Z&quot;,
        &quot;FinishedAt&quot;: &quot;0001-01-01T00:00:00Z&quot;
    },
    &quot;Image&quot;: &quot;7322fbe74aa5632b33a400959867c8ac4290e9c5112877a7754be70cfe5d66e9&quot;,
    &quot;NetworkSettings&quot;: {
        &quot;Bridge&quot;: &quot;&quot;,

  。。。。
  删除容器:
  [iyunv@localhost ~]# docker rm 94ab5a5ac359
94ab5a5ac359

  [iyunv@localhost ~]# docker rm `docker ps -a -q` 删除所有容器

  

  使用docker的镜相和仓库
  拉取镜像:
  [iyunv@localhost ~]# docker pull centos:7.0.1406 (指定版本号拉取)
7.0.1406: Pulling from docker.io/centos
feb2761601e7: Pull complete
f1b10cd84249: Already exists
Digest: sha256:c7b891ec033c2cd69b09f1e62c9749302961f941145802527afebaad3ed1e0d2
Status: Downloaded newer image for docker.io/centos:7.0.1406
  镜像列表:
[iyunv@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker.io/ubuntu    latest              91e54dfb1179        3 days ago          188.3 MB
docker.io/centos    latest              7322fbe74aa5        9 weeks ago         172.2 MB
docker.io/centos    7.0.1406            feb2761601e7        4 months ago        210.2 MB
  使用镜像:
[iyunv@localhost ~]# docker run -i -t --name phpapp_container centos:7.0.1406 /bin/bash   (不带版本号会使用latest版本)
  Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.
  [iyunv@68061e198dd5 /]# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
  创建守护进程式容器:
  [iyunv@localhost ~]# docker run -i -t --name daemon_dave -d centos:7.0.1406 /bin/sh
f6d9dcc6f9dc4ba480dcc108d74be631fbc20e10e90fc6a50434a84cb528ef83

  

  构建镜像:
  commit方式
  到https://hub.docker.com/注册帐号
  [iyunv@localhost static_web]# docker login
Username: jane1989
Password:
Email: 363394@qq.com
Error response from daemon: Login: Account is not Active. Please check your e-mail for a confirmation link.
快去邮箱验证一下就ok了,不过有时会time out,谁让不在china呢
  [iyunv@localhost static_web]# docker login
Username: jane1989
Password:
Email: 363394@qq.com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[iyunv@localhost static_web]# cat /root/.docker/config.json
{
        &quot;auths&quot;: {
                &quot;https://index.docker.io/v1/&quot;: {
                        &quot;auth&quot;: &quot;amFuZTE5ODk6MTIzNDU2&quot;,
                        &quot;email&quot;: &quot;363394@qq.com&quot;
                }
        }
}[iyunv@localhost static_web]#

  

  用docker 的commit命令创建镜像:每次提交容器与当前差异部分,比较轻量
  [iyunv@localhost static_web]# docker run -i -t centos /bin/bash

  在容器里装web服务:
  [iyunv@cde154b23ed2 /]# yum install -y epel-release

  [iyunv@cde154b23ed2 /]# yum install -y nginx

  然后退出提交:
  [iyunv@cde154b23ed2 conf.d]# exit
exit
[iyunv@localhost static_web]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
cde154b23ed2        centos              &quot;/bin/bash&quot;         17 minutes ago      Exited (130) 6 seconds ago                       tender_goodall      
[iyunv@localhost static_web]# docker commit -m=&quot;A new custom image&quot; --author=&quot;Jane&quot; cde154b23ed2 jane1989/nginx:V1
bb7080648d9a998f2e4e06d45aff741e7f8d177037302d430b448ab1ecd918fd

  使用我们刚建的镜像:
  [iyunv@localhost static_web]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
jane1989/nginx      V1                  bb7080648d9a        About a minute ago   313.3 MB
jane/static_web     v1                  56d3b8dd0faa        56 minutes ago       210.2 MB
docker.io/ubuntu    latest              91e54dfb1179        4 days ago           188.3 MB
docker.io/centos    latest              7322fbe74aa5        9 weeks ago          172.2 MB
docker.io/centos    7.0.1406            feb2761601e7        4 months ago         210.2 MB

  [iyunv@localhost static_web]# docker run -t -i jane1989/nginx:V1 /bin/bash

  推送镜像:
  [iyunv@localhost ~]# docker commit -m=&quot;A new custom image&quot; --author=&quot;Jane&quot; cde154b23ed2 jane1989/nginx:V1^C
[iyunv@localhost ~]# docker push nginx:V1(报错:带上用户名,因为默认是root 帐号,而那是官方帐号)
Error response from daemon: You cannot push a &quot;root&quot; repository. Please rename your repository to <user>/<repo> (ex: jane1989/nginx)
[iyunv@localhost ~]# docker push jane1989/nginx:V1

  

  Dockerfile方式
  vi Dockerfile
  FROM mysql:5.6

  docker build -t eva/mysql ./mysql

  

  删除镜像:
  [iyunv@localhost ~]# docker rmi jane1989/nginx:V1

  批量删除:[iyunv@localhost ~]# docker rmi `docker images -a -q`
  

  打包镜像:
  运行容器:docker run -t -i -d -p 50001:22 -p 80:80  --name=nginx6 jane1989/nginx:V1 /bin/bash

  exit
  生成新镜像
  [iyunv@localhost ~]# docker commit -m &quot;test image&quot; --author=&quot;jane&quot; 3f2e8e42d52f jane1989/nginx:V2

  [iyunv@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
jane1989/nginx      V2                  fb1f2c3c59a4        6 minutes ago       353 MB
jane1989/nginx      V1                  bb7080648d9a        23 hours ago        313.3 MB

  打包
  [iyunv@localhost ~]# docker save fb1f2c3c59a4 >/root/nginx:V2.tar
[iyunv@localhost ~]# ll /root/nginx\:V2.tar
-rw-r--r--. 1 root root 363127296 8?  26 23:01 /root/nginx:V2.tar

  在另外的机器上导入镜像(如果是测试,可以把当前机器的镜像删除,然后导入)
  [iyunv@localhost ~]# docker load < /root/nginx\:V2.tar
[iyunv@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              fb1f2c3c59a4        14 minutes ago      353 MB

  给镜像重新打上标签
  [iyunv@localhost ~]# docker tag fb1f2c3c59a4 jane1989/nginx:V2
[iyunv@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
jane1989/nginx      V2                  fb1f2c3c59a4        17 minutes ago      353 MB
[iyunv@localhost ~]# docker run -t -i -p 80:80 --name nginx1 jane1989/nginx:V2

  [iyunv@cbdb546b7454 /]# /usr/sbin/nginx

  

  使用官方镜像:
  安装pip
  wget --no-check-certificate https://github.com/pypa/pip/archive/1.5.5.tar.gz
注意:wget获取https的时候要加上:--no-check-certificate
tar zvxf 1.5.5.tar.gz    #解压文件
cd pip-1.5.5/
python setup.py install

  



         版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 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-126002-1-1.html 上篇帖子: Docker入门 下篇帖子: Docker体验 Ubuntu下安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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