一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

云服務器|WEB服務器|FTP服務器|郵件服務器|虛擬主機|服務器安全|DNS服務器|服務器知識|Nginx|IIS|Tomcat|

服務器之家 - 服務器技術 - 服務器知識 - Docker 網(wǎng)絡命令詳解

Docker 網(wǎng)絡命令詳解

2021-01-27 17:54Aiapple 服務器知識

這篇文章主要詳細介紹了Docker 網(wǎng)絡命令的相關資料,需要的朋友可以參考下

•docker network create
•docker network connect
•docker network ls
•docker network rm
•docker network disconnect
•docker network inspect

創(chuàng)建網(wǎng)絡

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
zane@zane-v:~$ docker network create simple-network
zane@zane-v:~$ docker network inspect simple-network
 {
  "name": "simple-network",
  "id": "8bf58f43c56622d1100f7da9ef6506e45a4aa68556b586311f3756130c311d75",
  "scope": "local",
  "driver": "bridge",
  "enableipv6": false,
  "ipam": {
   "driver": "default",
   "options": {},
   "config": [
    {
     "subnet": "172.20.0.0/16",
     "gateway": "172.20.0.1/16"
    }
   ]
  },
  "internal": false,
  "containers": {},
  "options": {},
  "labels": {}
 }

•進入一個鍵值存儲。引擎支持consul,etcd,zookeeper.
•在群集中的每個主機上正確配置的deamon引擎

支持overlay網(wǎng)絡的docker選項:

•--cluster-store-opt

使用--subnet選項直接指定子網(wǎng)絡,在bridge網(wǎng)絡中只可以指定一個子網(wǎng)絡,而在overlay網(wǎng)絡中支持多個子網(wǎng)絡。
除了--subnet,還可以指定:--gateway,--ip-range,--aux-address選項。

?
1
2
3
4
5
6
7
8
$ docker network create -d overlay \
--subnet=192.168.0.0/16 \
--subnet=192.170.0.0/16 \
--gateway=192.168.0.100 \
--gateway=192.170.0.100 \
--ip-range=192.168.1.0/24 \
--aux-address="my-switch=192.168.1.6" \
--aux-address="my-nas=192.170.1.6" \

如何要創(chuàng)建自己定制的網(wǎng)絡,docker也是支持很多選項的。
可以指定網(wǎng)絡的端口號:

?
1
2
3
4
5
6
$ docker run -d -p --name redis --network my-network redis
 
$ docker ps
container id image command created status ports names
bafb0c808c53 redis "/entrypoint.sh redis" 4 seconds ago up 3 seconds 172.23.0.1:32770->6379/tcp redis

連接容器

可以連接已存在的容器到一個或者多個網(wǎng)絡中。一個容器可以連接到多個不同網(wǎng)絡驅(qū)動的網(wǎng)絡中。
當連接一旦建立,容器便可以可其他的容器通訊,通過ip 或者 容器名稱。

基本容器網(wǎng)絡實例:

1.創(chuàng)建兩個容器,container1 和 container2

?
1
2
3
4
5
$ docker run -itd --name=container1 busybox
 
$ docker run -itd --name=container2 busybox
 
zane@zane-v:~$ docker network create -d bridge --subnet 172.25.0.0/16 isolated_nw

3.連接container2到這個網(wǎng)絡,然后驗證一下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
zane@zane-v:~$ docker network connect isolated_nw container2
 
zane@zane-v:~$ docker network inspect isolated_nw
 {
  "name": "isolated_nw",
  "id": "a8208641505d2d8fc37bf7cbd1027c01f0def461815786e076ef4ae65b7b2f9b",
  "scope": "local",
  "driver": "bridge",
  "enableipv6": false,
  "ipam": {
   "driver": "default",
   "options": {},
   "config": [
    {
     "subnet": "172.25.0.0/16"
    }
   ]
  },
  "internal": false,
  "containers": {
   "e9bce535ae32945f5e43340facdb6c16c93d92119e85b61c6cb7a5379a0caf63": {
    "name": "container2",
    "endpointid": "ef7244d32484407c3ec4aa30b7bdb0a6cbe3dbbfedc03e5c856ad20a08af172f",
    "macaddress": "02:42:ac:19:00:02",
    "ipv4address": "172.25.0.2/16",
    "ipv6address": ""
   }
  },
  "options": {},
  "labels": {}
 }

