Configuration et utilisation d'haproxy sous Docker

On crée un fichier Dockerfile et un fichier de configuration haproxy.cfg dans un dossier :

$ ls -al 
total 16
drwxrwxr-x 2 simon simon 4096 Nov 18 10:07 .
drwxr-xr-x 5 simon simon 4096 Nov 18 09:44 ..
-rw-rw-r-- 1 simon simon  185 Nov 18 09:52 Dockerfile
-rw-rw-r-- 1 simon simon  532 Nov 18 10:07 haproxy.cfg

Le Dockerfile :

FROM haproxy:1.8.14-alpine
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
 
LABEL version="0.1-beta"
LABEL type="haproxy"
 
EXPOSE 80/tcp
EXPOSE 80/udp
EXPOSE 443/tcp
EXPOSE 443/udp
EXPOSE 8080/tcp

La configuration haproxy.cfg :

global
        daemon
        maxconn 4096
 
defaults
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms
 
frontend http-in
        bind *:80
        acl is_site1 hdr_end(host) -i domain1.se
        acl is_site2 hdr_end(host) -i domain2.com
 
        use_backend site1 if is_site1
        use_backend site2 if is_site2
 
backend site1
        option httpclose
        option forwardfor
        server s1 127.0.0.1:30000 maxconn 32
 
backend site2
        option httpclose
        option forwardfor
        server s2 127.0.0.1:30001 maxconn 32
 
listen admin
        bind *:8080
        stats enable

On lance la génération de l'image :

$ docker build -t haproxy .

Cliquez pour afficher ⇲

Cliquez pour masquer ⇱

free(): invalid pointer
SIGABRT: abort
PC=0xb6d1e206 m=0 sigcode=4294967290
signal arrived during cgo execution
 
goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0xaec2c, 0x10449e74, 0xadad0)
	/usr/lib/go-1.8/src/runtime/cgocall.go:131 +0xb4 fp=0x10449e5c sp=0x10449e40
github.com/docker/docker-credential-helpers/secretservice._Cfunc_free(0x16ff20)
	github.com/docker/docker-credential-helpers/secretservice/_obj/_cgo_gotypes.go:111 +0x30 fp=0x10449e70 sp=0x10449e5c
github.com/docker/docker-credential-helpers/secretservice.Secretservice.List.func5(0x16ff20)
	/build/golang-github-docker-docker-credential-helpers-0nGw5q/golang-github-docker-docker-credential-helpers-0.5.0/obj-arm-linux-gnueabihf/src/github.com/docker/docker-credential-helpers/secretservice/secretservice_linux.go:96 +0x44 fp=0x10449e88 sp=0x10449e70
github.com/docker/docker-credential-helpers/secretservice.Secretservice.List(0x0, 0x1404a8, 0x1046e168)
	/build/golang-github-docker-docker-credential-helpers-0nGw5q/golang-github-docker-docker-credential-helpers-0.5.0/obj-arm-linux-gnueabihf/src/github.com/docker/docker-credential-helpers/secretservice/secretservice_linux.go:97 +0x1a0 fp=0x10449edc sp=0x10449e88
github.com/docker/docker-credential-helpers/secretservice.(*Secretservice).List(0x15aecc, 0x1eb3c, 0x1f414, 0x0)
	<autogenerated>:4 +0x44 fp=0x10449ef8 sp=0x10449edc
github.com/docker/docker-credential-helpers/credentials.List(0x140a48, 0x15aecc, 0x1404d8, 0x1046e100, 0x0, 0x1220c)
	/build/golang-github-docker-docker-credential-helpers-0nGw5q/golang-github-docker-docker-credential-helpers-0.5.0/obj-arm-linux-gnueabihf/src/github.com/docker/docker-credential-helpers/credentials/credentials.go:145 +0x24 fp=0x10449f38 sp=0x10449ef8
github.com/docker/docker-credential-helpers/credentials.HandleCommand(0x140a48, 0x15aecc, 0xbecbc7b6, 0x4, 0x1404c0, 0x1046e0f8, 0x1404d8, 0x1046e100, 0xaeaf0, 0xc9738)
	/build/golang-github-docker-docker-credential-helpers-0nGw5q/golang-github-docker-docker-credential-helpers-0.5.0/obj-arm-linux-gnueabihf/src/github.com/docker/docker-credential-helpers/credentials/credentials.go:60 +0x11c fp=0x10449f6c sp=0x10449f38
