Introduction
In this hands-on lab, you will explore Docker networking fundamentals by performing tasks that involve creating and managing Docker networks. You will use the Nano editor in a Linux environment to edit configuration files and Docker commands to manipulate Docker networks and container connections. This lab provides a practical introduction to Docker's networking capabilities, allowing you to understand how containers can communicate within the same host and across different networks.
Objectives
Lab Steps
Step 1: Preparing Your Environment
sudo apt-get install nano or the appropriate package manager command for your Linux distribution.Step 2: Creating a Custom Bridge Network
my-bridge-net:docker network create --driver bridge my-bridge-net
docker network ls
Step 3: Running Containers and Connecting to the Network
my-bridge-net. These containers will remain idle and open for interactive commands:docker run -dit --name alpine1 --network my-bridge-net alpine ash
docker run -dit --name alpine2 --network my-bridge-net alpine ash
Step 4: Inspecting Network Configuration
my-bridge-net to view the connected containers and network details:docker network inspect my-bridge-net
Step 5: Communication Between Containers
docker exec to ping alpine2 from alpine1 using its container name, demonstrating inter-container communication within the same network:docker exec alpine1 ping -c 4 alpine2
Step 6: Editing Configuration with Nano
alpine1:docker exec -it alpine1 vi /ping_script.sh
ESC, :x):#!/bin/sh
ping -c 4 alpine2
docker exec alpine1 chmod +x /ping_script.sh
docker exec alpine1 /ping_script.sh
You should see the following output:
PING alpine2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.071 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.090 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.091 ms
64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.090 ms
--- alpine2 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.071/0.085/0.091 ms
Summary
In this lab, you've gained hands-on experience with Docker networking by creating a custom bridge network and connecting containers to it. You explored how containers can communicate with each other on the same network and used the Nano editor to edit a simple script within a container. This lab has demonstrated the power and flexibility of Docker networking, providing a foundation for more complex networking scenarios and configurations. Understanding Docker networking is essential for building and managing containerized applications effectively.