注意container2,自動分配到了ip地址。此時container1,仍然連接在默認的bridge網(wǎng)絡。

4.啟動第三個container,但是這是使用--ip 選項指定它的ip地址,

?
1
zane@zane-v:~$ docker run --network=isolated_nw --ip=172.25.3.3 -itd --name=container3 busybox

5.檢查container3使用的是哪個網(wǎng)絡:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"networks": {
   "isolated_nw": {
    "ipamconfig": {
     "ipv4address": "172.25.3.3"
    },
    "links": null,
    "aliases": [
     "adf68dd9e09c"
    ],
    "networkid": "a8208641505d2d8fc37bf7cbd1027c01f0def461815786e076ef4ae65b7b2f9b",
    "endpointid": "71d5d272d056b6111a83f0843a10d1944f1648f34d5099258d5865d053a939b0",
    "gateway": "172.25.0.1",
    "ipaddress": "172.25.3.3",
    "ipprefixlen": 16,
    "ipv6gateway": "",
    "globalipv6address": "",
    "globalipv6prefixlen": 0,
    "macaddress": "02:42:ac:19:03:03"
   }
  }
 }

6.檢查container2使用的是哪個網(wǎng)絡:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
"networks": {
    "isolated_nw": {
     "aliases": [
      "e9bce535ae32"
     ],
     "endpointid": "ef7244d32484407c3ec4aa30b7bdb0a6cbe3dbbfedc03e5c856ad20a08af172f",
     "gateway": "172.25.0.1",
     "globalipv6address": "",
     "globalipv6prefixlen": 0,
     "ipamconfig": {},
     "ipaddress": "172.25.0.2",
     "ipprefixlen": 16,
     "ipv6gateway": "",
     "links": null,
     "macaddress": "02:42:ac:19:00:02",
     "networkid": "a8208641505d2d8fc37bf7cbd1027c01f0def461815786e076ef4ae65b7b2f9b"
    }
   },

注意:container2 在兩個網(wǎng)絡中間,它加入了默認bridge網(wǎng)絡,當你在創(chuàng)建它的時候,然后又連接它到了isolation_nw.

一個容器可以連接到多個網(wǎng)絡中

Docker 網(wǎng)絡命令詳解

7.使用docker attach 命令連接一個正在運行的容器,然后查看

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
zane@zane-v:~$ docker attach container2
/ # ifconfig -a
eth1  link encap:ethernet hwaddr 02:42:ac:19:00:02
   inet addr:172.25.0.2 bcast:0.0.0.0 mask:255.255.0.0
   inet6 addr: fe80::42:acff:fe19:2/64 scope:link
   up broadcast running multicast mtu:1500 metric:1
   rx packets:86 errors:0 dropped:0 overruns:0 frame:0
   tx packets:8 errors:0 dropped:0 overruns:0 carrier:0
   collisions:0 txqueuelen:0
   rx bytes:11780 (11.5 kib) tx bytes:648 (648.0 b)
 
eth2  link encap:ethernet hwaddr 02:42:ac:11:00:03
   inet addr:172.17.0.3 bcast:0.0.0.0 mask:255.255.0.0
   inet6 addr: fe80::42:acff:fe11:3/64 scope:link
   up broadcast running multicast mtu:1500 metric:1
   rx packets:23 errors:0 dropped:0 overruns:0 frame:0
   tx packets:8 errors:0 dropped:0 overruns:0 carrier:0
   collisions:0 txqueuelen:0
   rx bytes:3809 (3.7 kib) tx bytes:648 (648.0 b)
 
lo  link encap:local loopback
   inet addr:127.0.0.1 mask:255.0.0.0
   inet6 addr: ::1/128 scope:host
   up loopback running mtu:65536 metric:1
   rx packets:0 errors:0 dropped:0 overruns:0 frame:0
   tx packets:0 errors:0 dropped:0 overruns:0 carrier:0
   collisions:0 txqueuelen:0
   rx bytes:0 (0.0 b) tx bytes:0 (0.0 b)

8.可以通過容器名稱來相互連接

?
1
2
3
4
5
6
7
/ # ping -w 4 container3
ping container3 (172.25.3.3): 56 data bytes
64 bytes from 172.25.3.3: seq=0 ttl=64 time=0.077 ms
64 bytes from 172.25.3.3: seq=1 ttl=64 time=0.049 ms
64 bytes from 172.25.3.3: seq=2 ttl=64 time=0.047 ms
64 bytes from 172.25.3.3: seq=3 ttl=64 time=0.054 ms

