【1.6】docker

25.1 docker简介

  • 官网 www.docker.com
  • github https://github.com/docker/docker.github.io
  • 开源的容器引擎,可以让开发者打包应用以及依赖的库,然后发布到任何流行的linux发行版上,移植很方便
  • 由 go 语言编写,基于 apache2.0 协议发布
  • 基于 linux kernel,要想在 win 下运行需要借助一个 vm(虚拟机)来实现
  • 自 2013 年开始,近些年发展迅猛
  • docker 从1.13x开始,版本分为社区版 ce 和企业版 ee,并且基于年月的时间线形式,当前最新稳定版为 17.09 参考http://blog.csdn.net/chenhaifeng2016/article/details/68062414

Docker和传统的虚拟化比较:
在这里插入图片描述
Docker的优势:

  • 启动非常快,秒级实现
  • 资源利用率高,一台高配置服务器可以跑上千个docker容器
  • 更快的交付和部署,一次创建和配置后,可以在任意地方运行
  • 内核级别的虚拟化,不需要额外的hypevisor支持,会有更高的性能和效率
  • 易迁移,平台依赖性不强
    在这里插入图片描述
    Docker核心概念:
  • 镜像,是一个只读的模板,类似于安装系统用到的那个iso文件,我们通过镜像来完成各种应用的部署。
  • 容器,镜像类似于操作系统,而容器类似于虚拟机本身。它可以被启动、开始、停止、删除等操作,每个容器都是相互隔离的。
  • 仓库,存放镜像的一个场所,仓库分为公开仓库和私有仓库。 最大的公开仓库是Docker hub(hub.docker.com),国内公开仓库(dockerpool.com)

25.2 安装docker

1、下载 yum 源,并安装

