简体   繁体   English

Docker网络从默认桥移动

[英]Docker Network moving from the default bridge

I have a bunch of docker containers running on the default bridge network, that need to communicate with each other. 我有一堆在默认网桥网络上运行的docker容器,它们需要相互通信。

I want to move some of the containers to a separate user defined network so I can specify their IP addresses. 我想将一些容器移动到单独的用户定义的网络,以便可以指定其IP地址。

Is there any way to do this without having to take down/replicate all the containers and move them to the other network, or is this the only way? 有什么方法可以做到而不必删除/复制所有容器并将它们移到另一个网络,还是唯一的方法?

It's possible to create networks and connect containers while they are live. 可以在网络处于活动状态时创建网络并连接容器。 You may still need to stop/start processes if the process is listening on specific a IP addresses rather than all interfaces ( * or :: ) 如果进程正在侦听特定的IP地址而不是所有接口( *:: :),则可能仍需要停止/启动进程

Create a network 创建一个网络

docker network create \
  --driver=bridge \
  --subnet=192.168.38.0/24 \
  --gateway=172.16.238.1 \
  <NETWORK> 

Connect a container 连接容器

docker network connect \
  --ip 192.168.38.14 \
  <NETWORK> \
  <CONTAINER>

Disconnect from original network 断开与原始网络的连接

 docker network disconnect <OLDNETWORK> <CONTAINER>

Example

Before the containers eth0 is on the default bridge network 容器eth0在默认网桥网络上之前

→ docker exec $CONTAINER ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
15: eth0@if16: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

Afterwards, eth1 has been added and no more eth0 之后,添加了eth1 ,不再添加eth0

→ docker exec $CONTAINER ip ad sh
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue 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
17: eth1@if18: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue 
    link/ether 02:42:c0:a8:26:0e brd ff:ff:ff:ff:ff:ff
    inet 192.168.38.14/24 brd 192.168.38.255 scope global eth1
       valid_lft forever preferred_lft forever

You also should think about using a docker compose. 您还应该考虑使用docker compose。 It will create a network automatically, with its own DNS, allowing the containers to be connected. 它将自动创建一个具有自己的DNS的网络,以允许连接容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM