Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 203 of 8023. Docker Orchestration and StorageFree lesson

Load Balancing Traffic Across Replicas

You will configure internal service discovery to automatically distribute incoming network requests evenly across multiple running container instances.

In this lesson, you will learn how Docker automatically distributes network requests across multiple container replicas to ensure your application remains responsive under load.

How Load Balancing Works with Service Discovery

In previous lessons, we covered how Docker’s Embedded DNS allows containers to resolve service names to IP addresses. When you scale an application—for example, running three replicas of a web server—you might wonder how traffic reaches all of them instead of just one.

When you define a service in a docker-compose.yml file, Docker creates a Virtual IP (VIP) for that service name. Even though you have multiple containers, the service name maps to this single VIP. When a request hits that name, Docker’s internal load balancer intercepts the traffic and forwards it to one of the available healthy replicas using a round-robin algorithm.

Configuring Your Deployment

To enable this, you simply specify the replicas count in your service definition. Because Docker handles the networking layer automatically, you don't need to manually track individual container IPs.

services:
  web-app:
    image: my-web-app:latest
    deploy:
      replicas: 3
    networks:
      - frontend

networks:
  frontend:

When a request is sent to web-app from another container on the same network, the Docker engine looks at the list of active containers attached to that service. It transparently routes the request to one of the three replicas, balancing the workload evenly. If one replica becomes unresponsive—a state you can monitor using the Health Checks we discussed earlier—the load balancer will automatically stop routing traffic to that specific instance until it recovers.

By leveraging this built-in mechanism, you eliminate the need for external load-balancing hardware or complex configuration scripts. You simply scale your service up or down, and Docker handles the distribution of traffic across your pool of replicas instantly.

Key Takeaway: Docker’s embedded load balancer automatically distributes incoming traffic across all healthy service replicas using a virtual IP, abstracting the complexity of managing individual container connections.