bmwm3 发表于 2015-10-13 07:22:10

CentOS7 安装 使用 Docker

CentOS 7 中 Docker 的安装
# yum install docker
# systemctlstart docker
  # systemctlenable 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




# docker images centos
REPOSITORY          TAG               IMAGE ID            CREATED             VIRTUAL SIZE
docker.io/centos    latest            7322fbe74aa5      9 weeks ago         172.2 MB


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

  运行一个ubuntu Docker 容器:
# docker run -i -t ubuntu /bin/bash


使用第一个容器:
# cat /etc/redhat-release
  CentOS Linux release 7.1.1503 (Core)
  

  # hostname
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
  
# 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
  
# ps -aux
USER      PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          10.00.1117441912 ?      Ss   11:05   0:00 /bin/bash
root         790.00.0197641244 ?      R&#43;   13:24   0:00 ps -aux

  

  退出容器
# exit
exit
#


显示所有容器的列表(停止或运行的)
# 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   
  显示最后一个容器(运行或停止的)
# 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   
  显示当前正在运行容器的列表

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

  容器命名:
  # docker run --name phpapp_comtainer -i -t centos /bin/bash

  重启启动已停止的容器:
  # docker start phpapp_comtainer   容器名或者重启ID
  phpapp_comtainer
# 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:
  # docker attach e94f6675975f
  回车
  exit
  

  查看日志:
  # docker logs e94f6675975f

  监控日志:
  # docker logs -f e94f6675975f

  查看最后10条:
  # docker logs --tail 10 45020b088fcd

  查看最新日志:
  # docker logs --tail 0 -f 45020b088fcd

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


  深入容器:
  # 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;,

  。。。。
  删除容器:
  # docker rm 94ab5a5ac359
94ab5a5ac359

  # docker rm `docker ps -a -q` 删除所有容器

  

  使用docker的镜相和仓库
  拉取镜像:
  # 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
  镜像列表:
# 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
  使用镜像:
# 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.
  # cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)
  创建守护进程式容器:
  # docker run -i -t --name daemon_dave -d centos:7.0.1406 /bin/sh
f6d9dcc6f9dc4ba480dcc108d74be631fbc20e10e90fc6a50434a84cb528ef83

  

  构建镜像:
  commit方式
  到https://hub.docker.com/注册帐号
  # 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呢
  # docker login
Username: jane1989
Password:
Email: 363394@qq.com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
# 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;
                }
      }
}#

  

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

  在容器里装web服务:
  # yum install -y epel-release

  # yum install -y nginx

  然后退出提交:
  # exit
exit
# 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      
# docker commit -m=&quot;A new custom image&quot; --author=&quot;Jane&quot; cde154b23ed2 jane1989/nginx:V1
bb7080648d9a998f2e4e06d45aff741e7f8d177037302d430b448ab1ecd918fd

  使用我们刚建的镜像:
  # 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

  # docker run -t -i jane1989/nginx:V1 /bin/bash

  推送镜像:
  # docker commit -m=&quot;A new custom image&quot; --author=&quot;Jane&quot; cde154b23ed2 jane1989/nginx:V1^C
# 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)
# docker push jane1989/nginx:V1

  

  Dockerfile方式
  vi Dockerfile
  FROM mysql:5.6

  docker build -t eva/mysql ./mysql

  

  删除镜像:
  # docker rmi jane1989/nginx:V1

  批量删除:# 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
  生成新镜像
  # docker commit -m &quot;test image&quot; --author=&quot;jane&quot; 3f2e8e42d52f jane1989/nginx:V2

  # 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

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

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

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

  # /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]
查看完整版本: CentOS7 安装 使用 Docker