【0730】docker 入門(上)

25.1 docker簡介php

  • 官網 www.docker.comjava

  • github  https://github.com/docker/docker.github.iomysql

  • 開源的容器引擎,可讓開發者打包應用以及依賴的庫,而後發佈到任何流行的linux發行版上,移植很方便linux

  • 由 go 語言編寫,基於 apache2.0 協議發佈nginx

  • 基於 linux kernel,要想在 win 下運行須要藉助一個 vm(虛擬機)來實現git

  • 自 2013 年開始,近些年發展迅猛github

  • docker 從1.13x開始,版本分爲社區版 ce 和企業版 ee,而且基於年月的時間線形式,當前最新穩定版爲 17.09 參考http://blog.csdn.net/chenhaifeng2016/article/details/6806241sql

Docker和傳統的虛擬化比較:docker

1212.png

Docker的優點:apache

  • 啓動很是快,秒級實現

  • 資源利用率高,一臺高配置服務器能夠跑上千個docker容器

  • 更快的交付和部署,一次建立和配置後,能夠在任意地方運行

  • 內核級別的虛擬化,不須要額外的hypevisor支持,會有更高的性能和效率

  • 易遷移,平臺依賴性不強

12133.png

Docker核心概念:

  • 鏡像,是一個只讀的模板,相似於安裝系統用到的那個iso文件,咱們經過鏡像來完成各類應用的部署。

  • 容器,鏡像相似於操做系統,而容器相似於虛擬機自己。它能夠被啓動、開始、中止、刪除等操做,每一個容器都是相互隔離的。

  • 倉庫,存放鏡像的一個場所,倉庫分爲公開倉庫和私有倉庫。 最大的公開倉庫是Docker hub(hub.docker.com),國內公開倉庫(dockerpool.com)


25.2 安裝docker

一、下載 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

二、啓動 docker

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


25.3 鏡像管理

  • 下載 centos 鏡像

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

速度比較慢,可使用 docker 加速器

  • 配置 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 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 經過容器建立鏡像

一、啓動容器

