- Dockerfile制作镜像
- dockerfile制作haproxy镜像
- 目录结构
- Dockerfile文件
- files目录文件
- 制作haproxy镜像
- 测试
- 查看
[root@localhost haproxy]# tree
.
├── Dockerfile
└── files
├── haproxy-2.4.0.tar.gz
├── haproxy.cfg
├── haproxy.sh
├── run_haproxy.sh
└── sysctl.conf
Dockerfile文件
[root@localhost haproxy]# cat Dockerfile
FROM centos
LABEL MAINTAINER "yyy 123@com"
ENV version 2.4.0
WORKDIR /usr/local
ADD files/haproxy-${version}.tar.gz /usr/local
ADD files/haproxy.sh /usr/local
ADD files/haproxy.cfg /etc/haproxy/
ADD files/run_haproxy.sh /usr/local
ADD files/sysctl.conf /etc/
RUN ["/bin/bash","-c","/usr/local/haproxy.sh"]
EXPOSE 80
CMD ["/usr/local/run_haproxy.sh"]
files目录文件
[root@localhost files]# tree
.
├── haproxy-2.4.0.tar.gz
├── haproxy.cfg
├── haproxy.sh
├── run_haproxy.sh
└── sysctl.conf
[root@localhost haproxy]# cat files/haproxy.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==2{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 -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
useradd -r -M -s /sbin/nologin haproxy
cd /usr/local
cd haproxy-$version
make clean
make -j $(grep 'processor' /proc/cpuinfo |wc -l)
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/
[root@localhost files]# chmod +x haproxy.sh
haproxy.cfg文件
[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 web1 172.17.0.3:80 check inter 3000 fall 5
server web2 172.17.0.4:80 check inter 3000 fall 5
#server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5
sysctl.conf文件
[root@localhost files]# 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
run_haproxy.sh
[root@localhost files]# cat run_haproxy.sh #!/bin/bash haproxy -f /etc/haproxy/haproxy.cfg tail -f /etc/hosts [root@localhost files]# chmod +x run_haproxy.sh制作haproxy镜像
[root@localhost haproxy]# docker build -t haproxy:v0.1 . Sending build context to Docker daemon 3.602MB Step 1/11 : FROM centos ---> 5d0da3dc9764 Step 2/11 : ENV version 2.4.0 ---> Running in 14931f984d02 Removing intermediate container 14931f984d02 ---> 2a6420f3d238 Step 3/11 : WORKDIR /usr/local ---> Running in 815abc60d5ce Removing intermediate container 815abc60d5ce ---> 90adbb753f42 ................................. [root@localhost haproxy]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE haproxy v0.1 949e1a86fb6b 3 minutes ago 578MB busybox latest d23834f29b38 10 days ago 1.24MB centos 8 5d0da3dc9764 2 months ago 231MB centos latest 5d0da3dc9764 2 months ago 231MB测试
//这一步是从我自己的镜像仓库拉取了一个http的镜像
[root@localhost haproxy]# docker pull yzy0923/httpd:v0.01
v0.01: Pulling from yzy0923/httpd
a1d0c7532777: Already exists
b71617e0b577: Pulling fs layer
c12e8de6a34d: Downloading 56.27MB/170.5MB
v0.01: Pulling from yzy0923/httpd
a1d0c7532777: Already exists
b71617e0b577: Pull complete
c12e8de6a34d: Pull complete
Digest: sha256:a5e7f7c00ccfab0b21819ebf36fa52d0ea39851968ccb1bd7f4ddaf9330f12ca
Status: Downloaded newer image for yzy0923/httpd:v0.01
docker.io/yzy0923/httpd:v0.01
[root@localhost haproxy]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v0.1 949e1a86fb6b 14 minutes ago 578MB
yzy0923/httpd v0.01 a88b702deaee 40 hours ago 701MB
busybox latest d23834f29b38 10 days ago 1.24MB
centos 8 5d0da3dc9764 2 months ago 231MB
centos latest 5d0da3dc9764 2 months ago 231MB
[root@localhost haproxy]# cd
//创建目录存放不同的index.html文件进行测试
[root@localhost ~]# mkdir -p test/{html1,html2}
[root@localhost ~]# vim test/html1
[root@localhost ~]# vim test/html1/index.html
[root@localhost ~]# vim test/html2/index.html
[root@localhost ~]# cat test/html1/index.html
this web1 http
[root@localhost ~]# cat test/html2/index.html
this is web2 httpd
//用我们刚刚拉下来创建测试容器
[root@localhost ~]# docker run -d -v /root/test/html1:/usr/local/apache/htdocs --name web1 yzy0923/httpd:v0.01
e8089e31b65998d674318ccbc0abcec0c92cc49292075f213aa01e1d840e366b
[root@localhost ~]# docker run -d -v /root/test/html2:/usr/local/apache/htdocs --name web2 yzy0923/httpd:v0.01
51b1940589a37e6b8321e39139a5b0cdf428b5257d314c63131eb3b2e1cf9d7a
[root@localhost ~]# docker ps
ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
51b1940589a3 yzy0923/httpd:v0.01 "/usr/local/apache/b…" 8 seconds ago Up 7 seconds web2
e8089e31b659 yzy0923/httpd:v0.01 "/usr/local/apache/b…" 24 seconds ago Up 23 seconds web1
[root@localhost ~]# curl 172.17.0.3
this web1 http
[root@localhost ~]# curl 172.17.0.4
this is web2 httpd
//用刚刚创建的haproxy镜像创建容器
[root@localhost ~]# docker run -d -p 80:80 -p 8189:8189 --name haproxy haproxy:v0.1
adaa66ef2744bce2e80304ff81d78de3b8fe8b97562cbc8f91ddeb0ce6ddb8f7
[root@localhost ~]# docker ps
ConTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
adaa66ef2744 haproxy:v0.1 "/usr/local/run_hapr…" 14 minutes ago Up 14 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp haproxy
51b1940589a3 yzy0923/httpd:v0.01 "/usr/local/apache/b…" 16 minutes ago Up 16 minutes web2
e8089e31b659 yzy0923/httpd:v0.01 "/usr/local/apache/b…" 16 minutes ago Up 16 minutes web1
查看



