container_networking
Docker Container Networking
This guide explores various aspects of Docker container networking, from inspecting IP addresses to creating custom networks, exposing ports, and using Docker Compose for multi-container setups.
(Assumption: The examples use a custom Docker image named hackon:v1. If this image is not available, you can substitute it with a standard image like alpine or busybox and install necessary tools like iproute2 for ip a, curl, or siege if needed.)
1. Basic Network Commands and Concepts
1.1 Check a Container's IP Address
Run a container and check its IP address using the ip a command within the container.
docker run -it --rm hackon:v1 ip a(Note: --rm is added to automatically remove the container when it exits.)
1.2 Create and Use a Custom Docker Network
User-defined bridge networks are recommended for communication between containers.
Create a new Docker network:
docker network create my-netRun a container in the new network: Containers on the same user-defined network can resolve each other by name.
docker run -it --rm --network my-net hackon:v1 ip a
1.3 Attach a Running Container to Multiple Networks
A container can be connected to more than one network.
Create another network and a container (initially not started):
(Note: Removed
--rmfromcreateas it's not applicable here. Addedbashas CMD to keep it alive.)Connect the container to networks:
Start the container and inspect its interfaces:
1.4 Using Host Network Mode
A container can share the host's network stack. This means it uses the host's IP address and port space.
Once inside the container's shell (hackon-host), you can run commands like:
(Clarified that ip a and ps are run inside the container. Clarified ps aux behavior with/without --pid=host.)
The comment "Maybe time to TCPdump correctly ..." suggests using tcpdump in such a container to see host traffic.
2. Inter-Container Communication and Exposing Ports
2.1 Communication on the Default Bridge Network
Let's create webservers and generate traffic. By default, containers (without a specified --network) are often attached to a default bridge network.
Start an Nginx web server:
Inspect the Nginx container to find its IP address on the default bridge:(This IP is internal to Docker and can change.)
Generate traffic from another container using the IP:(Replace
172.17.0.2with the actual IP found fromdocker inspect www.)Note: For reliable communication, user-defined networks are preferred, allowing resolution by container name (e.g.,
http://www).
2.2 Exposing Container Ports to the Host (--publish)
--publish)To make a container's service reachable from the host machine or externally:
Publish a specific port (host:container): This maps port 80 on the host to port 80 in the
www2container.You can now access this Nginx instance via
http://localhost:80orhttp://<your-host-ip>:80.Publish all exposed ports to random host ports (
-P): This maps all ports EXPOSEd in the Nginx Dockerfile (port 80) to random high-numbered ports on the host.Inspect
www3to find the dynamically assigned host port:Accessing published ports: Assuming
www2is on port 80 andwww3was assigned, for example, port32768by Docker:From a browser on your network (replace
studentx-4.training.dockerhack.mewith your host's actual DNS name or IP):
3. Lab: Sniffing Traffic with tcpdump
This section outlines a small exercise for practicing with tcpdump.
Create a user-defined network, e.g.,
tcpdump-lab:Run an Nginx container on this network:
Run a
hackon:v1(or similar) container on the sametcpdump-labnetwork. Can it reach the Nginx container by name (e.g.,curl http://webserver)?Try to use
tcpdump:Can you see all traffic on the host? (Requires
tcpdumpon the host or in a--network=hostcontainer).Can you see inter-container traffic on the
tcpdump-labnetwork? (e.g., using atcpdumpcontainer attached to this network, or on the host inspecting thetcpdump-labbridge interface).Can you see traffic inside a specific container (e.g.,
webserveror thehackon:v1client)? (e.g., usingdocker exec webserver tcpdump ...if tcpdump is installed in it, or a container in--network container:webservermode).
Refer to external guides for detailed tcpdump usage with Docker, such as:How To TCPDump Effectively in Docker (Link from original document)
Appendix: Using Docker Compose
If you want to avoid starting multiple containers manually all the time, you can use Docker Compose.
Install Docker Compose:(Note:
apt install docker-composeinstalls an older version. Modern Docker Desktop and Docker Engine often includedocker compose(with a space) as a plugin. Refer to official Docker documentation for current installation methods.)Create a
docker-compose.yamlfile: In a separate directory, create a file nameddocker-compose.yaml:(Updated version, clarified port mapping, added depends_on and more specific siege command)
Start the application: (Assuming your file is named
docker-compose.yamlordocker-compose.ymlin the current directory)Check running containers and networks:
Stop the application: This will stop and remove containers, and networks created by Compose.
Verify:
Last updated