How to Disconnect a Docker Container from a Network
Use the
docker network disconnect [NETWORK] [CONTAINER] command to disconnect a container from a Docker network. Replace [NETWORK] with the network name and [CONTAINER] with the container ID or name.Syntax
The command to disconnect a container from a network is:
docker network disconnect [OPTIONS] NETWORK CONTAINER
Here:
- NETWORK: The name or ID of the Docker network.
- CONTAINER: The name or ID of the container to disconnect.
- OPTIONS: Optional flags like
--forceto force disconnect.
bash
docker network disconnect [OPTIONS] NETWORK CONTAINER
Example
This example shows how to disconnect a container named my_container from a network called my_network.
bash
docker network disconnect my_network my_container
Common Pitfalls
- Trying to disconnect a container from a network it is not connected to will cause an error.
- Not using
--forcewhen disconnecting a container that is the last one on the network may fail if the network is in use. - Using container or network names incorrectly (typos) will cause the command to fail.
bash
docker network disconnect my_network wrong_container # Error: Error response from daemon: container wrong_container is not connected to network my_network docker network disconnect --force my_network my_container # Forces disconnection even if container is busy
Output
Error response from daemon: container wrong_container is not connected to network my_network
Quick Reference
Remember these tips when disconnecting containers from networks:
- Always verify the container is connected to the network first with
docker network inspect NETWORK. - Use
--forceif normal disconnect fails. - Check container and network names carefully to avoid typos.
Key Takeaways
Use
docker network disconnect NETWORK CONTAINER to remove a container from a network.Verify the container is connected to the network before disconnecting to avoid errors.
Use the
--force option to force disconnect if needed.Check names carefully to prevent command failures.
Disconnecting a container removes its network access on that network immediately.