[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

二、進入容器    docker exec -it xxxxx  bash 

[root@arslinux-01 ~]# docker exec -it 77fbe7 bash
[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爲進入容器後咱們要執行的命令,這樣就能夠打開一個終端

進入到該容器中,咱們作一些變動,好比安裝一些東西,而後針對這個容器進行建立新的鏡像

三、容器中的操做和機器中操做沒有區別

[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

四、將容器作成鏡像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,再後面爲新鏡像的名字

五、打開新鏡像的容器並進入

[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 經過模板建立鏡像

一、下載模板 http://download.openvz.org/template/precreated/

本地下完後上傳到虛擬機中

二、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

三、啓動容器,進入

[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 的內核

四、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
307Mcento7_with_nettool.tar

五、先刪除鏡像

[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

六、再用 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


25.6 容器管理

  • docker create -it 鏡像名 bash      建立容器

  • docker start 容器id      啓動容器

[root@arslinux-01 ~]# docker create -it centos6 bash
03cea99df28677d8b4ade4e1d729d14b3c0d4c8d183cc9e76f239456ed878534
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
03cea99df286        centos6             "bash"              8 seconds ago       Created                                       hopeful_darwin
ced4739b6204        centos6             "bash"              37 minutes ago      Up 37 minutes                                 suspicious_perlman
77fbe7aeaee6        9f38484d220f        "/bin/bash"         2 hours ago         Up 2 hours                                    blissful_yonath
9d06f43b6871        9f38484d220f        "/bin/bash"         2 days ago          Exited (137) 2 days ago                       zealous_mestorf
[root@arslinux-01 ~]# docker start 03cea99df286
03cea99df286
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
03cea99df286        centos6             "bash"              29 seconds ago      Up 5 seconds                            hopeful_darwin
ced4739b6204        centos6             "bash"              37 minutes ago      Up 37 minutes                           suspicious_perlman
77fbe7aeaee6        9f38484d220f        "/bin/bash"         2 hours ago         Up 2 hours                              blissful_yonath

啓動容器後,可使用 docker ps  查看到,有 start 就有 stop,和 restart

docker create 再 docker start 至關於 docker run

  • docker run -it 鏡像名 bash     啓動並進入容器

[root@arslinux-01 ~]# docker run -it centos bash
  • docker run -d   可讓容器在後臺運行

例如:docker run -d centos bash -c "while :; do echo "123"; sleep 2; done"

  • docker run --name 名稱 -itd 鏡像 bash      給容器自定義名字

[root@arslinux-01 ~]# docker run --name arsenal_pepe -itd centos6 bash
6b975789ec240520e4316699cfb83a9752a84da8890f62b18085ce759d252152
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6b975789ec24        centos6             "bash"              22 seconds ago      Up 21 seconds                           arsenal_pepe
03cea99df286        centos6             "bash"              27 minutes ago      Up 27 minutes                           hopeful_darwin
ced4739b6204        centos6             "bash"              About an hour ago   Up About an hour                        suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         2 hours ago         Up 2 hours                              blissful_yonath
[root@arslinux-01 ~]# docker exec -it arsenal_pepe bash
[root@6b975789ec24 /]#
  • docker run --rm -it 容器名 bash -c "命令"      命令執行完容器就會退出

[root@arslinux-01 ~]# docker run --rm -it centos bash -c "sleep 15"

--rm 可讓容器退出後直接刪除,在這裏命令執行完容器就會退出

  • docker logs 容器id      查看容器運行歷史信息

[root@arslinux-01 ~]# docker run -itd centos bash -c "echo 123"
066f53f67367fe0da907c26d7abd2d0c95acff40eaf2a0203fba556787f4ab83
[root@arslinux-01 ~]# docker logs 066f53f67367
123
  • docker attach 容器id      進入後臺運行的容器

  • docker exec -it 容器id bash      臨時打開一個虛擬終端,exit 後,容器依然運行着

attach命令不算好用,好比咱們想要退出終端,就得exit了,這樣容器也就退出了,用 exec 比較

  • docker rm 容器id      刪除容器

[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                        PORTS               NAMES
066f53f67367        centos              "bash -c 'echo 123'"   5 minutes ago       Exited (0) 5 minutes ago                          sharp_liskov
6b975789ec24        centos6             "bash"                 18 minutes ago      Up 18 minutes                                     arsenal_pepe
944bc4839130        centos              "bash"                 30 minutes ago      Exited (127) 30 minutes ago                       nervous_sammet
37366b75bd90        centos              "/bin/bash"            30 minutes ago      Exited (127) 30 minutes ago                       inspiring_saha
a80eed529a34        centos              "bash"                 31 minutes ago      Exited (0) 30 minutes ago                         hungry_elgamal
32da41e087c2        centos              "bash"                 32 minutes ago      Exited (0) 32 minutes ago                         infallible_tereshkova
985de0f4f889        centos              "bash"                 33 minutes ago      Exited (0) 33 minutes ago                         nifty_nightingale
d55e81eae6aa        centos              "/bin/bash"            35 minutes ago      Exited (0) 33 minutes ago                         condescending_varahamihira
02bccad25efb        centos              "/bin/bash"            38 minutes ago      Exited (0) 37 minutes ago                         heuristic_wu
03cea99df286        centos6             "bash"                 46 minutes ago      Up 45 minutes                                     hopeful_darwin
ced4739b6204        centos6             "bash"                 About an hour ago   Up About an hour                                  suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"            3 hours ago         Up 3 hours                                        blissful_yonath
9d06f43b6871        centos              "/bin/bash"            2 days ago          Exited (137) 2 days ago                           zealous_mestorf
[root@arslinux-01 ~]# docker rm 066f53f67367
066f53f67367
[root@arslinux-01 ~]# docker rm 944bc4839130
944bc4839130
[root@arslinux-01 ~]# docker rm 37366b75bd90
37366b75bd90
[root@arslinux-01 ~]# docker rm a80eed529a34
a80eed529a34
[root@arslinux-01 ~]# docker rm 32da41e087c2
32da41e087c2
[root@arslinux-01 ~]# docker rm 985de0f4f889
985de0f4f889
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6b975789ec24        centos6             "bash"              19 minutes ago      Up 19 minutes                                   arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"         36 minutes ago      Exited (0) 34 minutes ago                       condescending_varahamihira
02bccad25efb        centos              "/bin/bash"         40 minutes ago      Exited (0) 38 minutes ago                       heuristic_wu
03cea99df286        centos6             "bash"              47 minutes ago      Up 46 minutes                                   hopeful_darwin
ced4739b6204        centos6             "bash"              About an hour ago   Up About an hour                                suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         3 hours ago         Up 3 hours                                      blissful_yonath
9d06f43b6871        centos              "/bin/bash"         2 days ago          Exited (137) 2 days ago                         zealous_mestor

——對於啓動狀態的容器,刪除須要加 -f 強行刪除

[root@arslinux-01 ~]# docker rm 03cea99df286
Error response from daemon: You cannot remove a running container 03cea99df28677d8b4ade4e1d729d14b3c0d4c8d183cc9e76f239456ed878534. Stop the container before attempting removal or force remove
[root@arslinux-01 ~]# docker rm -f 03cea99df286
03cea99df286
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6b975789ec24        centos6             "bash"              21 minutes ago      Up 21 minutes                                   arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"         38 minutes ago      Exited (0) 36 minutes ago                       condescending_varahamihira
02bccad25efb        centos              "/bin/bash"         41 minutes ago      Exited (0) 40 minutes ago                       heuristic_wu
ced4739b6204        centos6             "bash"              About an hour ago   Up About an hour                                suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         3 hours ago         Up 3 hours                                      blissful_yonath
9d06f43b6871        centos              "/bin/bash"         2 days ago          Exited (137) 2 days ago                         zealous_mestor
  • docker export 容器id > 文件.tar      導出容器

  • cat 文件.tar |docker import - 鏡像名      生成鏡像

[root@arslinux-01 ~]# docker export 9d06f43b6871 > centos.tar

[root@arslinux-01 ~]# cat centos.tar |docker import - centos111

sha256:48f71221434353e6f02227794e1a95fcc8ae5f0dc9608142febc957dd124dba0


25.7 倉庫管理

  • docker start          啓動未啓動的容器(重啓docker服務後,容器會退出)

[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6b975789ec24        centos6             "bash"              27 hours ago        Exited (137) 22 hours ago                       arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"         28 hours ago        Exited (0) 28 hours ago                         condescending_varahamihira
02bccad25efb        centos              "/bin/bash"         28 hours ago        Exited (0) 28 hours ago                         heuristic_wu
ced4739b6204        centos6             "bash"              29 hours ago        Exited (137) 22 hours ago                       suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         30 hours ago        Exited (137) 22 hours ago                       blissful_yonath
9d06f43b6871        centos              "/bin/bash"         3 days ago          Exited (137) 3 days ago                         zealous_mestorf
[root@arslinux-01 ~]# docker start 6b975789ec24
6b975789ec24
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6b975789ec24        centos6             "bash"              27 hours ago        Up 1 second                             arsenal_pepe

一、利用 registry 建立私有倉庫

1)下載 registry 鏡像

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

2)啓動 registry 容器

  • docker run -d -p 宿主機端口號:容器端口號 registry      啓動 registry 容器

[root@arslinux-01 ~]# docker run -d -p 5000:5000 registry
3f51ff27501d3f81a29996cc04b8a16ae9b85a9c5b03f4876d13f16fc53e2315
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3f51ff27501d        registry            "/entrypoint.sh /etc…"   9 seconds ago       Up 7 seconds        0.0.0.0:5000->5000/tcp   quizzical_chandrasekhar
6b975789ec24        centos6             "bash"                   28 hours ago        Up 14 minutes                                arsenal_pepe

說明:以registry鏡像啓動容器,-p會把容器的端口映射到宿主機上,:左邊爲宿主機監聽端口,:右邊爲容器監聽端口

3)查看 registry 倉庫內容

  • curl 127.0.0.1:5000/v2/_catalog     查看 registry 倉庫內容

[root@arslinux-01 ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":[]}

二、上傳鏡像到私有倉庫

1)標記鏡像

  • docker tag 要標記的鏡像 ip:端口/鏡像      標記tag,必須帶私有倉庫 ip:端口

[root@arslinux-01 ~]# docker tag centos6 192.168.194.130:5000/centos6
[root@arslinux-01 ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.194.130:5000/centos6   latest              d0fc4ef11ea3        29 hours ago        512MB
centos6                        latest              d0fc4ef11ea3        29 hours ago        512MB
centos_with_net                latest              9c213599fd8f        30 hours ago        314MB
ubuntu                         latest              3556258649b2        2 weeks ago         64.2MB
arslinux                       latest              9f38484d220f        5 months ago        202MB
centos                         latest              9f38484d220f        5 months ago        202MB
test111                        190808              9f38484d220f        5 months ago        202MB
registry                       latest              f32a97de94e1        5 months ago        25.8MB

2)上傳鏡像

  • docker push ip:端口/鏡像      上傳鏡像到 registry

[root@arslinux-01 ~]# docker push 192.168.194.130:5000/centos6
The push refers to repository [192.168.194.130:5000/centos6]
Get https://192.168.194.130:5000/v2/: http: server gave HTTP response to HTTPS client

3)指定私有倉庫地址

以上報錯須要修改配置文件 /etc/docker/daemon.json,在 { } 中加一行內容(若是本來有內容需用逗號 , 隔開)

[root@arslinux-01 ~]# vim /etc/docker/daemon.json
{ "insecure-registries":["192.168.194.130:5000"] }

4)重啓 docker、鏡像

[root@arslinux-01 ~]# systemctl restart docker
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
3f51ff27501d        registry            "/entrypoint.sh /etc…"   31 minutes ago      Exited (2) 2 minutes ago                         quizzical_chandrasekhar
6b975789ec24        centos6             "bash"                   28 hours ago        Exited (137) 4 minutes ago                       arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"              28 hours ago        Exited (0) 28 hours ago                          condescending_varahamihira
02bccad25efb        centos              "/bin/bash"              29 hours ago        Exited (0) 29 hours ago                          heuristic_wu
ced4739b6204        centos6             "bash"                   29 hours ago        Exited (137) 23 hours ago                        suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"              31 hours ago        Exited (137) 23 hours ago                        blissful_yonath
9d06f43b6871        centos              "/bin/bash"              4 days ago          Exited (137) 4 days ago                          zealous_mestorf
[root@arslinux-01 ~]# docker start 3f51ff27501d
3f51ff27501d
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3f51ff27501d        registry            "/entrypoint.sh /etc…"   31 minutes ago      Up 3 seconds        0.0.0.0:5000->5000/tcp   quizzical_chandrasekhar

5)從新上傳

[root@arslinux-01 ~]# docker push 192.168.194.130:5000/centos6
The push refers to repository [192.168.194.130:5000/centos6]
0a2f11f7b1ef: Pushed
latest: digest: sha256:5c0d4992624653abc8764aec2409903be43105246fffd961fa69c3adbc78f0be size: 529

6)查看倉庫內容 curl

[root@arslinux-01 ~]# curl 192.168.194.130:5000/v2/_catalog
{"repositories":["centos6"]}

7)再用以上方法上傳 ubuntu

[root@arslinux-01 ~]# docker tag ubuntu 192.168.194.130:5000/ubuntu
[root@arslinux-01 ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.194.130:5000/centos6   latest              d0fc4ef11ea3        29 hours ago        512MB
centos6                        latest              d0fc4ef11ea3        29 hours ago        512MB
centos_with_net                latest              9c213599fd8f        30 hours ago        314MB
192.168.194.130:5000/ubuntu    latest              3556258649b2        2 weeks ago         64.2MB
ubuntu                         latest              3556258649b2        2 weeks ago         64.2MB
centos                         latest              9f38484d220f        5 months ago        202MB
test111                        190808              9f38484d220f        5 months ago        202MB
arslinux                       latest              9f38484d220f        5 months ago        202MB
registry                       latest              f32a97de94e1        5 months ago        25.8MB
[root@arslinux-01 ~]# docker push 192.168.194.130:5000/ubuntu
The push refers to repository [192.168.194.130:5000/ubuntu]
b079b3fa8d1b: Pushed
a31dbd3063d7: Pushed
c56e09e1bd18: Pushed
543791078bdb: Pushed
latest: digest: sha256:d91842ef309155b85a9e5c59566719308fab816b40d376809c39cf1cf4de3c6a size: 1152
[root@arslinux-01 ~]# curl 192.168.194.130:5000/v2/_catalog
{"repositories":["centos6","ubuntu"]}


25.8 數據管理

  • 其餘機器如何從私有倉庫下載?

一、使用的機器安裝 docker,啓動docker

[root@arslinux-02 ~]# yum install -y docker-ce-19.03.1-3.el7.x86_64.rpm
[root@arslinux-02 ~]# systemctl start docker

二、編輯 /etc/docker/daemon.json,添加私有倉庫地址,重啓 docker

[root@arslinux-02 ~]# vim /etc/docker/daemon.json
{ "insecure-registries":["192.168.194.130:5000"] }
[root@arslinux-02 ~]# systemctl restart docker

三、從倉庫下載鏡像

[root@arslinux-02 ~]# docker pull 192.168.194.130:5000/ubuntu
Using default tag: latest
latest: Pulling from ubuntu
7413c47ba209: Pull complete
0fe7e7cbb2e8: Pull complete
1d425c982345: Pull complete
344da5c95cec: Pull complete
Digest: sha256:d91842ef309155b85a9e5c59566719308fab816b40d376809c39cf1cf4de3c6a
Status: Downloaded newer image for 192.168.194.130:5000/ubuntu:latest
192.168.194.130:5000/ubuntu:latest

一樣也能夠從官方下載鏡像

——將宿主機目錄掛載到容器中,減少數據丟失的風險

一、掛載本地的目錄到容器中

  • docker run -itd -v 宿主機目錄:容器目錄 使用的鏡像 bash         掛載

[root@arslinux-01 ~]# ls /data/
ftp  mysql  wwwroot
[root@arslinux-01 ~]# docker run -itd -v /data/:/data centos_with_net bash
b8bbf33335c6491bee278976f64383ca627b157d1f80f6dc132886fe4cc9513a
[root@arslinux-01 ~]# docker exec -it b8bbf33 bash
[root@b8bbf33335c6 /]# ls /data/
ftp  mysql  wwwroot

說明:-v 用來指定掛載目錄;:前面的/data/爲宿主機本地目錄,:後面的/data/爲容器裏的目錄,會在容器中自動建立,兩個目錄能夠不同

——容器中修改內容,看宿主機變化

[root@b8bbf33335c6 /]# mkdir /data/12334
[root@b8bbf33335c6 /]# exit
exit
[root@arslinux-01 ~]# ls /data/
12334  ftp  mysql  wwwroot

相似於映射

二、掛載數據卷

  • docker run -itd --volumes-from 容器名 鏡像名 bash          掛載數據卷

[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b8bbf33335c6        centos_with_net     "bash"                   11 minutes ago      Up 11 minutes                                keen_brattain
3f51ff27501d        registry            "/entrypoint.sh /etc…"   3 days ago          Up 33 minutes       0.0.0.0:5000->5000/tcp   quizzical_chandrasekhar
[root@arslinux-01 ~]# docker run -itd --volumes-from keen_brattain arslinux bash
6ba3b2b7b37ea12aeeca0e885ecc4796a3bfc50016f50cea73f4bbc455c742aa
[root@arslinux-01 ~]# docker exec -it 6ba3b2b7b bash
[root@6ba3b2b7b37e /]# ls /data/
12334  ftp  mysql  wwwroot

說明:其實咱們掛載目錄的時候,能夠指定容器 name,若是不指定就隨機定義了。好比上面咱們沒有指定,它就生成了一個名字爲 keen_brattain,這個名字可使用命令 docker ps 看最右側一列

這樣,咱們使用 arslinux 鏡像建立了新的容器,而且使用了 relaxed_franklin  容器的數據卷

三、定義數據卷容器

有時候,咱們須要多個容器之間相互共享數據,相似於 linux 裏面的 NFS,因此就能夠搭建一個專門的數據卷容器,而後其餘容器直接掛載該數據卷

首先,創建數據卷容器

[root@arslinux-01 ~]# docker run -itd -v /data/ --name testvol centos bash
8ac3ab5f3cabd5232ff1da88f7d188434c29e6597da2399ef14f585a570d22fc

而後,讓其餘容器掛載該數據卷

[root@arslinux-01 ~]# docker run -itd --volumes-from testvol arslinux bash
f222617a931c4857d14995593359c827a377bfc3200f89a01fbfdc4f01b01c14


25.9 數據卷備份恢復

121212.png

——備份

一、宿主機建立備份目錄

[root@arslinux-01 ~]# mkdir /data/backup

二、備份

[root@arslinux-01 ~]# docker run --volumes-from testvol -v /data/backup/:/backup centos tar cvf /backup/data.tar /data/
tar: Removing leading `/' from member names
/data/
[root@arslinux-01 ~]# ls /data/backup/
data.tar

說明:首先咱們須要使用 testvol 數據卷新開一個容器,同時咱們還須要把本地的 /data/backup/ 目錄掛載到該容器的 /backup下,這樣在容器中 /backup 目錄裏面新建的文件,咱們就能夠直接在 /data/backup/ 目錄中看到了。 而後再把 /data/ 目錄下面的文件打包到成 data.tar 文件放到 /backup 目錄下面

——恢復

思路: 先新建一個數據卷容器,再建一個新的容器並掛載該數據卷容器,而後再把 tar 包解包

一、新建數據卷容器

[root@arslinux-01 ~]# docker run -itd -v /data/ --name testvol2 centos bash
db37af0c5168b405a17594857e80ad0ae8a1bdd780355fd5194269475b24eab0

二、掛載數據卷新建容器,並解包

[root@arslinux-01 ~]# docker run --volumes-from testvol2 -v /data/backup/:/backup centos tar xvf /backup/data.tar
data/


25.10 docker 網絡模式

  • host 模式,使用 docker run 時使用 --net=host 指定

docker 使用的網絡實際上和宿主機同樣,在容器內看到的網卡ip是宿主機 ip

  • container 模式,使用 --net=container:container_id/container_name

多個容器使用共同的網絡,看到的ip是同樣的

  • none 模式,使用 --net=none 指定

這種模式下,不會配置任何網絡

  • bridge 模式,使用 --net=bridge 指定默認模式,不用指定默認就是這種網絡模式。

這種模式會爲每一個容器分配一個獨立的 Network Namespace。相似於 vmware 的 nat 網絡模式。同一個宿主機上的全部容器會在同一個網段下,相互之間是能夠通訊的

[root@arslinux-01 ~]# docker start keen_brattain
keen_brattain
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
db37af0c5168        centos              "bash"              29 minutes ago      Up 29 minutes                           testvol2
ff72d738a130        arslinux            "bash"              31 minutes ago      Up 31 minutes                           musing_brown
504f568c9f78        centos              "bash"              31 minutes ago      Up 31 minutes                           testvol
3c8c66de157f        centos              "bash"              38 minutes ago      Up 38 minutes                           strange_merkle
b8bbf33335c6        centos_with_net     "bash"              3 days ago          Up 33 seconds                           keen_brattain
[root@arslinux-01 ~]# docker exec -it b8bbf33335c6 bash
[root@b8bbf33335c6 /]# yum install -y epel-release
[root@b8bbf33335c6 /]# yum install -y nginx
[root@b8bbf33335c6 /]# systemctl start nginx
Failed to get D-Bus connection: Operation not permitted
[root@b8bbf33335c6 /]# exit
exit
[root@arslinux-01 ~]# docker run -itd -p 8088:80 centos_with_nginx bash
8082d76f51020755cb179b8a9aa975a1eb648f36cd33aa837033a8a49bc4c677



25.11 opration not permitted

[root@arslinux-01 ~]# docker ps
CONTAINER ID     IMAGE             COMMAND       CREATED           STATUS          PORTS        NAMES
4b35349a250e     centos            "/bin/bash"   4 minutes ago     Up 4 minutes                 gracious_moore
db37af0c5168     centos            "bash"        44 minutes ago    Up 44 minutes                testvol2
ff72d738a130     arslinux          "bash"        47 minutes ago    Up 47 minutes                musing_brown
504f568c9f78     centos            "bash"        47 minutes ago    Up 47 minutes                testvol
3c8c66de157f     centos            "bash"        54 minutes ago    Up 54 minutes                strange_merkle
b8bbf33335c6     centos_with_net   "bash"        3 days ago        Up 16 minutes                keen_brattain
[root@arslinux-01 ~]# docker commit -m "install nginx" -a "arslinux" b8bbf33335c6 centos_with_nginx
sha256:b25e841545c54449ef945319e1e9b323a4f77b587591a1f30036a0ec6ed2b0bc
[root@arslinux-01 ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
centos_with_nginx              latest              b25e841545c5        8 seconds ago       523MB
centos111                      latest              48f712214343        6 days ago          202MB
centos6                        latest              d0fc4ef11ea3        7 days ago          512MB
192.168.194.130:5000/centos6   latest              d0fc4ef11ea3        7 days ago          512MB
centos_with_net                latest              9c213599fd8f        7 days ago          314MB
192.168.194.130:5000/ubuntu    latest              3556258649b2        3 weeks ago         64.2MB
ubuntu                         latest              3556258649b2        3 weeks ago         64.2MB
arslinux                       latest              9f38484d220f        5 months ago        202MB
centos                         latest              9f38484d220f        5 months ago        202MB
test111                        190808              9f38484d220f        5 months ago        202MB
registry                       latest              f32a97de94e1        5 months ago        25.8MB
[root@arslinux-01 ~]# docker run -itd --privileged -e "container=docker" -p 8088:80 centos_with_nginx /usr/sbin/init
f8125fbc05426351395f22f9b143509ff9df04c288c86b0b5086a582dcb7461d
[root@arslinux-01 ~]# docker ps
CONTAINER ID     IMAGE              COMMAND           CREATED            STATUS           PORTS                 NAMES
f8125fbc0542     centos_with_nginx  "/usr/sbin/init"  5 seconds ago      Up 3 seconds     0.0.0.0:8088->80/tcp  compassionate_brattain
4b35349a250e     centos             "/bin/bash"       16 minutes ago     Up 16 minutes                          gracious_moore
db37af0c5168     centos             "bash"            56 minutes ago     Up 56 minutes                          testvol2
ff72d738a130     arslinux           "bash"            58 minutes ago     Up 58 minutes                          musing_brown
504f568c9f78     centos             "bash"            59 minutes ago     Up 59 minutes                          testvol
3c8c66de157f     centos             "bash"            About an hour ago  Up About an hour                       strange_merkle
b8bbf33335c6     centos_with_net    "bash"            3 days ago         Up 27 minutes                          keen_brattain
[root@arslinux-01 ~]# docker exec -it f8125fbc0542 bash
[root@f8125fbc0542 /]# systemctl start nginx
[root@f8125fbc0542 /]# ps aux|grep nginx
root       3412  0.0  0.2 125108  2112 ?        Ss   14:17   0:00 nginx: master process /usr/sbin/nginx
nginx      3413  0.0  0.3 125496  3140 ?        S    14:17   0:00 nginx: worker process
root       3415  0.0  0.0   9088   668 pts/1    S+   14:17   0:00 grep --color=auto nginx
[root@f8125fbc0542 /]# netstat -lntp|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3412/nginx: master
tcp6       0      0 :::80                   :::*                    LISTEN      3412/nginx: master
[root@f8125fbc0542 /]# exit
[root@arslinux-01 ~]# curl localhost:8088
[root@arslinux-02 ~]# curl 192.168.194.130:8088

宿主機上訪問 8088 就是容器中的 80 端口,其餘主機想要訪問容器,訪問宿主機的 8088 端口便可


25.12 配置橋接網絡

爲了使本地網絡中的機器和 Docker 容器更方便的通訊,咱們常常會有將 Docker 容器配置到和主機同一網段的需求。這個需求其實很容易實現,咱們只要將 Docker 容器和宿主機的網卡橋接起來,再給 Docker 容器配上 IP 就能夠了

一、以默認網卡參數爲模板,編輯新的網卡參數 br0

[root@arslinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@arslinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@arslinux-01 network-scripts]# vim !$
vim ifcfg-br0

將 第一行改成 TYPE=Bridge,NAME=br0,DEVICE=br0,其餘參數不變

二、更改默認網卡參數 ens33,重啓網絡

[root@arslinux-01 network-scripts]# vim ifcfg-ens33

將 UUID、IPADDR、NETMASK、GATEWAY、DNS所有註釋掉

並在最後增長一行 BRIDGE=br0

[root@arslinux-01 network-scripts]# systemctl restart network
[root@arslinux-01 network-scripts]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.194.130  netmask 255.255.255.0  broadcast 192.168.194.255
    inet6 fe80::7b5a:1723:e063:9f29  prefixlen 64  scopeid 0x20<link>
    ether 00:0c:29:24:ea:f2  txqueuelen 1000  (Ethernet)
    RX packets 33  bytes 16939 (16.5 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 41  bytes 3861 (3.7 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
    inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
    ether 02:42:2e:f0:d8:fb  txqueuelen 0  (Ethernet)
    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
    
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    ether 00:0c:29:24:ea:f2  txqueuelen 1000  (Ethernet)
    RX packets 5569  bytes 971820 (949.0 KiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 4869  bytes 493960 (482.3 KiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.100.1  netmask 255.255.255.0  broadcast 192.168.100.255
    inet6 fe80::f41:9da7:d8e3:10ba  prefixlen 64  scopeid 0x20<link>
    ether 00:0c:29:24:ea:fc  txqueuelen 1000  (Ethernet)
    RX packets 0  bytes 0 (0.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 59  bytes 4514 (4.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
    inet6 ::1  prefixlen 128  scopeid 0x10<host>
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 61507  bytes 19731949 (18.8 MiB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 61507  bytes 19731949 (18.8 MiB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

三、安裝 pipwork

[root@arslinux-01 ~]# git clone https://github.com/jpetazzo/pipework
[root@arslinux-01 ~]# cp pipework/pipework /usr/local/bin/

四、開啓一個容器

[root@arslinux-01 ~]# docker run -itd --net=none centos_with_nginx bash
43109bea3b870a590178677caf548dd04b0ab6d08201a50f9ff8c45c67b5e8a4
[root@arslinux-01 ~]# docker exec -it 43109b bash
[root@43109bea3b87 /]# ifconfig
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@43109bea3b87 /]# exit
exit

四、爲容器添加新的虛擬網卡

[root@arslinux-01 ~]# pipework br0 43109b 192.168.194.135/24@192.168.194.2
[root@arslinux-01 ~]# docker exec -it 43109b bash
[root@43109bea3b87 /]# ifconfig
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.194.135  netmask 255.255.255.0  broadcast 192.168.194.255
    ether 42:b1:e4:da:4e:c4  txqueuelen 1000  (Ethernet)
    RX packets 7  bytes 586 (586.0 B)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 1  bytes 42 (42.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@43109bea3b87 /]# ping www.qq.com
PING public.sparta.mig.tencent-cloud.net (61.151.166.139) 56(84) bytes of data.
64 bytes from 139.166.151.61.dial.xw.sh.dynamic.163data.com.cn (61.151.166.139): icmp_seq=1 ttl=128 time=12.0 ms
64 bytes from 139.166.151.61.dial.xw.sh.dynamic.163data.com.cn (61.151.166.139): icmp_seq=2 ttl=128 time=12.5 ms
64 bytes from 139.166.151.61.dial.xw.sh.dynamic.163data.com.cn (61.151.166.139): icmp_seq=3 ttl=128 time=12.0 ms
^C
--- public.sparta.mig.tencent-cloud.net ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 12.022/12.202/12.501/0.212 ms
[root@43109bea3b87 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.194.2   0.0.0.0         UG    0      0        0 eth1
192.168.194.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1

六、其餘主機上測試鏈接

[root@arslinux-02 ~]# ping 192.168.194.135
PING 192.168.194.135 (192.168.194.135) 56(84) bytes of data.
64 bytes from 192.168.194.135: icmp_seq=1 ttl=64 time=0.581 ms
64 bytes from 192.168.194.135: icmp_seq=2 ttl=64 time=0.318 ms
64 bytes from 192.168.194.135: icmp_seq=3 ttl=64 time=0.669 ms
^C
--- 192.168.194.135 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.318/0.522/0.669/0.151 ms
相關文章
相關標籤/搜索