github.com/docker/docker-credential-helpers/credentials.Serve(0x140a48, 0x15aecc)
	/build/golang-github-docker-docker-credential-helpers-0nGw5q/golang-github-docker-docker-credential-helpers-0.5.0/obj-arm-linux-gnueabihf/src/github.com/docker/docker-credential-helpers/credentials/credentials.go:41 +0x170 fp=0x10449fa8 sp=0x10449f6c
main.main()
	/build/golang-github-docker-docker-credential-helpers-0nGw5q/golang-github-docker-docker-credential-helpers-0.5.0/secretservice/cmd/main_linux.go:9 +0x38 fp=0x10449fbc sp=0x10449fa8
runtime.main()
	/usr/lib/go-1.8/src/runtime/proc.go:185 +0x1e4 fp=0x10449fe4 sp=0x10449fbc
runtime.goexit()
	/usr/lib/go-1.8/src/runtime/asm_arm.s:1017 +0x4 fp=0x10449fe4 sp=0x10449fe4
 
goroutine 17 [syscall, locked to thread]:
runtime.goexit()
	/usr/lib/go-1.8/src/runtime/asm_arm.s:1017 +0x4
 
trap    0x6
error   0x0
oldmask 0x0
r0      0x0
r1      0xbecbc19c
r2      0x0
r3      0x8
r4      0x0
r5      0xb6fb1968
r6      0xbecbc19c
r7      0xaf
r8      0xbecbc3e8
r9      0x2
r10     0xb6dfb000
fp      0x1
ip      0xaf
sp      0xbecbc190
lr      0xb6d2bb33
pc      0xb6d1e206
cpsr    0x70030
fault   0x0
Sending build context to Docker daemon  3.584kB
Step 1/9 : FROM haproxy:1.8.14-alpine
 ---> 8254a3d7c0a2
Step 2/9 : COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
 ---> Using cache
 ---> 726bb8b2831d
Step 3/9 : LABEL version="0.2-beta"
 ---> Running in 33cd4aeca9b6
Removing intermediate container 33cd4aeca9b6
 ---> ea10826e5f6b
Step 4/9 : LABEL type="haproxy"
 ---> Running in 6070b706fb6d
Removing intermediate container 6070b706fb6d
 ---> 3cacd29d9a43
Step 5/9 : EXPOSE 80/tcp
 ---> Running in b800919bebd2
Removing intermediate container b800919bebd2
 ---> ef5d38e646c5
Step 6/9 : EXPOSE 80/udp
 ---> Running in df8d9d5c95db
Removing intermediate container df8d9d5c95db
 ---> 760d38b14a4f
Step 7/9 : EXPOSE 443/tcp
 ---> Running in 9e8b3dcd642b
Removing intermediate container 9e8b3dcd642b
 ---> 8f4ee4c313ac
Step 8/9 : EXPOSE 443/udp
 ---> Running in e87d071fb6ef
Removing intermediate container e87d071fb6ef
 ---> 3b6da444cc3c
Step 9/9 : EXPOSE 8080/tcp
 ---> Running in ddb64fc64b85
Removing intermediate container ddb64fc64b85
 ---> 3e3076009603
Successfully built 3e3076009603
Successfully tagged haproxy:latest

On teste la configuration :

$ docker run -it --rm --name haproxy-syntax-check haproxy haproxy -c -f /usr/local/etc/haproxy/haproxy.cfg
Configuration file is valid

Lancer le container :

$ docker run -d -p 8080:8080 -p 443:443 -p 80:80 --name my-haproxy3 haproxy
 
$ docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                                                                               NAMES
2c8973e841b2        haproxy                "/docker-entrypoint.…"   8 seconds ago       Up 5 seconds        80/udp, 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 443/udp, 0.0.0.0:8080->8080/tcp   my-haproxy

Statistiques : http://192.168.1.101:8080/haproxy?stats