docker network
The docker network command is used to manage Docker networks. Docker networks allow containers to communicate with each other and with the outside world. This command provides various subcommands to create, inspect, list, and remove networks.
Basic Syntax
Subcommands and Options
create: Create a new network.
docker network create [OPTIONS] NETWORKOptions:
-d, --driver: Driver to manage the network (default is
bridge).docker network create -d bridge mynetwork--subnet: Subnet in CIDR format.
docker network create --subnet 192.168.1.0/24 mynetwork--gateway: IPv4 or IPv6 gateway for the master subnet.
docker network create --gateway 192.168.1.1 mynetwork
inspect: Display detailed information on one or more networks.
docker network inspect NETWORKls: List all networks.
docker network lsrm: Remove one or more networks.
docker network rm NETWORKconnect: Connect a container to a network.
docker network connect [OPTIONS] NETWORK CONTAINERdisconnect: Disconnect a container from a network.
docker network disconnect [OPTIONS] NETWORK CONTAINER
Examples
Creating a Network
docker network create mynetworkThis command creates a network named
mynetworkwith the defaultbridgedriver.Creating a Network with a Specific Subnet and Gateway
docker network create --subnet 192.168.1.0/24 --gateway 192.168.1.1 mynetworkThis command creates a network named
mynetworkwith the specified subnet and gateway.Listing All Networks
docker network lsThis command lists all the networks available on the Docker host.
Inspecting a Network
docker network inspect mynetworkThis command displays detailed information about the network named
mynetwork.Removing a Network
docker network rm mynetworkThis command removes the network named
mynetwork.Connecting a Container to a Network
docker network connect mynetwork mycontainerThis command connects the container named
mycontainerto the network namedmynetwork.Disconnecting a Container from a Network
docker network disconnect mynetwork mycontainerThis command disconnects the container named
mycontainerfrom the network namedmynetwork.
Conclusion
The docker network command is essential for managing Docker networks. Understanding its subcommands and options allows you to effectively create, inspect, list, and remove networks, as well as connect and disconnect containers to and from networks.