Dockerfile使用alpine制作haproxy镜像


[root@master file]# wget http://download.openpkg.org/components/cache/haproxy/haproxy-2.4.0.tar.gz
--2021-12-12 21:46:40--  http://download.openpkg.org/components/cache/haproxy/haproxy-2.4.0.tar.gz
正在解析主机 download.openpkg.org (download.openpkg.org)... 148.251.204.41
正在连接 download.openpkg.org (download.openpkg.org)|148.251.204.41|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:3570069 (3.4M) [application/x-gzip]
正在保存至: “haproxy-2.4.0.tar.gz”

haproxy-2.4.0.tar.gz              100%[===========================================================>]   3.40M  58.1KB/s  用时 59s     

2021-12-12 21:47:41 (59.0 KB/s) - 已保存 “haproxy-2.4.0.tar.gz” [3570069/3570069])

[root@master file]# cd ..
[root@master haproxy]# vim Dockerfile 
[root@master haproxy]# cat Dockerfile 
FROM alpine

LABEL MAINTAINER="yanghaixx 2568887571@qq.com"

ENV version 2.4.0

ADD file/haproxy-${version}.tar.gz /tmp/
ADD file/install.sh /tmp/
ADD file/haproxycfg.sh /tmp/
ADD file/sysctl.conf /tmp/

