docker_networking
docker_networking
Docker networking allows containers to communicate with each other, with the host, and
with external networks. Docker provides different networking drivers for various use cases.
---
### Example: Running Two Containers on the Same Host in a Bridge Network
```sh
docker network create my_bridge_network
docker run -d --name container1 --network my_bridge_network nginx
docker run -d --name container2 --network my_bridge_network alpine sleep 3600
docker exec -it container2 ping container1
```
---
### Example: Deploying Containers Across Multiple Hosts Using an Overlay Network
```sh
docker swarm init --advertise-addr <MANAGER-IP>
docker network create --driver overlay my_overlay_network
docker swarm join --token <SWARM-TOKEN> <MANAGER-IP>:2377
docker service create --name web --network my_overlay_network -p 80:80 nginx
docker network inspect my_overlay_network
docker service ls
docker service ps web
```
---
This approach ensures **security and isolation**, preventing direct access to the database
from external sources while allowing the web service to communicate with it.