Docker Network Modes Command Guide
In Docker, networking refers to the communication methods and rules between Docker containers and between Docker containers and external networks. Within Docker, you can configure networks to enable inter-container connectivity as well as container-to-external-network communication. Docker provides several network modes that allow you to select a mode based on specific requirements:
- host: In this mode, the Docker container shares the same network namespace with the host machine and uses the host's network configuration. This mode is suitable for applications where maximum network performance is required.
- bridge: This is the default networking mode in Docker. Each Docker container is assigned a unique IP address, and a virtual network bridge interface is created on a virtual network bridge device. This mode is ideal for applications that require communication between containers.
- none: In this mode, no network resources are allocated to the Docker container; it cannot communicate with other containers or external networks.
- overlay: This mode is used to create cross-host networking across multiple Docker hosts. Containers can communicate across different hosts even if they reside on separate machines.
In addition to these modes, Docker supports custom networks. Through custom networks, you can define specific network configurations tailored to your needs—for example, creating a network that allows communication only between containers or establishing a public network permitting container access from external sources.
Related Commands
Docker provides a series of commands for managing networking within the environment. Below are some commonly used Docker network-related commands:
docker network ls: Lists all existing Docker networks.docker network create: Creates a new Docker network.
Example:docker network create my-networkdocker network inspect: Displays detailed information about a specific Docker network.
Example:docker network inspect my-networkdocker network connect: Connects an existing Docker container to a specified Docker network.
Example:docker network connect my-network my-containerdocker network disconnect: Disconnects a Docker container from its associated Docker network.
Example:docker network disconnect my-network my-containerdocker network rm: Removes (deletes) an existing Docker network.
Example:docker network rm my-networkdocker network prune: Deletes all unused networks that are not attached to any containers or images.
Example:docker network prune