用dockerfile方式做一个haproxy的镜像


要求:要用ENV定义RS变量,但是不能给值,而是在docker run的时候通过-e选项去传递变量的值

创建目录结构、上传安装包

[root@localhost ~]# mkdir haproxy
[root@localhost ~]# cd haproxy/
[root@localhost haproxy]# mkdir files
[root@localhost haproxy]# touch Dockerfile
[root@localhost haproxy]# cd files/
[root@localhost files]# ls
haproxy-2.5.0.tar.gz

编写Dockerfile

[root@localhost haproxy]# cat Dockerfile 
#基础镜像
FROM centos

#作者信息
LABEL MAINTAINER wjj200112 1234567890@qq.com

ENV VERSION 2.5.0
WORKDIR /usr/src

#传输文件
ADD files/haproxy-${version}.tar.gz  /usr/src
ADD files/haproxy.cfg  /usr/local/haproxy/conf/haproxy.cfg
ADD files/start.sh  /usr/local
ADD files/install.sh /usr/local

#安装
RUN ["/bin/bash","-c","usr/local/install.sh"]    

#暴露端口
EXPOSE 80

#启动命令
CMD ["usr/local/start.sh"]

提供Dockerfile中所需的文件

安装脚本

[root@localhost haproxy]# cd files/
[root@localhost files]# touch install.sh
[root@localhost files]# chmod +x install.sh 
[root@localhost files]# vim install.sh 
[root@localhost files]# cat install.sh 
#!/bin/bash

rm -rf /etc/yum.repos.d/*  
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F'"' 'NR==5{print $2}' /etc/os-release).repo 
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo 
yum clean all && yum makecache 
yum -y install make gcc gcc-c++ pcre-devel bzip2-devel openssl-devel systemd-devel
useradd -r -M -s /sbin/nologin haproxy 
cd /usr/src/haproxy-${version}  
make clean && \
make -j $(nproc)  \
    TARGET=linux-glibc  \
    USE_OPENSSL=1  \
    USE_ZLIB=1  \
    USE_PCRE=1  \
    USE_SYSTEMD=1 && \
make install PREFIX=/usr/local/haproxy
cp haproxy  /usr/sbin/  
echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf 
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
rm -rf /usr/src/haproxy-2.5.0/  /var/cache/*
yum -y remove make gcc gcc-c++

启动脚本

[root@localhost files]# touch start.sh
[root@localhost files]# chmod +x start.sh 
[root@localhost files]# vim start.sh 
[root@localhost files]# cat start.sh 
#!/bin/bash

haproxy -f etc/haproxy/haproxy.cfg
/bin/bash

haproxy配置文件

[root@localhost files]# cat haproxy.cfg 
#--------------全局配置----------------
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
    server web01 192.168.47.2:80 check inter 2000 fall 5   
    server web01 192.168.47.3:80 check inter 2000 fall 5

构建haproxy镜像

[root@localhost ~]# docker build -t haproxy:v0.1 haproxy/
Complete!
Removing intermediate container f5795c76586f
 ---> 6e64f9df9842
Step 10/11 : EXPOSE 80
 ---> Running in 2eae63d97fc5
Removing intermediate container 2eae63d97fc5
 ---> 06678f570981
Step 11/11 : CMD ["scripts/start.sh"]
 ---> Running in a0a53bdbe7c8
Removing intermediate container a0a53bdbe7c8
 ---> 36b5fc68cb96
Successfully built 36b5fc68cb96
Successfully tagged haproxy:v0.1

基于镜像创建haproxy容器

[root@localhost ~]# docker run --name haproxy -dit -p 80:80 haproxy:v0.1
0bcf6d8459d2a878029478b18b5ede2050ad094dfd4a8b9993e291e9d7bb5bac
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE          COMMAND              CREATED         STATUS         PORTS                               NAMES
0bcf6d8459d2   haproxy:v0.1   "scripts/start.sh"   7 seconds ago   Up 6 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   haproxy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值