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
This guide demonstrates how to set up a Dockerized Nginx web server for testing load balancing. The steps can be performed on various systems such as Ubuntu, Ubuntu Core, and even older solutions like Boot2Docker. However, note that Boot2Docker is deprecated and it’s recommended to use more current alternatives like Docker Desktop or native Docker installations on Linux. You’ll create multiple containers to simulate different servers, which is ideal for testing network load balancing. The method is practical and modern, but remember that some legacy tools mentioned may no longer be supported or recommended for new projects.
Here, 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
-
Create a file named
webserver_dockerfile.1touch webserver_dockerfile -
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:#3383ff}</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;'
In above script replace 3383ff (blue color HTML code) with D9001D (red color HTML code) if you are working with two servers.
Step 2: Build the Docker Image
-
Open your terminal and navigate to the directory containing the Dockerfile.
-
Run the following command to build the Docker image:
1docker build -f webserver_dockerfile -t webserver .webserver_dockerfileis the name of the Dockerfile.-fspecifies the location of your Dockerfile.-ttags your image with a name (webserver).
Step 3: Verify Your Docker Host IP Address
-
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:
1ifconfig -
Note the IP address for the
eth0interface (e.g.,172.19.131.41).
-
-
Assign additional IP addresses to your Docker host in the same network range.
-
Use the following commands to create virtual interfaces:
1 2 3sudo 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
-
Run the Docker containers with the following commands:
1 2 3docker 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
CONTAINERNAMEenvironment variable to differentiate the web servers.
- Each command runs a new container, setting the
Step 5: Test the Web Servers
-
Open your web browser.
-
Access the web servers using the assigned IP addresses:
- For WebServer1: http://172.19.131.42
- For WebServer2: http://172.19.131.43
- For WebServer3: http://172.19.131.44
-
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.
