dockerfile使用alpine作为基础镜像构建haproxy镜像
1. 目录结构
[root@localhost haproxy]# tree
.
├── Dockerfile
└── files
├── haproxy-2.5.0.tar.gz
├── haproxycfg.sh
└── install.sh
2. dockerfile文件内容
[root@localhost haproxy]# cat Dockerfile
FROM alpine
ENV version 2.5.0
ADD files/haproxy-${version}.tar.gz /tmp
ADD files/install.sh /tmp
ADD files/haproxycfg.sh /tmp
RUN /tmp/install.sh
EXPOSE 80 8189
ENTRYPOINT /tmp/haproxycfg.sh
WORKDIR /usr/local/haproxy
3. 其他文件内容
[root@localhost haproxy]# cat entrypoint.sh
#!/bin/sh
cat > /etc/haproxy/haproxy.cfg <<EOF
#--------------全局配置----------------
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
#--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 30s
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:80
mode http
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
EOF
#循环判断
count=1
for rs_ip in $RSs;do
cat >> /etc/haproxy/haproxy.cfg <<EOF
server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done
/usr/localhaproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db
// install.sh脚本
[root@localhost files]# cat install.sh
#!/bin/sh
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories
apk update
adduser -S -H -s /sbin/nologin haproxy
addgroup haproxy
apk add --no-cache -U make gcc pcre-dev bzip2-dev openssl-dev elogind-dev libc-dev dahdi-tools dahdi-tools-dev libexecinfo libexecinfo-dev ncurses-dev zlib-dev zlib
cd /tmp/haproxy-${version}
make TARGET=linux-musl USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1
make install PREFIX=/usr/local/haproxy
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
cp haproxy /usr/sbin/
mkdir /etc/haproxy
apk del gcc make
rm -rf /tmp/haproxy-2.5.0/ /tmp/install.sh
3. 构建镜像
[root@localhost haproxy]# docker build -t dockerimages123/haproxy:v1.0 .
[root@localhost haproxy]# docker run -d --name haproxy -p 82:80 -p 8189:8189 -v /haproxy_config/:/tmp dockerimages123/haproxy:v1.0
7d4373c3dda91bfb53f9486168f50814e8abd5ab8e97ae5429864713547ec72b
// 启动一个htpd和nginx容器
[root@localhost haproxy]# docker run -d --name nginx -p 8080:80 nginx:v1.0
[root@localhost haproxy]# docker run -d --name httpd -p 80:80 dockerimages123/httpd:latest
4. 测试
![](https://i-blog.csdnimg.cn/blog_migrate/7a84f2ae0360e32fe8f5bd52100341f3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/152987babd940cef245d7fd0ba899034.png)