Dockerfile如何使用alpine系统制作haproxy镜像
作者:slyybw
这篇文章主要介绍了Dockerfile如何使用alpine系统制作haproxy镜像问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
Dockerfile目录结构
[root@localhost ~]# tree haproxy_alpinelinux/ haproxy_alpine/ ├── Dockerfile └── files ├── haproxy-2.4.0.tar.gz ├── haproxycfg.sh ├── install.sh └── sysctl.conf 1 directory, 5 files
拉取alpine镜像
[root@localhost ~]# [root@localhost ~]# docker pull alpine Using default tag: latest latest: Pulling from library/alpine Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300 Status: Image is up to date for alpine:latest docker.io/library/alpine:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest c059bfaa849c 2 weeks ago 5.59MB
Dockerfile文件内容
[root@localhost ~]# cat haproxy_alpine/Dockerfile FROM alpine LABEL MAINTAINER "syblyw0806 1234567890@qqq.com" ENV version 2.4.0 ADD files/haproxy-${version}.tar.gz /opt/ ADD files/install.sh /opt/ ADD files/haproxycfg.sh /opt/ ADD files/sysctl.conf /opt/ RUN /opt/install.sh ENTRYPOINT /opt/haproxycfg.sh
安装haproxy的脚本
[root@localhost ~]# cat haproxy_alpine/files/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 /opt/haproxy-${version} 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 /opt/haproxy-${version}/ /opt/install.sh
haproxy配置文件
[root@localhost ~]# cat haproxy_alpine/files/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
sysctl.conf
[root@localhost ~]# cat haproxy_alpine/files/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@localhost ~]# docker build -t haproxy:v3.0 haproxy_alpinelinux/ Sending build context to Docker daemon 3.602MB Step 1/9 : FROM alpine ---> c059bfaa849c Step 2/9 : LABEL MAINTAINER "syblyw0806 1234567890@qqq.com" ---> Using cache ---> bbfbe82a7f48 Step 3/9 : ENV version 2.4.0 ---> Using cache ---> dccb41beb82b Step 4/9 : ADD files/haproxy-${version}.tar.gz /opt/ ---> Using cache ---> 856cb1d8e0de Step 5/9 : ADD files/install.sh /opt/ ---> 589a939efe69 Step 6/9 : ADD files/haproxycfg.sh /opt/ ---> 37bd73459586 Step 7/9 : ADD files/sysctl.conf /opt/ ---> be1b5cd53414 Step 8/9 : RUN /opt/install.sh ---> Running in 6fd54c54e040 Removing intermediate container 6fd54c54e040 ---> e1bf872ca416 Step 9/9 : ENTRYPOINT /opt/haproxycfg.sh ---> Running in 89515ad8dc41 Removing intermediate container 89515ad8dc41 ---> 4f4d41c2eebb Successfully built 4f4d41c2eebb Successfully tagged haproxy:v3.0 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE haproxy v3.0 4f4d41c2eebb About an hour ago 83.8MB
启动容器
[root@localhost ~]# docker run -d --name haproxy_alpinelinux -p 1234:80 -e RSs="172.17.0.2 172.17.0.3" haproxy:v3.0 // 启动一个httpd容器和一个nginx容器 be589a4d9689427ec0810c2f2b28aa59dd4bba425590f0277b4e4f82d3b973e8 [root@localhost ~]# docker run -d --name httpd syblyw0806/httpd:v2.0 0706632d35fbf02807c5668cae27fc8d3fe3406f198dff5988370c688c630b5b [root@localhost ~]# docker run -d --name nginx nginx 80ccbde27a8e0d5545b8422f809c9b09f178bff7a20d5bc194925bd707a34af7 [root@localhost ~]# docker exec -it haproxy_alpinelinux /bin/sh / # ss -anlt // 这里如果没有ss命令,需要安装iproute2这个包 State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:8189 0.0.0.0:* LISTEN 0 128 0.0.0.0:80 0.0.0.0:* [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES be589a4d9689 haproxy:v3.0 "/bin/sh -c /opt/hap…" About a minute ago Up About a minute 0.0.0.0:1234->80/tcp, :::1234->80/tcp haproxy_alpinelinux 80ccbde27a8e nginx "/docker-entrypoint.…" 10 minutes ago Up 10 minutes 80/tcp nginx 0706632d35fb syblyw0806/httpd:v2.0 "/usr/local/apache/b…" 10 minutes ago Up 10 minutes 80/tcp httpd
访问测试
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。