Modify docker's default network card (bridge)

  • The default network card of the docker engine is docker0. When systemctl start docker starts the container server, a network card device with a network segment of 172.17.10.1/24 will be generated by default.
  • docker0 is not only a virtual network card, but also a virtual switch
    Network card: realizes its communication with the host
    virtual switch: Add its gateway to the container, so that the container can communicate with the container, and the container and the external network

insert image description here
The eth0 at the bottom of the figure is the external network card device of this machine

How to add a custom network card?

1. Obtain the local network card ifconfig
insert image description here
2. Manually create a virtual network card (bridge)
cd /etc/sysconfig/network-scripts
cp ifcfg-ens33 ifcfg-br0
[root@node2 network-scripts]# vi ifcfg-br0
TYPE= "Bridge"
Bridge = "BR0"
Bootproto = Static
Defroute = YES
NAME = BR0
Device = BR0
Onbook = YES
iPaddr = 192.168.10.135
Gateway = 192.168.10.2
Netmask = 255.255.255.0
DNS1 = 8.8.8.8
3. Start new network card ( Bridge) ifconfig br0 up
4. Modify the docker.service file and add the parameter of -b br0
[root@node2 network-scripts]# vi /usr/lib/systemd/system/docker.service

ExecStart=/usr/bin/dockerd-current -b br0
specifies that the network card when starting the service is br0
5. Make the parameters take effect:
systemctl daemon-reload
systemctl restart docker
6. Check the network, and br0 takes effect:

[root@node2 ~]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        ether 76:3e:f8:46:5e:e1  txqueuelen 1000  (Ethernet)
        RX packets 16  bytes 1088 (1.0 KiB)
        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

docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.10.1  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::42:4aff:fe6b:8061  prefixlen 64  scopeid 0x20<link>
        ether 02:42:4a:6b:80:61  txqueuelen 0  (Ethernet)
        RX packets 24  bytes 1632 (1.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 656 (656.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.135  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::9b50:e592:a2e1:5127  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:26:02:e6  txqueuelen 1000  (Ethernet)
        RX packets 862  bytes 69807 (68.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0

6. Start a new container

[root@node2 ~]# docker run -itd --name=nginx4 192.168.10.135:5000/nginx:v1
8bb095b460888d2519101bdcb7b47a617a55682b6d97ab4529c1cdbf8d458532

Check that the IP of the container is the network segment of the br0 network card:

[root@node2 docker-static-ip]# docker inspect nginx4|grep -i ipa
            "SecondaryIPAddresses": null,
            "IPAddress": "172.18.0.3"

Guess you like

Origin blog.csdn.net/weixin_42808782/article/details/116210960