雖然container1 和 container2 都在bridge網(wǎng)絡中,但是他們是不支持 容器名稱通信的。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
zane@zane-v:~$ docker attach container2
/ # ping container3
ping container3 (172.25.3.3): 56 data bytes
64 bytes from 172.25.3.3: seq=0 ttl=64 time=0.042 ms
64 bytes from 172.25.3.3: seq=1 ttl=64 time=0.050 ms
64 bytes from 172.25.3.3: seq=2 ttl=64 time=0.063 ms
--- container3 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.042/0.051/0.063 ms
 
/ # ping -w 4 container1
ping: bad address 'container1'
 
/ # ping -w 4 172.17.0.2
ping 172.17.0.2 (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.104 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.052 ms
64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.127 ms
64 bytes from 172.17.0.2: seq=3 ttl=64 time=0.057 ms
 
--- 172.17.0.2 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.052/0.085/0.127 ms

注意退出attach 時,使用ctr-p + ctr-q.
如果使用ctr-d 則會stop container.

?
1
2
3
4
5
6
zane@zane-v:~$ docker attach container3
/ # ping -w 4 172.17.0.2
ping 172.17.0.2 (172.17.0.2): 56 data bytes
 
--- 172.17.0.2 ping statistics ---
4 packets transmitted, 0 packets received, 100% packet loss

上面的實驗我們知道,用戶自定義的網(wǎng)絡,是可以相互解析容器名的,也就是可以用容器名來相互同行。

•定義網(wǎng)絡別名 •--link=container-name:alias

1.斷開container2和isolated_nw的連接,然后

?
1
2
3
zane@zane-v:~$ docker network disconnect isolated_nw container2
 
zane@zane-v:~$ docker network rm simple-network

•創(chuàng)建網(wǎng)絡 •docker network create simple-network

•overlay網(wǎng)絡條件 •進入一個鍵值存儲

•支持overlay網(wǎng)絡的docker選項 •--cluser-store

•指定子網(wǎng)絡,網(wǎng)關,地址范圍

•將容器添加到網(wǎng)絡中 •docker network connect isolated_nw container2

•連接一個正在運行的容器 •docker attach

•attach 的退出 •ctr p + ctr q

•默認bridge網(wǎng)絡不支持,容器名稱通信,其他網(wǎng)絡支持; •使用link 來支持默認網(wǎng)絡的容器名稱通信

•斷開連接

•docker network disconnect isolated_nw container2

•刪除網(wǎng)絡

•docker network rm simple-network

•檢測網(wǎng)絡

•docker network inspect isolated_nw

原文鏈接:http://www.cnblogs.com/Aiapple/p/6991606.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本免费久久久久久久网站 | 99久久国产综合精品网成人影院 | 日本一区二区免费在线 | 牛人国产偷窥女洗浴在线观看 | 亚洲欧美优优色在线影院 | a优女网| 女仆色永久免费网站 | 美女和男生搞基 | 久久精品视在线观看2 | 欧美一卡二卡科技有限公司 | 男女精品视频 | 青春草在线观看精品免费视频 | 婷婷色网 | 免费看片黄色 | 亚洲娇小性hd | www91在线观看| 日本一区免费观看 | 精品久久久久久久久久久久久久久 | 精品AV亚洲乱码一区二区 | 香艳69xxxxx有声小说 | 视频网站入口在线看 | 99久久伊人一区二区yy5099 | 好大好深好舒服 | 国产成人精品男人的天堂538 | 精品乱lun小说 | 亚洲精品国产综合久久一线 | 国产欧美精品专区一区二区 | 精品久久久久久无码人妻国产馆 | 精品久久一区 | 国产成人精品一区二三区在线观看 | 国产人成激情视频在线观看 | 久久精品午夜一区二区福利 | 天天澡夜夜澡狠狠澡 | 午夜神器老司机高清无码 | 99久久99热久久精品免 | 四虎影库网址 | 高清日韩在线 | 北岛玲亚洲一区在线观看 | 欧美日韩免费一区二区在线观看 | 午夜十八岁禁 | 国产精品亚洲午夜不卡 |