The “docker network” command in Docker allows you to create and manage networks for your Docker containers. With Docker networking, you can establish communication and connectivity between containers, enabling them to interact with each other and external resources.
Here are some key functionalities and operations provided by the “docker network” command:
- Create a Network: You can use the “docker network create” command to create a new network. Specify a name for the network, and Docker will create a bridge network by default. Bridge networks are the most common type of network used in Docker, providing isolation and connectivity among containers on the same host.
- List Networks: To view the existing networks in your Docker environment, you can use the “docker network ls” command. It will display a list of all the networks along with their respective IDs, names, and drivers.
- Inspect a Network: The “docker network inspect” command allows you to retrieve detailed information about a specific network. By providing the network name or ID, you can get insights into its configuration, subnet, IP address range, and connected containers.
- Connect Containers to a Network: To connect a container to a specific network, you can use the “docker network connect” command. Specify the network name and the container ID or name, and Docker will establish network connectivity between them. This enables containers to communicate with each other using their assigned network aliases.
- Disconnect Containers from a Network: If you no longer need a container to be connected to a network, you can disconnect it using the “docker network disconnect” command. Specify the network name and the container ID or name, and Docker will remove the network connection.
- Remove a Network: To remove a network that is no longer required, you can use the “docker network rm” command followed by the network name or ID. This will delete the network, disconnecting all containers associated with it.
Docker provides various types of networks, such as bridge, host, overlay, and MACVLAN, each with its own use cases and capabilities. By leveraging Docker networking features, you can create flexible and scalable architectures for your containerized applications.
Remember to run Docker commands with the appropriate permissions, such as using sudo or being a member of the Docker group, depending on your system configuration.
docker network Command Examples
1. List all available and configured networks on docker daemon:
# docker network ls
2. Create a user-defined network:
# docker network create --driver driver_name network_name
3. Display detailed information of a space-separated list of networks:
# docker network inspect network_name
4. Connect a container to a network using a name or ID:
# docker network connect [network_name container_name|ID]
5. Disconnect a container from a network:
# docker network disconnect network_name [container_name|ID]
6. Remove all unused (not referenced by any container) networks:
# docker network prune
7. Remove a space-separated list of unused networks:
# docker network rm network_name