CentOS7 安装 使用 Docker

CentOS 7 中 Docker 的安装
[root@localhost ~]# yum install docker
[root@localhost ~]# systemctl  start docker

[root@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




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


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

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


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


使用第一个容器:
[root@45020b088fcd /]# cat /etc/redhat-release 

CentOS Linux release 7.1.1503 (Core) 


[root@45020b088fcd /]# hostname
45020b088fcd


[root@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


[root@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


[root@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+   13:24   0:00 ps -aux


退出容器

[root@cd05639b3f5c /]# exit
exit
[root@localhost ~]#


显示所有容器的列表(停止或运行的)
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
45020b088fcd        centos              "/bin/bash"         19 minutes ago      Up 19 minutes                                 nostalgic_ptolemy   
1a172adbc4ae        centos              "/bin/bash"         2 days ago          Exited (0) 2 days ago                         cocky_brattain      
e7a9a701b2ae        ubuntu              "/bin/bash"         2 days ago          Exited (0) 2 days ago                         thirsty_hopper      
a691456feccd        ubuntu              "/bin/bash"         2 days ago          Exited (0) 2 days ago                         cranky_noyce        
2e795b2ea988        ubuntu              "/bin/bash"         2 days ago          Exited (0) 2 days ago                         thirsty_elion       
94ab5a5ac359        centos              "/bin/bash"         2 days ago          Exited (137) 2 days ago                       serene_babbage      

6e485094bfdb        centos              "/bin/bash"         2 days ago          Exited (0) 2 days ago                         happy_davinci    

显示最后一个容器(运行或停止的)

[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

45020b088fcd        centos              "/bin/bash"         20 minutes ago      Up 20 minutes                           nostalgic_ptolemy   

显示当前正在运行容器的列表

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


容器命名:

[root@localhost ~]# docker run --name phpapp_comtainer -i -t centos /bin/bash

重启启动已停止的容器:

[root@localhost ~]# docker start phpapp_comtainer   容器名或者重启ID

phpapp_comtainer
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
e94f6675975f        centos              "/bin/bash"         16 minutes ago      Up 8 seconds                            phpapp_comtainer    
45020b088fcd        centos              "/bin/bash"         2 hours ago         Up 2 hours                              nostalgic_ptolemy   

进入shell:

[root@localhost ~]# docker attach e94f6675975f

回车

exit


查看日志:

[root@localhost ~]# docker logs e94f6675975f

监控日志:

[root@localhost ~]# docker logs -f e94f6675975f

查看最后10条:

[root@localhost ~]# docker logs --tail 10 45020b088fcd

查看最新日志:

[root@localhost ~]# docker logs --tail 0 -f 45020b088fcd

查看容器内的进程:

[root@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

深入容器:

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

。。。。

删除容器:

[root@localhost ~]# docker rm 94ab5a5ac359
94ab5a5ac359

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


使用docker的镜相和仓库

拉取镜像:

[root@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

镜像列表:
[root@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

使用镜像:
[root@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.

[root@68061e198dd5 /]# cat /etc/redhat-release 
CentOS Linux release 7.0.1406 (Core) 

创建守护进程式容器:

[root@localhost ~]# docker run -i -t --name daemon_dave -d centos:7.0.1406 /bin/sh
f6d9dcc6f9dc4ba480dcc108d74be631fbc20e10e90fc6a50434a84cb528ef83


构建镜像:

commit方式

到https://hub.docker.com/注册帐号

[root@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呢

[root@localhost static_web]# docker login
Username: jane1989
Password: 
Email: 363394@qq.com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
[root@localhost static_web]# cat /root/.docker/config.json
{
        "auths": {
                "https://index.docker.io/v1/": {
                        "auth": "amFuZTE5ODk6MTIzNDU2",
                        "email": "363394@qq.com"
                }
        }
}[root@localhost static_web]# 


用docker 的commit命令创建镜像:每次提交容器与当前差异部分,比较轻量

[root@localhost static_web]# docker run -i -t centos /bin/bash

在容器里装web服务:

[root@cde154b23ed2 /]# yum install -y epel-release

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

然后退出提交:

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

使用我们刚建的镜像:

[root@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

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

推送镜像:

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


Dockerfile方式

vi Dockerfile

FROM mysql:5.6

docker build -t eva/mysql ./mysql


删除镜像:

[root@localhost ~]# docker rmi jane1989/nginx:V1

批量删除:[root@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

生成新镜像

[root@localhost ~]# docker commit -m "test image" --author="jane" 3f2e8e42d52f jane1989/nginx:V2

[root@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

 打包

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

在另外的机器上导入镜像(如果是测试,可以把当前机器的镜像删除,然后导入)

[root@localhost ~]# docker load < /root/nginx\:V2.tar 
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              fb1f2c3c59a4        14 minutes ago      353 MB

给镜像重新打上标签

[root@localhost ~]# docker tag fb1f2c3c59a4 jane1989/nginx:V2
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
jane1989/nginx      V2                  fb1f2c3c59a4        17 minutes ago      353 MB
[root@localhost ~]# docker run -t -i -p 80:80 --name nginx1 jane1989/nginx:V2

[root@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


指定目录:
docker run -i -t -v /webtv:/webtv -v /etc/Sobey/nginx/init:/config/init -v /etc/Sobey/nginx/conf.d:/etc/nginx/conf.d --net=host --name=nginx4 centosimgages


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值