Bobcares

Docker Image HAproxy Centos | Set Up Guide

by | Apr 4, 2024

Learn how to set up Docker Image HAproxy Centos. Our Docker Support team is here to help you with your questions and concerns.

Docker Image HAproxy Centos | Set Up Guide

Docker is a lightweight and portable solution for packaging, distributing, and running applications in containers.

Docker Image HAproxy Centos | Set Up Guide

Today, we are going to take a look at setting up HAProxy inside a Docker container.

  1. To begin with, we have to create a `Dockerfile` in a new directory. This file acts as the blueprint for building our Docker image. Below are the contents of the Dockerfile:

    # Use the official CentOS base image
    FROM centos:latest
    # Install HAProxy
    RUN yum -y update && yum -y install haproxy && yum clean all
    # Copy the HAProxy configuration file to the image
    COPY haproxy.cfg /etc/haproxy/haproxy.cfg
    # Expose HAProxy port
    EXPOSE 80
    # Start HAProxy
    CMD ["haproxy", "-f", "/etc/haproxy/haproxy.cfg"]

  2. Next, create a HAProxy configuration file named `haproxy.cfg` in the same directory as the Dockerfile. Here’s a basic example to get you started:

    global
    log /dev/log local0
    log /dev/log local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    user haproxy
    group haproxy
    daemon
    defaults
    log global
    mode http
    option httplog
    option dontlognull
    timeout connect 5000
    timeout client 50000
    timeout server 50000
    frontend myfrontend
    bind *:80
    mode http
    default_backend mybackend
    backend mybackend
    mode http
    server webserver1 webserver1_ip:webserver1_port
    server webserver2 webserver2_ip:webserver2_port

    Remember to replace `webserver1_ip`, `webserver1_port`, `webserver2_ip`, and `webserver2_port` with the actual IP addresses and ports of the backend servers.

  3. Then, open a terminal and go to the directory containing the Dockerfile and HAProxy configuration file. Next, run this command:

    docker build -t my-haproxy

    This command builds a Docker image named `my-haproxy` based on the Dockerfile and configuration files in the current directory.

  4. Once the image is built, we can run a container from it:

    docker run -d -p 8080:80 --name my-haproxy-container my-haproxy

    This command runs the HAProxy container in detached mode, mapping port 8080 on our host machine to port 80 on the container.

With the above steps, we can easily set up an HAProxy instance running on CentOS inside a Docker container. Let us know in the comments if you need further help.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

In brief, our Support Experts demonstrated how to set up HAProxy inside a Docker container.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

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

Never again lose customers to poor
server speed! Let us help you.