您好,登錄后才能下訂單哦!
創(chuàng)建一個(gè)沒有網(wǎng)絡(luò)配置的容器
[root@localhost ~]# docker run -i -t --rm --net=none alpine sh
/ #
打開另一個(gè)終端
查看容器id
[root@localhost ~]# docker ps -a
查找進(jìn)程id
[root@localhost ~]# docker inspect -f '{{.State.Pid}}' 2aefc41dbdeb
9076
[root@localhost ~]# pid=9076
創(chuàng)建命名空間
[root@localhost ~]# mkdir -p /var/run/netns
[root@localhost ~]# ln -s /proc/$pid/ns/net /var/run/netns/$pid
查看橋接網(wǎng)卡的IP 和子網(wǎng)掩碼信息
[root@localhost ~]# ip addr show docker0
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:8e:e7:64:4a brd ff:ff:ff:ff:ff:ff
inet 172.18.0.1/16 brd 172.18.255.255 scope global docker0
valid_lft forever preferred_lft forever
創(chuàng)建一對"veth pair"接口A和B
[root@localhost ~]# sudo ip link add A type veth peer name B
綁定A接口到網(wǎng)橋docker0
[root@localhost ~]# sudo brctl addif docker0 A
并啟用它
[root@localhost ~]# sudo ip link set A up
將B 接口放到容器的網(wǎng)絡(luò)命名空間
[root@localhost ~]# sudo ip link set B netns $pid
命名為eth0
[root@localhost ~]# sudo ip netns exec $pid ip link set dev B name eth0
開啟網(wǎng)絡(luò)接口B
[root@localhost ~]# sudo ip netns exec $pid ip link set eth0 up
配置一個(gè)可用IP(橋接網(wǎng)段)
[root@localhost ~]# sudo ip netns exec $pid ip addr add 172.18.0.99/16 dev eth0
配置默認(rèn)網(wǎng)關(guān)
[root@localhost ~]# sudo ip netns exec $pid ip route add default via 172.18.0.1
最后回到容器檢查一下網(wǎng)卡配置
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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
18: eth0@if19: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP qlen 1000
link/ether 6e:b9:75:8e:f4:99 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.99/16 scope global eth0
valid_lft forever preferred_lft forever
/ # ping baidu.com
PING baidu.com (123.125.114.144): 56 data bytes
64 bytes from 123.125.114.144: seq=0 ttl=53 time=2.827 ms
64 bytes from 123.125.114.144: seq=1 ttl=53 time=2.900 ms
^C
--- baidu.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 2.827/2.863/2.900 ms
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。