Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker manually configure the network

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Create a container without network configuration

[root@localhost] # docker run-I-t-- rm-- net=none alpine sh/ #

Open another terminal

View Container id

[root@localhost] # docker ps-a

Find process id

[root@localhost ~] # docker inspect-f'{{.State.Pid}} '2aefc41dbdeb 9076 [root@localhost ~] # pid=9076

Create a namespace

[root@localhost ~] # mkdir-p / var/run/netns [root@localhost ~] # ln-s / proc/$pid/ns/net / var/run/netns/$pid

View the IP and subnet mask information of the bridged Nic

[root@localhost ~] # ip addr show docker05: docker0: 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 scope global docker0 valid_lft forever preferred_lft forever 16 brd 172.18.255.255 scope global docker0 valid_lft forever preferred_lft forever

Create a pair of "veth pair" interfaces An and B

[root@localhost ~] # sudo ip link add A type veth peer name B

Bind interface A to bridge docker0

[root@localhost ~] # sudo brctl addif docker0 A

And enable it.

[root@localhost ~] # sudo ip link set A up

Put interface B into the network namespace of the container

[root@localhost ~] # sudo ip link set B netns $pid

Named eth0

[root@localhost ~] # sudo ip netns exec $pid ip link set dev B name eth0

Turn on network interface B

[root@localhost ~] # sudo ip netns exec $pid ip link set eth0 up

Configure an available IP (bridged segment)

[root@localhost ~] # sudo ip netns exec $pid ip addr add 172.18.0.99 16 dev eth0

Configure the default gateway

[root@localhost ~] # sudo ip netns exec $pid ip route add default via 172.18.0.1

Finally, go back to the container to check the network card configuration.

/ # ip a1: lo: 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 forever18: eth0@if19: 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.comPING baidu.com (123.125.114.144): 56 data bytes64 bytes from 123.125.114.144: seq=0 ttl=53 time=2.827 ms64 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 lossround-trip min/avg/max = 2.827 ms 2.863 ms

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers