Docker Networking

Docker Networking

Docker revolutionized the way developers think about application deployment. Yet, to truly tap into Docker's potential, understanding Docker networking is vital. This blog delves into the intricacies of Docker networking and, with hands-on examples, uncovers how various containers communicate.

Unpacking Docker Network

Docker employs network bridges to enable communication between containers and between a container and its host. By default, when Docker starts, it creates a virtual bridge named docker0, and every new container connects to it.

There are primarily three types of Docker networks:

  1. Bridge Network (default)

  2. Host Network

  3. Overlay Network

Let's dive into each with examples!

To list available drivers in dockers(Docker Engine - Community edition) we use the following command:

docker network ls

1. Bridge Network

Concept: It’s the default network type. If no network is specified, Docker runs containers under the bridge network. Each container gets an internal IP address, and they can communicate with each other.

The Bridge Network Driver | Networking in Docker #6 - TechMormo

Example: Running two containers and communicating using the bridge network.

  1. Run Two Containers:

     docker run -d --name container1 busybox sleep 3600
     docker run -d --name container2 busybox sleep 3600
    
  2. Install ping and Test Communication:

     docker exec container1 apk add iputils
     docker exec container1 ping -c 3 container2
    

You'll see the ping responses, indicating container1 can reach container2.

2. Host Network

Concept: As discussed in a previous blog, the host network type allows the container to share the host's network stack.

The Host Network Driver | Networking in Docker #5 - TechMormo

Example: Running a web server on the host network.

  1. Run Nginx:

     docker run --rm -d --network host --name nginx_host_net nginx
    
  2. Access on Host: Just open a browser and go to http://localhost to see the Nginx welcome page.

3. Overlay Network

Concept: Essential for Docker Swarm, overlay networks enable communication between containers running on different host machines.

The Overlay Network Driver | Networking in Docker #7 - TechMormo

Example: This example requires Docker Swarm, so it's a bit advanced. We'll create an overlay network, run services attached to this network, and ensure they communicate.

  1. Initialize Docker Swarm:

     docker swarm init
    
  2. Create Overlay Network:

     docker network create --driver overlay my_overlay
    
  3. Run Services:

     docker service create --network my_overlay --name service1 busybox sleep 3600
     docker service create --network my_overlay --name service2 busybox sleep 3600
    
  4. Test Communication: Like the bridge network example, you can exec into one service and ping the other.

Bonus: User-defined Bridge Networks

For more explicit networking needs, Docker lets you define your bridge networks.

  1. Create a New Bridge Network:

     docker network create my_bridge
    
  2. Run Containers on This Network:

     docker run -d --network=my_bridge --name custom_container1 busybox sleep 3600
     docker run -d --network=my_bridge --name custom_container2 busybox sleep 3600
    

Now, custom_container1 and custom_container2 can communicate seamlessly, just like in the default bridge example.

Conclusion

Docker networking, at its core, is about ensuring smooth communication between containers regardless of their host environment. With a deep understanding of Docker networks, developers can design better, more robust Dockerized applications. Whether it's a simple bridge network or a complex overlay setup for Swarm mode, Docker offers flexibility for almost every use-case imaginable. So, dive in, experiment, and unlock the full potential of Docker!

Β