Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active June 27, 2024 16:46
Show Gist options
  • Save x-yuri/9387455cd04241311fe8d0f640b1035e to your computer and use it in GitHub Desktop.
Save x-yuri/9387455cd04241311fe8d0f640b1035e to your computer and use it in GitHub Desktop.
haproxy + docker

haproxy + docker

docker-compose.yml:

services:
  haproxy:
    image: haproxy:2.9-alpine
    ports:
      - 8888:80
    volumes:
      - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro

  app:
    build: .
    command: perl server.pl
    init: true
    deploy:
      replicas: 4

haproxy.cfg:

global
    maxconn  1024
    stats socket /var/lib/haproxy/haproxy.sock level admin
    stats timeout 60m

listen http
    mode http
    bind *:80
    server-template s 4 app:80 check init-addr last,none resolvers default
    timeout connect 5s
    timeout client 50s
    timeout server 50s
    log stderr local7
    option httplog
    option log-health-checks
    option httpchk GET /health

Dockerfile:

FROM alpine:3.20
RUN apk add perl perl-http-message perl-http-server-simple
COPY server.pl .

server.pl:

use strict;
use warnings;

{package MyServer;
use HTTP::Response;
use HTTP::Server::Simple::CGI;
use Data::Dumper;
use base qw(HTTP::Server::Simple::CGI);
sub handle_request {
    my ($self, $cgi) = @_;
    my $resp = HTTP::Response->new(200, undef, undef, 'hello world');
    $resp->protocol('HTTP/1.0');
    print $resp->as_string;
    print STDERR $cgi->path_info, "\n";
}
}

MyServer->new(80)->run();
$ docker compose up
$ curl localhost:8888
hello world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment