How to Setup HAProxy with Docker for Scalable Deployments on CentOS and Ubuntu

How to Setup HAProxy with Docker for Scalable Deployments on CentOS and Ubuntu

In web hosting, scalability and efficiency are key. As your website or app grows, so does the need for a robust and reliable infrastructure that can handle increased traffic and provide a seamless user experience. One of the solutions to this challenge is the use of HAProxy in conjunction with Docker.

HAProxy, a high-performance load balancer, is known for its ability to distribute web traffic across multiple servers, thereby enhancing performance and ensuring high availability. Docker, on the other hand, is a platform that allows you to automate the deployment, scaling, and management of applications within containers. When used together, they provide a powerful tool for scalable deployments.

docker haproxy

In this tutorial, we will guide you through the process of setting up HAProxy with Docker for scalable deployments on CentOS and Ubuntu. This setup will allow you to manage your web traffic more effectively, ensuring that your website or application remains accessible even during peak traffic periods. You will not only improve the performance of your server but also enhance the overall user experience on your website or app.

Let’s get started.

Step 1: Installing Docker

The first step in setting up HAProxy with Docker is to install Docker on your CentOS or Ubuntu server. Docker is available in the default repositories of these operating systems, so you can install it using the package manager of your OS.

On CentOS, you can install Docker with the following command:

sudo yum install docker

On Ubuntu, you can install Docker with the following command:

sudo apt-get install

After installing Docker, you need to start the Docker service and enable it to start on boot. You can do this with the following commands:

On CentOS:

sudo systemctl start docker
sudo systemctl enable docker

On Ubuntu:

sudo systemctl start docker
sudo systemctl enable docker

Step 2: Pulling the HAProxy Docker Image

Once Docker is installed and running on your server, the next step is to pull the HAProxy Docker image. This image contains the HAProxy software and all the dependencies it needs to run.

You can pull the HAProxy Docker image with the following command:

sudo docker pull haproxy

This command will download the latest version of the HAProxy Docker image from the Docker Hub.

See also  How to Add Persistent Static Routes on RHEL 6/CentOS 6

Step 3: Creating a Configuration File for HAProxy

Before you can run HAProxy in a Docker container, you need to create a configuration file for it. This file will define how HAProxy should distribute the web traffic among your servers.

You can create a new configuration file for HAProxy with the following command:

sudo nano /etc/haproxy/haproxy.cfg

In this file, you need to define the frontend (the interface that will receive the web traffic), the backend (the servers that will handle the traffic), and the load balancing algorithm that HAProxy should use.

Here is a basic example of what the configuration file might look like:

    maxconn 256

    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http_front
    bind *:80
    default_backend http_back

backend http_back
    balance roundrobin
    server server1 check
    server server2 check

In this example, HAProxy is configured to listen on port 80 and to distribute the web traffic between two servers ( and using the round-robin algorithm.

After creating the configuration file, save it and exit the text editor.

Step 4: Running HAProxy in a Docker Container

Now that you have a configuration file for HAProxy, you can run it in a Docker container. To do this, you need to use the docker run command and specify the HAProxy Docker image and the configuration file.

Here is the command you need to run:

sudo docker run -d --name my-haproxy -v /etc/haproxy:/usr/local/etc/haproxy:ro -p 80:80 haproxy

In this command, the -d option tells Docker to run the container in the background, the –name option sets the name of the container, the -v option mounts the /etc/haproxy directory on your server to the /usr/local/etc/haproxy directory in the container (where HAProxy expects to find its configuration file), and the -p option maps port 80 on your server to port 80 in the container.

After running this command, HAProxy should be up and running in a Docker container on your server, ready to distribute your web traffic.

Step 5: Testing Your Setup

After setting up HAProxy with Docker, it’s crucial to test your setup to ensure that everything is working as expected. This involves sending a request to your server and checking if it gets distributed to one of your backend servers.

See also  How to Configure and Use HAProxy as a Reverse Proxy

To do this, you can use the curl command. curl is a command-line tool used for transferring data with URLs and is a useful tool for testing HTTP requests.

Here’s how you can use curl to test your HAProxy setup:

curl http://your-server-ip

In the command above, replace your-server-ip with the IP address of your server. When you run this command, it sends an HTTP GET request to your server.

If your HAProxy setup is correct, this command should return the default web page of one of your backend servers. This is because HAProxy, acting as a load balancer, receives the HTTP request and forwards it to one of the backend servers.

For instance, if you have two backend servers with IP addresses and, and both servers are hosting a website, running the curl command should return the HTML content of the website hosted on either or

This indicates that HAProxy is correctly distributing incoming requests to the backend servers, confirming that your setup is working as expected. If you don’t receive the expected output, you may need to revisit the previous steps and check your HAProxy configuration and Docker setup.

Commands Mentioned:

  • sudo yum install docker – Installs Docker on CentOS
  • sudo apt-get install – Installs Docker on Ubuntu
  • sudo systemctl start docker – Starts the Docker service
  • sudo systemctl enable docker – Enables Docker to start on boot
  • sudo docker pull haproxy – Pulls the HAProxy Docker image
  • sudo nano /etc/haproxy/haproxy.cfg – Creates a new configuration file for HAProxy
  • sudo docker run -d –name my-haproxy -v /etc/haproxy:/usr/local/etc/haproxy:ro -p 80:80 haproxy – Runs HAProxy in a Docker container
  • curl http://your-server-ip – Sends a request to your server to test the HAProxy setup


In this tutorial, we have walked you through the process of setting up HAProxy with Docker for scalable deployments on CentOS and Ubuntu. This setup allows you to effectively manage your web traffic, ensuring high availability and performance for your website or application.

See also  How to Install wget on Ubuntu

Remember, the key to a successful web hosting setup is not only having a robust and reliable infrastructure but also being able to scale and adapt as your needs grow. By using HAProxy with Docker, you can easily scale your setup by adding more backend servers as your traffic increases. Moreover, since HAProxy distributes the traffic evenly among your servers, it ensures that no single server becomes a bottleneck, thereby improving the overall performance of your website or application.

If you encounter any issues or have any questions about this setup, feel free to leave a comment below.

Explore our other articles on proxy servers, web servers, and dedicated server hosting for more information and guidance.


  1. What is the benefit of using HAProxy with Docker?

    Using HAProxy with Docker allows for scalable deployments. As your website or application grows, you can easily add more backend servers to handle the increased traffic. HAProxy ensures the traffic is distributed evenly among your servers, preventing any single server from becoming a bottleneck and improving overall performance.

  2. Can I use HAProxy with Docker on other operating systems?

    Yes, while this guide focuses on CentOS and Ubuntu, HAProxy and Docker can be used on many other Linux distributions. The installation and configuration steps may vary slightly depending on the distribution.

  3. How can I monitor the performance of HAProxy?

    HAProxy includes a built-in statistics module that provides real-time data about its performance. You can enable this module in the HAProxy configuration file. Additionally, there are several third-party tools available that can provide more detailed analytics and monitoring for HAProxy.

  4. What kind of load balancing algorithms does HAProxy support?

    HAProxy supports several load balancing algorithms, including round robin, least connections, and source. The round robin algorithm distributes requests evenly across the servers, while the least connections algorithm sends requests to the server with the fewest active connections. The source algorithm distributes requests based on the client’s IP address.

  5. Can HAProxy handle SSL/TLS traffic?

    Yes, HAProxy can handle SSL/TLS traffic and can offload the SSL/TLS processing from your backend servers. This allows your backend servers to focus on processing the actual requests, which can improve their performance.


Leave a Reply

Your email address will not be published. Required fields are marked *