This project demonstrates a more advanced approach to setting up a Docker network using a bridge interface with additional features like container names and optional NAT for internet access.
- Debian-based Linux distribution
- Docker installed and running
- Essential tools: bridge-utils, iptables, net-tools, tcpdump
-
Download the Script: Download the script to your local machine.
-
Make it Executable: Ensure the script is executable by running the following command:
chmod +x docker_networking_in_time.sh
-
Execute the Script: Run the script with sudo privileges:
sudo ./docker_networking_in_time.sh
-
Update package lists and install required tools:
- The script starts by updating package lists and installing essential tools:
bridge-utils: For managing bridge interfaces.iptables: For firewall configuration (optional).net-tools: For basic networking utilities.tcpdump: For network traffic capturing (optional).
- The script starts by updating package lists and installing essential tools:
-
Create bridge interface:
sudo ip link add name br0 type bridgecreates a new bridge interface namedbr0.
-
Assign IP address to the bridge:
sudo ip addr add 192.168.1.1/24 dev br0assigns the IP address192.168.1.1with a subnet mask of/24to the bridge interface.
-
Bring up the bridge interface:
sudo ip link set dev br0 upactivates the bridge interface.
-
Create Docker network with the bridge interface:
docker network create --driver=bridge --subnet=192.168.1.0/24 --gateway=192.168.1.1 br0creates a Docker network namedbr0using the bridge driver, the specified subnet, and the bridge interface as the gateway.
-
Launch container 1 with assigned IP:
- Create veth pair:
ip link add name veth1 type veth peer name veth2creates a pair of virtual ethernet interfaces (veth1andveth2) for container 1.ip link set veth1 upandip link set veth2 upbring up both interfaces.
- Run container:
docker run -d --name nginx_container1 --network=br0 --ip=192.168.1.10 nginxlaunches a detached Nginx container namednginx_container1connected to thebr0network with the static IP192.168.1.10.docker network connect br0 container1explicitly connects the container to the network (optional, usually handled automatically).
- Attach veth interface to container and bridge:
ip addr add dev veth1 192.168.1.10/24assigns the IP address and subnet toveth1.ip link set veth1 master br0attachesveth1to the bridge interface, effectively connecting the container to the network.
- Create veth pair:
-
Launch container 2 with assigned IP (similar to container 1):
- Follow the same steps as for container 1, creating a veth pair (
veth3andveth4), running a container namednginx_container2with IP192.168.1.11, and attachingveth3to the bridge.
- Follow the same steps as for container 1, creating a veth pair (
-
Optional: Set up NAT for traffic forwarding:
- The script includes an optional section for setting up NAT (Network Address Translation) using
iptables. This allows containers to access the internet if your environment requires it. Adjust the configuration based on your specific network setup.
- The script includes an optional section for setting up NAT (Network Address Translation) using
-
Verify connectivity between containers:
docker exec nginx_container1 ping -c 3 192.168.1.11anddocker exec nginx_container2 ping -c 3 192.168.1.10commands check if the containers can ping each other, indicating successful network connectivity.
- Ensure Docker is installed and running before executing the script.
- The script assumes a Debian-based Linux distribution. Adjust package installation commands if using a different distribution.
- Customize IP addresses and container names as needed.
- This script is for my educational purposes only. Exercise caution in production environments and ensure proper network security measures are in place.
- Consider alternative methods for internet access within containers depending on your specific needs and security requirements.