Scale Docker Horizontally with Nginx Load Balancing

Share this video with your friends

Send Tweet

Node.js apps built with Docker cannot scale horizontally by themselves. In this lesson we will spawn out multiple Node.js processes/containers, and move the incoming request handler to an Nginx proxy to load balance and properly scale our Node.js app.

Stefan Stolniceanu
Stefan Stolniceanu
~ 7 years ago

I know that docker-compose has the scale option, which can be used to scale a container very easy, but you'll need to update manually the nginx conf to use the new containers. Is there any way to do this automatically?

Another question: I used to put the Load Balancer in the same docker-compose file as the node app. Is that a good practice?

Mark Shust
Mark Shust(instructor)
~ 7 years ago

Good question. It appears Docker still does not offer native service discovery. There is a long-standing issue to implement it, however it doesn't appear to be gaining g development traction. I stumbled upon this post -- http://blog.gaiterjones.com/docker-mono-host-service-scaling-and-dynamic-load-balancing-with-nginx/ -- which pretty much provides service discovery within nginx for load balancing. Another solution would be to mount the Docker socket for reading within containers, however this has security implications. You could also code a custom solution on the software side, even though this is not ideal.

Regarding Docker Compose, you can absolutely and should have nginx and node within the same compose definition. Hope this helps.