NGINX load balancing with Docker
Once you've tackled the conversion from complex deploy scripts into neat Dockerfile
, the next step is to deal with scale. As for most other problems, Docker has a solution for this too; scaling is one of those.
While newer versions of Docker have added native load balancing, it's still quite simplistic. This makes NGINX a better choice for many uses. Since you're already familiar with the workings of NGINX, it's easily adapted to provide load balancing within a Docker environment.
In a larger deployment, we'd use a more formal service discovery tool, such as consul
or etcd
, in order to provide more granular control. This recipe will simply use the built-in DNS capability of Docker in order to round-robin the requests.
Getting ready
This recipe has been simplified to run on a single VM. In most real-world scenarios, this would be spread across multiple VMs. Here's what our test scenario looks like:

We're using the HTest tool, as covered back in Chapter 8, Load...