Load Balancing containers

tirthaguha profile image Tirtha Guha ・2 min read

In me previous post, I demonstrated a simple scenario of how we can run express based web applications in docker containers.

Often, for performance or resiliency, we run more than one instances of Web Applications. When you containerised your Web Application, you should be able to distribute the traffic evenly/weighted between multiple containers too.

In this exercise, I'm going to demonstrate how we can load balance between two containers.

What we need

  1. A system with docker installed in it.
  2. Complete the exercise in my previous post. If your system has docker, it won't take more that 15 mins.
  3. Basic knowledge of nginx

Lets get started

Quick recap: I have a repo in github with a basic express application. we're going to run this in a docker container.

Clone, Build and Run Container

$ git clone https://github.com/tirthaguha/card-deck-test.git
$ cd card-deck-test
$ touch Dockerfile

Open Dockerfile in a editor and paste the following

FROM node:12-slim

COPY ./package*.json ./
RUN npm ci --only-production
COPY ./ ./

CMD [ "node", "./bin/www" ]

Build the Image

docker build -t card-deck-test .

Run Multiple Instances of the same container image

docker run -p 4000:4000 -d card-deck-test
docker run -p 4001:4000 -d card-deck-test

Open the following URLs in your browser to check if they are running fine on their individual ports

Load Balance them

Enter nginx. nginx is a popular simple open-sourced webserver, often used as a loadbalancer and reverseproxy.

Create the nginx.conf

In a different directory, outside the card-deck-test, create a directory

$ mkdir nginx-docker
$ cd nginx-docker
$ touch nginx.conf
$ touch Dockerfile

Open nginx.conf in your editor and enter the following code

upstream card-app {
    server weight=1;
    server weight=1;

server {
    location / {
        proxy_pass http://card-app;

Here, you're redirecting web traffic between 2 docker containers running on and respectively. is default docker gateway IP address. You're giving them the weight=1 for both, so traffic is going to be evenly routed, in a round-robin fashion.

Create the Dockerfile for nginx

Open the Dockerfile in the editor and lets put this code in it

FROM nginx

# remove default nginx conf file
RUN rm /etc/nginx/conf.d/default.conf

# Copy the nginx.conf file as default conf
COPY nginx.conf /etc/nginx/conf.d/default.conf

Build and run the nginx docker container

Build the nginx image

$ docker build -t nginx-load-balancer .

Run the nginx image

docker run -p 8080:80 -d nginx-load-balancer

You now can run the nginx URL on the browser

Happy Load Balancing!

Posted on by:

tirthaguha profile

Tirtha Guha


javascript, ux, nodejs, react, backbonejs, marionettejs, npm, devops,


markdown guide