-
Notifications
You must be signed in to change notification settings - Fork 0
Networking
PotatoScript edited this page Feb 16, 2025
·
1 revision
Docker networking enables communication between containers and the outside world. Containers can communicate with each other, the host machine, and external networks using Docker’s built-in networking capabilities.
| Driver | Description |
|---|---|
bridge |
Default driver for single-host container networking. Containers can communicate using an internal bridge. |
host |
Removes the network isolation; the container uses the host's network stack directly. |
none |
Disables networking for the container. |
overlay |
Allows communication between containers across multiple hosts in a swarm. |
macvlan |
Assigns a MAC address to the container, making it appear as a physical device. |
| Command | Description |
|---|---|
docker network ls |
List all available networks. |
docker network inspect <name> |
Display details about a specific network. |
docker network create <name> |
Create a custom network. |
docker network rm <name> |
Remove a specific network. |
docker network prune |
Remove all unused networks. |
- Create a network:
docker network create my_network
- Run a container and connect it to the network:
docker run -d --name my_container --network my_network my_image
- Automatically created on the host.
- Containers can communicate with each other using container names.
- Example:
docker run -d --name app1 --network bridge nginx docker run -d --name app2 --network bridge nginx docker exec app1 ping app2
- Shares the host’s network stack.
- Useful for performance-critical applications.
- Example:
docker run --network host nginx
- Disables all networking.
- Useful for isolated workloads.
- Example:
docker run --network none nginx
- Enables communication between containers on different Docker hosts in a swarm.
- Example:
docker network create -d overlay my_overlay
- Assigns a MAC address to the container.
- Example:
docker network create -d macvlan \ --subnet=192.168.1.0/24 \ --gateway=192.168.1.1 \ -o parent=eth0 my_macvlan
version: '3.8'
services:
app:
image: my_app
networks:
- frontend
- backend
db:
image: postgres
networks:
- backend
networks:
frontend:
backend:docker network connect my_network my_containerdocker network disconnect my_network my_container- Create a custom network:
docker network create my_network
- Start two containers on the same network:
docker run -d --name app1 --network my_network alpine sleep 3600 docker run -d --name app2 --network my_network alpine sleep 3600
- Test communication:
docker exec app1 ping app2
docker run -d -p 8080:80 nginx-
-p 8080:80: Maps port 80 in the container to port 8080 on the host.
docker network inspect my_networkUse tools like curl or ping to verify connectivity:
docker exec my_container curl http://app2docker network prune-
Use Custom Networks: Prefer custom networks over the default
bridgefor better isolation and control. - Name Your Networks: Use meaningful names to identify networks easily.
-
Secure Sensitive Containers: Use the
nonenetwork or restrict access using firewalls. -
Leverage
docker-compose: Define and manage networks for multi-container applications indocker-compose.yml.