RUN /tmp/install.sh
ENTRYPOINT /tmp/haproxycfg.sh
[root@master haproxy]# cd file/
[root@master file]# vim install.sh
[root@master file]# 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-2.4.0
make TARGET=linux-musl USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 
make install PREFIX=/usr/local/haproxy
cp haproxy  /usr/sbin/
mkdir /etc/haproxy
apk del gcc make
rm -rf /tmp/haproxy-2.4.0/* /tmp/install.sh
[root@master file]# chmod +x install.sh 
[root@master file]# vim haproxycfg.sh
[root@master file]# cat haproxycfg.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

haproxy -f  /etc/haproxy/haproxy.cfg -db
[root@master file]# chmod +x haproxycfg.sh 
[root@master file]# cd 
[root@master ~]# ls
 公共   文档   anaconda-ks.cfg               haproxy                mysql                                  soft
 模板   下载   apache                        index.jsp              prometheus-2.31.1.linux-amd64.tar.gz
 视频   音乐   apache-tomcat-9.0.54.tar.gz   initial-setup-ks.cfg   psutil-3.2.2
 图片   桌面  'ervice iptables status'       instll.sh              psutil-3.2.2.tar.gz
[root@master ~]# cd haproxy/
[root@master haproxy]# ls
Dockerfile  file
[root@master haproxy]# cd file/
[root@master file]# vim sysctl.conf 
[root@master file]# cat sysctl.conf 
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[root@master file]# cd ..
[root@master haproxy]# docker build -t haproxy:v1.0 .
Sending build context to Docker daemon  3.578MB
Step 1/9 : FROM alpine
latest: Pulling from library/alpine
59bf1c3509f3: Pull complete 
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Downloaded newer image for alpine:latest
 ---> c059bfaa849c
Step 2/9 : LABEL MAINTAINER="yanghaixx 2568887571@qq.com"
 ---> Running in 97e3dc776af0
Removing intermediate container 97e3dc776af0
 ---> 2e914d51bfb7
Step 3/9 : ENV version 2.4.0
 ---> Running in 0d721f8f91c0
Removing intermediate container 0d721f8f91c0
 ---> 0d214c519aad
Step 4/9 : ADD file/haproxy-${version}.tar.gz /tmp/
 ---> 4acca9ca9c3d
Step 5/9 : ADD file/install.sh /tmp/
 ---> 82f9b7529adb
Step 6/9 : ADD file/haproxycfg.sh /tmp/
 ---> da71f584a77e
Step 7/9 : ADD file/sysctl.conf /tmp/
 ---> ce8470fb616f
Step 8/9 : RUN /tmp/install.sh
 ---> Running in f06302838561
fetch https://mirrors.aliyun.com/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
v3.15.0-83-gd925f25ff4 [https://mirrors.aliyun.com/alpine/v3.15/main]
v3.15.0-86-g72df58eb05 [https://mirrors.aliyun.com/alpine/v3.15/community]
OK: 15832 distinct packages available
fetch https://mirrors.aliyun.com/alpine/v3.15/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.15/community/x86_64/APKINDEX.tar.gz
(1/41) Installing libbz2 (1.0.8-r1)
(2/41) Installing bzip2-dev (1.0.8-r1)
(3/41) Installing popt (1.18-r0)
(4/41) Installing pcre (8.45-r1)
(5/41) Installing slang (2.3.2-r0)
(6/41) Installing newt (0.52.21-r0)
(7/41) Installing dahdi-tools (3.1.0-r1)
(8/41) Installing bsd-compat-headers (0.7.2-r3)
(9/41) Installing linux-headers (5.10.41-r0)
(10/41) Installing dahdi-linux-dev (3.1.0-r0)
(11/41) Installing pkgconf (1.8.0-r0)
(12/41) Installing newt-dev (0.52.21-r0)
(13/41) Installing dahdi-tools-dev (3.1.0-r1)
(14/41) Installing libcap (2.61-r0)
(15/41) Installing libelogind (246.10-r4)
(16/41) Installing elogind-dev (246.10-r4)
(17/41) Installing libgcc (10.3.1_git20211027-r0)
(18/41) Installing libstdc++ (10.3.1_git20211027-r0)
(19/41) Installing binutils (2.37-r3)
(20/41) Installing libgomp (10.3.1_git20211027-r0)
(21/41) Installing libatomic (10.3.1_git20211027-r0)
(22/41) Installing libgphobos (10.3.1_git20211027-r0)
(23/41) Installing gmp (6.2.1-r0)
(24/41) Installing isl22 (0.22-r0)
(25/41) Installing mpfr4 (4.1.0-r0)
(26/41) Installing mpc1 (1.2.1-r0)
(27/41) Installing gcc (10.3.1_git20211027-r0)
(28/41) Installing musl-dev (1.2.2-r7)
(29/41) Installing libc-dev (0.7.2-r3)
(30/41) Installing libexecinfo (1.1-r1)
(31/41) Installing libexecinfo-dev (1.1-r1)
(32/41) Installing make (4.3-r0)
(33/41) Installing ncurses-terminfo-base (6.3_p20211120-r0)
(34/41) Installing ncurses-libs (6.3_p20211120-r0)
(35/41) Installing ncurses-dev (6.3_p20211120-r0)
(36/41) Installing openssl-dev (1.1.1l-r7)
(37/41) Installing libpcre16 (8.45-r1)
(38/41) Installing libpcre32 (8.45-r1)
(39/41) Installing libpcrecpp (8.45-r1)
(40/41) Installing pcre-dev (8.45-r1)
(41/41) Installing zlib-dev (1.2.11-r3)
Executing busybox-1.34.1-r3.trigger
OK: 144 MiB in 55 packages
  CC      src/ev_poll.o
  CC      src/ev_epoll.o
  CC      src/cpuset.o
  CC      src/ssl_sample.o
  CC      src/ssl_sock.o
  CC      src/ssl_crtlist.o
  CC      src/ssl_ckch.o
  CC      src/ssl_utils.o
  CC      src/cfgparse-ssl.o
  CC      src/namespace.o
  CC      src/mux_h2.o
  CC      src/mux_fcgi.o
  CC      src/http_ana.o
  CC      src/mux_h1.o
  CC      src/stream.o
  CC      src/tcpcheck.o
  CC      src/stats.o
  CC      src/flt_spoe.o
  CC      src/server.o
  CC      src/tools.o
  CC      src/sample.o
  CC      src/log.o
  CC      src/backend.o
  CC      src/stick_table.o
  CC      src/cfgparse.o
  CC      src/peers.o
  CC      src/cli.o
  CC      src/pattern.o
  CC      src/resolvers.o
  CC      src/proxy.o
  CC      src/http_htx.o
  CC      src/check.o
  CC      src/cache.o
  CC      src/cfgparse-listen.o
  CC      src/haproxy.o
  CC      src/http_act.o
  CC      src/stream_interface.o
  CC      src/http_fetch.o
  CC      src/listener.o
  CC      src/dns.o
  CC      src/connection.o
  CC      src/tcp_rules.o
  CC      src/debug.o
  CC      src/sink.o
  CC      src/payload.o
  CC      src/mux_pt.o
  CC      src/filters.o
  CC      src/fcgi-app.o
  CC      src/server_state.o
  CC      src/vars.o
  CC      src/map.o
  CC      src/cfgparse-global.o
  CC      src/task.o
  CC      src/flt_http_comp.o
  CC      src/session.o
  CC      src/sock.o
  CC      src/flt_trace.o
  CC      src/acl.o
  CC      src/trace.o
  CC      src/http_rules.o
  CC      src/queue.o
  CC      src/mjson.o
  CC      src/h2.o
  CC      src/h1.o
  CC      src/mworker.o
  CC      src/lb_chash.o
  CC      src/ring.o
  CC      src/activity.o
  CC      src/tcp_sample.o
  CC      src/proto_tcp.o
  CC      src/htx.o
  CC      src/h1_htx.o
  CC      src/extcheck.o
  CC      src/channel.o
  CC      src/proto_sockpair.o
  CC      src/fd.o
  CC      src/compression.o
  CC      src/mqtt.o
  CC      src/tcp_act.o
  CC      src/raw_sock.o
  CC      src/frontend.o
  CC      src/http_conv.o
  CC      src/xprt_handshake.o
  CC      src/pool.o
  CC      src/applet.o
  CC      src/mailers.o
  CC      src/lb_fwrr.o
  CC      src/lb_fwlc.o
  CC      src/lb_fas.o
  CC      src/proto_uxst.o
  CC      src/http.o
  CC      src/action.o
  CC      src/protocol.o
  CC      src/thread.o
  CC      src/sock_unix.o
  CC      src/proto_udp.o
  CC      src/lb_map.o
  CC      src/sock_inet.o
  CC      src/lru.o
  CC      src/cfgparse-tcp.o
  CC      src/cfgdiag.o
  CC      src/proto_uxdg.o
  CC      src/ev_select.o
  CC      src/cfgparse-unix.o
  CC      src/uri_normalizer.o
  CC      src/ebmbtree.o
  CC      src/sha1.o
  CC      src/time.o
  CC      src/signal.o
  CC      src/mworker-prog.o
  CC      src/hpack-dec.o
  CC      src/fix.o
  CC      src/arg.o
  CC      src/eb64tree.o
  CC      src/chunk.o
  CC      src/shctx.o
  CC      src/regex.o
  CC      src/fcgi.o
  CC      src/eb32tree.o
  CC      src/eb32sctree.o
  CC      src/dynbuf.o
  CC      src/uri_auth.o
  CC      src/hpack-tbl.o
  CC      src/ebimtree.o
  CC      src/auth.o
  CC      src/ebsttree.o
  CC      src/ebistree.o
  CC      src/base64.o
  CC      src/wdt.o
  CC      src/pipe.o
  CC      src/http_acl.o
  CC      src/hpack-enc.o
  CC      src/dict.o
  CC      src/dgram.o
  CC      src/init.o
  CC      src/hpack-huff.o
  CC      src/freq_ctr.o
  CC      src/ebtree.o
  CC      src/hash.o
  CC      src/version.o
  LD      haproxy
  CC      dev/flags/flags.o
  LD      dev/flags/flags
created directory: '/usr/local/haproxy/'
created directory: '/usr/local/haproxy/sbin'
'haproxy' -> '/usr/local/haproxy/sbin/haproxy'
created directory: '/usr/local/haproxy/share/'
created directory: '/usr/local/haproxy/share/man/'
created directory: '/usr/local/haproxy/share/man/man1'
'doc/haproxy.1' -> '/usr/local/haproxy/share/man/man1/haproxy.1'
created directory: '/usr/local/haproxy/doc/'
created directory: '/usr/local/haproxy/doc/haproxy'
'doc/51Degrees-device-detection.txt' -> '/usr/local/haproxy/doc/haproxy/51Degrees-device-detection.txt'
'doc/DeviceAtlas-device-detection.txt' -> '/usr/local/haproxy/doc/haproxy/DeviceAtlas-device-detection.txt'
'doc/SOCKS4.protocol.txt' -> '/usr/local/haproxy/doc/haproxy/SOCKS4.protocol.txt'
'doc/SPOE.txt' -> '/usr/local/haproxy/doc/haproxy/SPOE.txt'
'doc/WURFL-device-detection.txt' -> '/usr/local/haproxy/doc/haproxy/WURFL-device-detection.txt'
'doc/architecture.txt' -> '/usr/local/haproxy/doc/haproxy/architecture.txt'
'doc/close-options.txt' -> '/usr/local/haproxy/doc/haproxy/close-options.txt'
'doc/configuration.txt' -> '/usr/local/haproxy/doc/haproxy/configuration.txt'
'doc/cookie-options.txt' -> '/usr/local/haproxy/doc/haproxy/cookie-options.txt'
'doc/intro.txt' -> '/usr/local/haproxy/doc/haproxy/intro.txt'
'doc/linux-syn-cookies.txt' -> '/usr/local/haproxy/doc/haproxy/linux-syn-cookies.txt'
'doc/lua.txt' -> '/usr/local/haproxy/doc/haproxy/lua.txt'
'doc/management.txt' -> '/usr/local/haproxy/doc/haproxy/management.txt'
'doc/netscaler-client-ip-insertion-protocol.txt' -> '/usr/local/haproxy/doc/haproxy/netscaler-client-ip-insertion-protocol.txt'
'doc/network-namespaces.txt' -> '/usr/local/haproxy/doc/haproxy/network-namespaces.txt'
'doc/peers-v2.0.txt' -> '/usr/local/haproxy/doc/haproxy/peers-v2.0.txt'
'doc/peers.txt' -> '/usr/local/haproxy/doc/haproxy/peers.txt'
'doc/proxy-protocol.txt' -> '/usr/local/haproxy/doc/haproxy/proxy-protocol.txt'
'doc/regression-testing.txt' -> '/usr/local/haproxy/doc/haproxy/regression-testing.txt'
'doc/seamless_reload.txt' -> '/usr/local/haproxy/doc/haproxy/seamless_reload.txt'
(1/10) Purging gcc (10.3.1_git20211027-r0)
(2/10) Purging binutils (2.37-r3)
(3/10) Purging libatomic (10.3.1_git20211027-r0)
(4/10) Purging libgomp (10.3.1_git20211027-r0)
(5/10) Purging libgphobos (10.3.1_git20211027-r0)
(6/10) Purging make (4.3-r0)
(7/10) Purging mpc1 (1.2.1-r0)
(8/10) Purging mpfr4 (4.1.0-r0)
(9/10) Purging isl22 (0.22-r0)
(10/10) Purging gmp (6.2.1-r0)
Executing busybox-1.34.1-r3.trigger
OK: 35 MiB in 45 packages
Removing intermediate container f06302838561
 ---> 8e143c533a59
Step 9/9 : ENTRYPOINT /tmp/haproxycfg.sh
 ---> Running in 346caff40165
Removing intermediate container 346caff40165
 ---> 804d81616657
Successfully built 804d81616657
Successfully tagged haproxy:v1.0
[root@master haproxy]# docker images
REPOSITORY          TAG       IMAGE ID       CREATED          SIZE
haproxy             v1.0      804d81616657   19 minutes ago   83.8MB
alpine              latest    c059bfaa849c   2 weeks ago      5.59MB
centos              latest    5d0da3dc9764   2 months ago     231MB
[root@localhost ~]# docker run -d --name apache yanghaixx/httpd:latest
[root@localhost ~]# docker run -d --name nginx yanghaixx/nginx:v1.0
[root@master ~]# docker run -d --name haproxy -p 80:80 -e RSs="172.17.0.4 172.17.0.5" haproxy:v1.0
19272fbe35e39748c4fc0aff191f16fb60dbfac43c5cd101400c197f89348bca

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值