使用docker SDK将两个不同网段的容器连接

使用docker SDK将两个不同网段的容器连接

docker 搭建两个不同网段的容器

root@OLTP-greatdb-1:~/文档/greatdb5.0.8/TestThreeIDC$ docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
b1c8e9afdf71   greatdb   "/bin/bash"   52 minutes ago   Up 52 minutes             haerbin_zone2_shard1_datanode1
823adf4d233c   greatdb   "/bin/bash"   52 minutes ago   Up 52 minutes             haerbin_zone2_shard0_datanode1
639fb45a362e   greatdb   "/bin/bash"   52 minutes ago   Up 52 minutes             haerbin_zone2_sqlnode1
fc53c5e85503   greatdb   "/bin/bash"   53 minutes ago   Up 53 minutes             beijing_zone1_shard1_datanode1
38ac4f32ff7e   greatdb   "/bin/bash"   54 minutes ago   Up 54 minutes             beijing_zone1_shard0_datanode1
31900df5edb8   greatdb   "/bin/bash"   54 minutes ago   Up 54 minutes             beijing_zone1_sqlnode1
2dadc1a62118   greatdb   "/bin/bash"   56 minutes ago   Up 56 minutes             beijing_zone0_shard1_datanode1
9f852b533b7e   greatdb   "/bin/bash"   57 minutes ago   Up 57 minutes             beijing_zone0_shard0_datanode1
8075582a6456   greatdb   "/bin/bash"   57 minutes ago   Up 57 minutes             beijing_zone0_sqlnode1

查看网卡

C$ docker exec -it beijing_zone0_sqlnode1 bash
[root@beijing_zone0_sqlnode1 ssd]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.18.0.2  netmask 255.255.0.0  broadcast 172.18.255.255
        ether 02:42:ac:12:00:02  txqueuelen 0  (Ethernet)
        RX packets 202711  bytes 51838722 (49.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 212664  bytes 22380459 (21.3 MiB)
        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 16165  bytes 854701 (834.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16165  bytes 854701 (834.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


$ docker exec -it haerbin_zone2_shard1_datanode1  ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.19.0.4  netmask 255.255.0.0  broadcast 172.19.255.255
        ether 02:42:ac:13:00:04  txqueuelen 0  (Ethernet)
        RX packets 47  bytes 7120 (6.9 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

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

代码如下:

from docker 


client = docker.from_env()


class TestFunction:
    def __init__(self):

        # self.setup()
        self.__addNetwork(networkName="beijing",
                          containerName="haerbin_zone2_shard0_datanode1")
     def __addNetwork(self,networkName: str = None, # 加入的网卡名称
                     containerName : str = None): # 加入到networkName网卡的容器名

        client.networks.get(networkName).connect(containerName)
                                 

即可,命令为```docker network connect <网卡名> <容器名>

上一篇:18.第六章 Shell脚本编程基础入门(一)


下一篇:2022-02-07每日刷题打卡