Dockerfile如何使用alpine系統(tǒng)制作haproxy鏡像
Dockerfile目錄結(jié)構(gòu)
[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文件內(nèi)容
[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.shhaproxy配置文件
[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 -dbsysctl.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
構(gòu)建鏡像
[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啟動(dòng)容器
[root@localhost ~]# docker run -d --name haproxy_alpinelinux -p 1234:80 -e RSs="172.17.0.2 172.17.0.3" haproxy:v3.0 // 啟動(dòng)一個(gè)httpd容器和一個(gè)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 // 這里如果沒(méi)有ss命令,需要安裝iproute2這個(gè)包 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
訪問(wèn)測(cè)試


總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
從入門(mén)到生產(chǎn)級(jí)配置詳解Docker部署Nginx的實(shí)戰(zhàn)指南
Nginx 作為高性能的 HTTP 服務(wù)器和反向代理服務(wù)器,在 Web 架構(gòu)中占據(jù)著核心地位,本文將帶你從零開(kāi)始學(xué)習(xí)使用?Docker?容器化部署?Nginx?的完整流程,有需要的小伙伴可以了解下2026-05-05
Docker-compose搭建Redis集群(Sentinel)的實(shí)現(xiàn)
本文主要介紹了Docker-compose搭建Redis集群(Sentinel)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
使用minikube安裝使用單機(jī)版K8S方式(docker)
本文介紹了如何在centos7上使用minikube快速搭建單機(jī)版k8s,主要步驟包括:下載kubectl和docker,創(chuàng)建新用戶,下載和安裝minikube,驗(yàn)證安裝情況,最后,通過(guò)執(zhí)行minikube和minikubestop啟停K8s,或使用minikubedelete徹底刪除K8S的數(shù)據(jù)2024-10-10
docker安裝Redis高可用實(shí)現(xiàn)一主二從三哨兵
redis提供了哨兵模式保證redis實(shí)現(xiàn)高可用,本文主要介紹了docker安裝Redis高可用實(shí)現(xiàn)一主二從三哨兵,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02
Docker數(shù)據(jù)卷常用操作代碼實(shí)例
這篇文章主要介紹了Docker數(shù)據(jù)卷常用操作代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
docker修改默認(rèn)存儲(chǔ)路徑和網(wǎng)段的操作過(guò)程
本文介紹了如何修改Docker的數(shù)據(jù)目錄和默認(rèn)網(wǎng)段,以避免與其他系統(tǒng)或應(yīng)用的網(wǎng)絡(luò)配置沖突,詳細(xì)步驟包括停止Docker服務(wù)、修改Docker配置文件、復(fù)制或移動(dòng)現(xiàn)有數(shù)據(jù)到新目錄、重啟Docker服務(wù)以及驗(yàn)證設(shè)置2025-12-12
Docker實(shí)現(xiàn)發(fā)布一個(gè)springboot項(xiàng)目
文章介紹了使用Docker打包SpringBoot項(xiàng)目的過(guò)程,包括基本用法、maven源碼打包法、使用maven插件打包和docker-maven-plugin插件打包,并詳細(xì)講述了每種方法的操作步驟和配置細(xì)節(jié),最后總結(jié)了使用插件打包的優(yōu)勢(shì)2026-04-04

