Appendix_B_tcpdump_lab
Appendix B: Docker Network Sniffing with tcpdump Lab
This lab demonstrates how to use tcpdump to inspect network traffic within Docker environments. We will create a custom Docker image containing tcpdump, set up a simple web server, generate some traffic, and then use tcpdump to sniff that traffic.
1. Create a Custom tcpdump Image
Create a
Dockerfile: Create a file namedDockerfile(no extension) in a new directory with the following content:FROM ubuntu RUN apt-get update && apt-get install -y tcpdump # Default command captures traffic on eth0. # You can override this or add more specific tcpdump arguments at runtime. CMD ["tcpdump", "-i", "eth0", "-n", "-s0", "-A"](Changed CMD to JSON format for clarity and added some common useful flags: -n to disable name resolution, -s0 to capture full packets, -A to print ASCII.)
Build the Docker image: Navigate to the directory containing your
Dockerfileand run:docker build -t my-tcpdump .This creates an image named
my-tcpdumpthat we can use later.
2. Set Up a Demo Network and Service
Create a Docker network: This allows containers to communicate with each other by name.
docker network create demo-netRun an Nginx web server: This will be our target service to generate traffic to.
docker run -d --network demo-net --name wwwnginx nginx
3. Generate Traffic
Use a tool like siege (or even curl from another container on demo-net) to send HTTP requests to the Nginx server. The original example used dockersec/siege.
(Added --rm to siege/curl containers for auto-cleanup. Added note about siege image and alternative with curl.)
4. Sniffing Traffic
Now, we'll use our my-tcpdump image to inspect traffic on the wwwnginx container's network interface.
Sniff all traffic on
wwwnginx: This command runstcpdumpfrom ourmy-tcpdumpimage but attaches it to the network namespace of thewwwnginxcontainer.(Used
my-tcpdumpimage. Added--rmfor auto-cleanup.) You should see the traffic generated in step 3. PressCtrl+Cto stop.Sniff specific traffic (e.g., port 80): You can pass arguments to
tcpdumpto filter the captured traffic. Since ourDockerfile'sCMDalready includestcpdumpand some flags, we just add the filter expression.(Corrected the double
tcpdumpand usedmy-tcpdumpimage. The argumentsport 80will be appended to the CMD in the Dockerfile.)Alternatively, if you wanted to completely override the
Dockerfile'sCMDand provide alltcpdumparguments:(Added an example of how to override CMD if needed.)
Last updated