You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器从默认bridge网络迁移至自定义网络的无损方案咨询

Answer

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:

  1. 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_network
    

    Swap out the subnet and network name with your preferred values.

  2. 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.

  3. Connect the stopped container to your custom network with a fixed IP
    Use the docker network connect command 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.

  4. (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>
    
  5. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:25:42