Docker Tutorial

15. Docker Networking | Understanding Container Networks & Communication

Docker – Networking

Docker networking enables communication between containers, the host system, and external networks. By default, Docker provides isolated network environments for containers, but you can configure custom networks to manage communication and security efficiently.

Docker Networking Basics

Each Docker container connects to a virtual network. Docker supports several network types, allowing flexibility depending on your application needs. Networks allow containers to discover and communicate with each other without exposing services publicly.

Types of Docker Networks

  • Bridge Network: Default network for containers on a single host. Containers can communicate via IP addresses.
  • Host Network: Containers share the host’s network namespace. No isolation from the host.
  • Overlay Network: Connects multiple Docker daemons for multi-host container communication.
  • Macvlan Network: Containers get their own MAC address and appear as physical devices on the network.
  • None: Disables networking for the container completely.

Creating a Custom Network


# Create a bridge network
docker network create my-bridge-network

# List all networks
docker network ls

# Inspect network details
docker network inspect my-bridge-network

Running Containers on a Custom Network


# Run container on custom network
docker run -d --name web1 --network my-bridge-network nginx

docker run -d --name web2 --network my-bridge-network nginx

# Ping web1 from web2
docker exec -it web2 ping web1

Networking Between Host and Container

You can map container ports to host ports to allow external access using the -p option:


# Map container port 80 to host port 8080
docker run -d -p 8080:80 nginx

Best Practices for Docker Networking

  • Use custom bridge networks for better container isolation and discovery.
  • Assign meaningful network names for easier management.
  • Expose only necessary ports to the host for security.
  • Use overlay networks for multi-host applications.
  • Regularly inspect network settings for debugging and security auditing.

Conclusion

Docker networking is crucial for container communication and orchestration. Understanding network types, port mapping, and custom networks ensures secure, scalable, and reliable containerized applications.

Leave a Reply

Your email address will not be published. Required fields are marked *