Search code examples
dockerhaproxy

Issue with running HAProxy locally on docker


I have a container running HAProxy version 2.0 locally on Docker port 3001. Config file is:

global  
        debug  

defaults  
        log global  
        mode    http  
        timeout connect 50000
        timeout client 50000  
        timeout server 50000  

frontend main  
        bind *:3000
        default_backend app  

backend app  
        balance leastconn
        mode http  
        server dummy <localhostIP>:80

Docker file is:

FROM haproxy:2.0
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

Docker Run command:

docker run -p3001 --name my-running-haproxy my-haproxy

I am issuing a postman GET to port 3000 and expecting HaProxy to redirect to my server "dummy" on local port 80. But I am not able to get any legible response back. Appreciate any inputs.


Solution

  • If you run the the container like you did, Docker will assign a random port on your localhost and route traffic to port 3001. You can check which port that is by running docker ps after you started the container and looking at the PORTS section:

    CONTAINER ID        IMAGE               COMMAND                   CREATED           STATUS              PORTS                      NAMES
    6b502af649be        my-haproxy           "/docker-entrypoint.…"   1 minute ago      Up 47 minutes       0.0.0.0:32769->3001/tcp    upbeat_shtern
    

    So on my example, you can access your application on port 32769, but this number is random.

    Keep in mind, that in your example, Docker routes traffic to port 3001, whereas you configured your HAProxy to bind to port 3000. You would at least need to change the docker run command to the following:

    docker run -p3000 --name my-running-haproxy my-haproxy
    

    But usually you want to have a fixed port on localhost, e.g. port 80. Start your container like this to achieve that:

    docker run -p 80:3000 --name my-running-haproxy my-haproxy
    

    Now you can access your application at localhost:80.