[root@arslinux-01 ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o  /etc/yum.repos.d/docker.repo
[root@arslinux-01 ~]# yum install -y docker-ce

速度比较慢,大家也可以直接下载rpm包
https://download.docker.com/linux/centos/7/x86_64/stable/Packages/
上传到 linux 下并 yum 安装,可以自动解决依赖 yum install -y docker-ce-xxxx.rpm
2、启动 docker

[root@arslinux-01 ~]# systemctl start docker

25.3 镜像管理

1、下载 centos 镜像

[root@arslinux-01 ~]# docker pull centos

速度比较慢,可以使用 docker 加速器
2、配置 docker 加速器

[root@arslinux-01 ~]# vim /etc/docker/daemon.json
{
  "registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}

说明: 这个url为加速器地址,需要去ali云申请
配置完加速器,重启 docker,再次 docker pull centos 会快很多

  • docker image 查看本地镜像
  • docker search xxx 搜索镜像,其中 xxx 为关键词
  • docker tag 镜像名 标签 给镜像打标签(就是更改镜像名称)
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        4 months ago        202MB
[root@arslinux-01 ~]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5493                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              122                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.6.1810 x86…   111                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   96                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   59                                      
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              57                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      44                                      
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   39                                      
kinogmt/centos-ssh                 CentOS with SSH                                 28                                      [OK]
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   10                                      
guyton/centos6                     From official centos6 container with full up…   9                                       [OK]
drecom/centos-ruby                 centos ruby                                     6                                       [OK]
pivotaldata/centos                 Base centos, freshened up a little with a Do…   3                                       
mamohr/centos-java                 Oracle Java 8 Docker image based on Centos 7    3                                       [OK]
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   2                                       
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   2                                       
miko2u/centos6                     CentOS6 日本語環境                                   2                                       [OK]
ovirtguestagent/centos7-atomic     The oVirt Guest Agent for Centos 7 Atomic Ho…   2                                       
indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developmen…   1                                       [OK]
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
mcnaughton/centos-base             centos base image                               1                                       [OK]
pivotaldata/centos7-dev            CentosOS 7 image for GPDB development           0                                       
smartentry/centos                  centos with smartentry                          0                                       [OK]
pivotaldata/centos6.8-dev          CentosOS 6.8 image for GPDB development         0                             
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
centos              latest              9f38484d220f        4 months ago        202MB
  • 如果想更改上方 TAG 处信息,可以用 : 将 REPOSITORY : TAG 分开
[root@arslinux-01 ~]# docker tag centos test111:190808
[root@arslinux-01 ~]# docker tag centos test111:1234
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
centos              latest              9f38484d220f        4 months ago        202MB
test111             1234                9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

这里的 TAG 和命令中的 tag 不是一个概念

  • docker run -itd 镜像名 把镜像启动为容器
  • docker ps 查看运行的容器
  • docker ps -a 查看所有容器(包括未运行的)
[root@arslinux-01 ~]# docker run -itd centos
9d06f43b687172d3cd5ab13529645482f2a9d6fad286c0dd82a5783863db2617
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d06f43b6871        centos              "/bin/bash"         5 seconds ago       Up 3 seconds                            zealous_mestorf
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d06f43b6871        centos              "/bin/bash"         3 minutes ago       Up 3 minutes                            zealous_mestorf

说明: -i 表示让容器的标准输入打开,-t 表示分配一个伪终端,-d 表示后台启动,要把 -i -t -d 放到镜像名字前面

  • docker rmi 镜像名 删除指定镜像
[root@arslinux-01 ~]# docker rmi test111
Error: No such image: test111
[root@arslinux-01 ~]# docker rmi test111:1234
Untagged: test111:1234
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
centos              latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

其中后面的参数可以是 tag,如果是 tag 时,实际上是删除该 tag。当后面的参数为镜像 ID 时,则会彻底删除整个镜像,所有标签也会一同删除
不加 TAG 删除镜像,实际 TAG 就是 latest ,docker rmi test111 就是 docker rmi test111:latest

25.4 通过容器创建镜像

1、启动容器

[root@arslinux-01 ~]# docker run -itd centos
77fbe7aeaee649478aae095af386e6b7796e96e865bb95f7d673875fdf6767ec
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
77fbe7aeaee6        centos              "/bin/bash"         15 seconds ago      Up 12 seconds                           blissful_yonath

2、进入容器 docker exec -it xxxxx bash

[root@arslinux-01 ~]# docker exec -it 77fbe7 bash
[root@77fbe7aeaee6 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@77fbe7aeaee6 /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          28G  6.1G   22G  22% /
tmpfs            64M     0   64M   0% /dev
tmpfs           487M     0  487M   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/sda3        28G  6.1G   22G  22% /etc/hosts
tmpfs           487M     0  487M   0% /proc/asound
tmpfs           487M     0  487M   0% /proc/acpi
tmpfs           487M     0  487M   0% /proc/scsi
tmpfs           487M     0  487M   0% /sys/firmware
[root@77fbe7aeaee6 /]# free
              total        used        free      shared  buff/cache   available
Mem:         995896      654636       73140        2620      268120      133448
Swap:       1999868      169472     1830396

说明: 其中xxxxx为容器id,这个id可以用docker ps查看,最后面的bash为进入容器后我们要执行的命令,这样就可以打开一个终端
进入到该容器中,我们做一些变更,比如安装一些东西,然后针对这个容器进行创建新的镜像

3、容器中的操作和机器中操作没有区别

[root@77fbe7aeaee6 /]# yum install -y net-tools
[root@77fbe7aeaee6 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 3381  bytes 14925255 (14.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2448  bytes 135606 (132.4 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
        loop  txqueuelen 1000  (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

[root@77fbe7aeaee6 /]# exit
[root@arslinux-01 ~]# ifconfig |grep docker
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:c8ff:fe99:eec1  prefixlen 64  scopeid 0x20<link>
        ether 02:42:c8:99:ee:c1  txqueuelen 0  (Ethernet)
        RX packets 2448  bytes 101334 (98.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3373  bytes 14924599 (14.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

容器中网卡在容器外也出现了

[root@arslinux-01 ~]# ifconfig |grep veth
veth47c82bd: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::5014:d2ff:fe64:e486  prefixlen 64  scopeid 0x20<link>
        ether 52:14:d2:64:e4:86  txqueuelen 0  (Ethernet)
        RX packets 2448  bytes 135606 (132.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3381  bytes 14925255 (14.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

每开启一个容器,都会有一个虚拟网卡让容器和虚拟机通信

4、将容器做成镜像 docker commit -m “具体操作” -a “操作人” 容器id 新镜像名称

[root@arslinux-01 ~]# docker commit -m "install net-tools" -a "arslinux" 77fbe7aeaee6 centos_with_net
sha256:9c213599fd8f289da3333e0a0ddb55d8f208c020e3ace833461d2f0706cdd4d6
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_with_net     latest              9c213599fd8f        4 seconds ago       314MB
centos              latest              8f92b9698cb1        3 minutes ago       314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

说明: -m 加一些改动信息,-a 指定作者相关信息 77fbe7aeaee6 这一串为容器id,再后面为新镜像的名字

5、打开新镜像的容器并进入

[root@arslinux-01 ~]# docker run -itd centos_with_net
b1778f203633755d801b022e6db3c07d781f010acf4681cfe5f86b7639b18d6f
[root@arslinux-01 ~]# docker exec -it b1778f bash 
[root@b1778f203633 /]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.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

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (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

[root@b1778f203633 /]# ping www.baidu.com
PING www.a.shifen.com (180.97.33.107) 56(84) bytes of data.
64 bytes from 180.97.33.107 (180.97.33.107): icmp_seq=5 ttl=127 time=6.15 ms
64 bytes from 180.97.33.107 (180.97.33.107): icmp_seq=6 ttl=127 time=6.53 ms
^C
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 14037ms
rtt min/avg/max/mdev = 5.900/6.595/7.355/0.532 ms

25.5 通过模板创建镜像

1、下载模板 http://download.openvz.org/template/precreated/
本地下完后上传到虚拟机中
2、cat tar包 | docker import - 镜像名(自定义) 导入镜像

[root@arslinux-01 ~]# cat centos-6-x86-minimal.tar.gz |docker import - centos6
sha256:d0fc4ef11ea3c1cd7039fb595bdb51023d1f01af68d7c58c45b201000e12306e
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              d0fc4ef11ea3        9 seconds ago       512MB
centos_with_net     latest              9c213599fd8f        53 minutes ago      314MB
centos              latest              8f92b9698cb1        56 minutes ago      314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

3、启动容器,进入

[root@arslinux-01 ~]# docker run -itd centos6
ced4739b62045d77aa6983589a1e43edeacad2b8ec979b77a47b901131cd637
[root@arslinux-01 ~]# docker exec -it ced4739b62 bash
[root@ced4739b6204 /]# cat /etc/issue
CentOS release 6.8 (Final)
Kernel \r on an \m
[root@ced4739b6204 /]# uname -a
Linux ced4739b6204 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

docker 用的是虚拟机 linux 的内核,哪怕是 centos6 的docker ,用的依然是虚拟机 centos7 的内核

4、docker save -o 保存的文件名 镜像名 导出镜像

[root@arslinux-01 ~]# docker save -o centos7_with_nettool.tar centos_with_net
[root@arslinux-01 ~]# du -sh centos7_with_nettool.tar 
307M	cento7_with_nettool.tar

5、先删除镜像

[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ced4739b6204        centos6             "bash"              11 minutes ago      Up 11 minutes                           suspicious_perlman
b1778f203633        centos_with_net     "/bin/bash"         About an hour ago   Up About an hour                        dazzling_bell
77fbe7aeaee6        9f38484d220f        "/bin/bash"         2 hours ago         Up 2 hours                              blissful_yonath
[root@arslinux-01 ~]# docker rm -f b1778f203633
b1778f203633
[root@arslinux-01 ~]# docker rmi 9c213599fd8f
Untagged: centos_with_net:latest
Deleted: sha256:9c213599fd8f289da3333e0a0ddb55d8f208c020e3ace833461d2f0706cdd4d6
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              d0fc4ef11ea3        12 minutes ago      512MB
centos              latest              8f92b9698cb1        About an hour ago   314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

6、再用 docker load 载入

  • docker load imput 镜像包
  • docker load < 镜像包
    以上两种命令皆可
[root@arslinux-01 ~]# docker load < centos7_with_nettool.tar 
Loaded image: centos_with_net:latest
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              d0fc4ef11ea3        16 minutes ago      512MB
centos_with_net     latest              9c213599fd8f        About an hour ago   314MB
centos              latest              8f92b9698cb1        About an hour ago   314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值