Docker容器从默认bridge网络迁移至自定义网络的无损方案咨询
Great question! Let's break this down clearly, since Docker's network model has specific constraints you need to know about.
First, the core truth: You can't migrate a running Docker container to a custom network with a specified IP address without stopping that specific container — but you absolutely don't need to shut down all your containers or copy them to new instances. Only the containers you want to move require a brief stop/restart.
Here's the step-by-step process to do this without copying containers:
Create your custom network (if you haven't already)
To assign fixed IPs, you need to define a subnet when creating the network. Run this command:docker network create --subnet=192.168.50.0/24 my_custom_networkSwap out the subnet and network name with your preferred values.
Stop the container you want to migrate
Docker doesn't let you modify network configurations (like setting a fixed IP) for running containers, so you'll need to pause the target container temporarily:docker stop <container_name_or_id>Remember: only the containers you're moving need this — your other bridge-network containers can keep running uninterrupted.
Connect the stopped container to your custom network with a fixed IP
Use thedocker network connectcommand to attach the container to your new network and specify its desired IP:docker network connect --ip 192.168.50.10 my_custom_network <container_name_or_id>Use an address from the subnet you defined in step 1.
(Optional) Disconnect the container from the default bridge network
If you want full isolation from the default bridge (recommended for most use cases), run this while the container is still stopped:docker network disconnect bridge <container_name_or_id>Restart the container
Fire the container back up — it’ll now use the custom network and fixed IP you configured:docker start <container_name_or_id>
What if downtime isn't an option?
If you can’t tolerate even a short stop for the target container, you’ll need a "seamless migration" approach (which involves copying the container’s state):
- Spin up a new container attached to your custom network with the fixed IP.
- Mount the same volumes, environment variables, and configs from the original container to the new one.
- Redirect traffic to the new container, then shut down the original.
This is only necessary for the specific container you can’t stop — you won’t need to touch the rest of your containers.
Quick side note on running containers
You can technically add a running container to a custom network without specifying an IP using docker network connect my_custom_network <container_id>, but this assigns a random IP from the network’s subnet. Since you want a specified IP, this workaround won’t work — stopping the container is mandatory for fixed IP assignment.
内容的提问来源于stack exchange,提问作者Kartik Prasad

