Contents

Setting Up a Dockerized Nginx Web Server for Load Balancing Testing

A Step-by-Step Guide to Creating Multiple Websites in Docker from Single Image

Website Visitors:

Introduction

In this tutorial, we will set up a Dockerized Nginx web server that serves multiple websites from a single server. This setup will allow us to test load balancing functionality on a network device effectively. By creating multiple web server containers, each serving a different website, we can simulate traffic and observe how the load balancer distributes requests across these containers.

Step 1: Create the Dockerfile

  1. Create a file named webserver_dockerfile.

    1
    
    touch webserver_dockerfile
    
  2. Open the file in a text editor and add the following content:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    # Use a base image that has a web server
    FROM nginx:alpine
    
    # Set an environment variable for container name
    ENV CONTAINERNAME webserver
    
    # Install bash for script execution
    RUN apk add --no-cache bash
    
    # Create a custom script to generate the index.html file
    RUN echo '#!/bin/sh' > /usr/local/bin/create_index.sh && \
        echo 'echo "<html><head><style>body{display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background-color:#3383ff}.box{background-color:white;padding:20px 40px;border-radius:8px;text-align:center;box-shadow:0 4px 8px rgba(0,0,0,0.2)}h1{font-family:Arial,sans-serif;margin:0}.welcome{color:black}.servername{color:red}</style></head><body><div class="box"><h1 class="welcome">Welcome to <span class="servername">${CONTAINERNAME}</span></h1></div></body></html>" > /usr/share/nginx/html/index.html' >> /usr/local/bin/create_index.sh && \
        chmod +x /usr/local/bin/create_index.sh
    
    # Expose port 80
    EXPOSE 80
    
    # Start Nginx and run the script to create the index.html
    CMD /usr/local/bin/create_index.sh && nginx -g 'daemon off;'
    

Step 2: Build the Docker Image

  1. Open your terminal and navigate to the directory containing the Dockerfile.

  2. Run the following command to build the Docker image:

    1
    
    docker build -f webserver_dockerfile -t webserver .
    
    • webserver_dockerfile is the name of the Dockerfile.
    • -f specifies the location of your Dockerfile.
    • -t tags your image with a name (webserver).

Step 3: Verify Your Docker Host IP Address

  1. Check your Docker host IP.

    If you’re running Docker on WSL, the WSL machine will serve as your Docker host. Make sure to run this command within the WSL environment, not on your machine’s base operating system.

    • Run the command:

      1
      
      ifconfig
      
    • Note the IP address for the eth0 interface (e.g., 172.19.131.41).

  2. Assign additional IP addresses to your Docker host in the same network range.

    • Use the following commands to create virtual interfaces:

      1
      2
      3
      
      sudo ifconfig eth0:1 172.19.131.42 netmask 255.255.255.0
      sudo ifconfig eth0:2 172.19.131.43 netmask 255.255.255.0
      sudo ifconfig eth0:3 172.19.131.44 netmask 255.255.255.0
      

    If your Docker host IP is different (e.g., 192.168.200.50), replace the IPs accordingly (e.g., 192.168.200.51, 192.168.200.52, etc.).

Step 4: Run the Docker Containers

  1. Run the Docker containers with the following commands:

    1
    2
    3
    
    docker run -d --name webserver1 -e CONTAINERNAME=WebServer1 -p 172.19.131.42:80:80 webserver
    docker run -d --name webserver2 -e CONTAINERNAME=WebServer2 -p 172.19.131.43:80:80 webserver
    docker run -d --name webserver3 -e CONTAINERNAME=WebServer3 -p 172.19.131.44:80:80 webserver
    
    • Each command runs a new container, setting the CONTAINERNAME environment variable to differentiate the web servers.

Step 5: Test the Web Servers

  1. Open your web browser.

  2. Access the web servers using the assigned IP addresses:

  3. You should see a welcome page that displays the respective web server names.

Everything is set now. You can now use these ip addresses in your load balancing servers and test the functionality.

Conclusion

You have successfully set up a Dockerized Nginx web server that serves multiple websites from a single server instance. This configuration is ideal for testing load balancing functionality on a network device, allowing you to observe how traffic is distributed across the different web server containers.

If you were to use this setup to test load balancing functionality, which server would you choose to install Docker on? Would it be a Linux machine with Docker installed, Boot2Docker, or Docker Desktop? Share your thoughts in the comments!

Your inbox needs more DevOps articles.

Subscribe to get our latest content by email.