dockerfile编写haproxy镜像

dockerfile编写haproxy镜像,第1张

dockerfile编写haproxy镜像

dockerfile结构

[root@localhost ~]# tree haproxy/
haproxy/
├── Dockerfile
└── files
    ├── entrypoint.sh
    ├── haproxy-1.7.8.tar.gz
    ├── haproxy.cfg
    └── install.sh

1 directory, 5 files

dockerfile

[root@localhost ~]# cat haproxy/Dockerfile 
FROM centos

ENV version 1.7.8
ENV PATH /usr/local/harpoxy/sbin:$PATH

ADD files/haproxy-${version}.tar.gz /usr/src/
ADD files/install.sh /tmp/
ADD files/entrypoint.sh /

EXPOSE 8189 80

RUN ["/bin/bash","-c","/tmp/install.sh"]

WORKDIR /usr/local/haproxy/

CMD ["/entrypoint.sh"]

entrypoint.sh

[root@localhost ~]# cat haproxy/files/entrypoint.sh 
#!/bin/bash

cat > /usr/local/haproxy/conf/haproxy.cfg <> /usr/local/haproxy/conf/haproxy.cfg < 

install.sh

[root@localhost ~]# cat haproxy/files/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 -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

useradd -r -M -s /sbin/nologin haproxy

cd /usr/src/haproxy-${version}
make clean && 
make 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

mkdir -p /usr/local/haproxy/conf
yum -y remove make gcc gcc-c++ && 
rm -rf /tmp/* /var/cache/*

 做镜像

[root@localhost ~]# docker build -t yangyonghu/haproxy:v0.3 haproxy/
Sending build context to Docker daemon  1.754MB
Step 1/10 : FROM centos
 ---> 5d0da3dc9764
Step 2/10 : ENV version 1.7.8
 ---> Running in affae3ffac91
Removing intermediate container affae3ffac91
 ---> 139159f97ad8
Step 3/10 : ENV PATH /usr/local/harpoxy/sbin:$PATH
 ---> Running in ca335770ef74
Removing intermediate container ca335770ef74
 ---> d2d16324b306
Step 4/10 : ADD files/haproxy-${version}.tar.gz /usr/src/
 ---> 8300658bbf39
Step 5/10 : ADD files/install.sh /tmp/
 ---> 31bf0c1dd3e3
Step 6/10 : ADD files/entrypoint.sh /
 ---> 7b6b4d3400f6
Step 7/10 : EXPOSE 8189 80
 ---> Running in 5858d95a4a58
Removing intermediate container 5858d95a4a58
 ---> 722c7a2eaecb
Step 8/10 : RUN ["/bin/bash","-c","/tmp/install.sh"]
 ---> b2e87962da48
Step 9/10 : WORKDIR /usr/local/haproxy/
 ---> Running in b22715a19eee
Removing intermediate container b22715a19eee
 ---> d6fa8993457f
Step 10/10 : CMD ["entrypoint.sh"]
 ---> Running in 5096deab9936
Removing intermediate container 5096deab9936
 ---> 2d2ae019decf
Successfully built 2d2ae019decf
Successfully tagged yangyonghu/haproxy:v0.3
[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
yangyonghu/haproxy   v0.3      2d2ae019decf   2 minutes ago   240MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago      703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago      550MB
busybox              latest    d23834f29b38   12 days ago     1.24MB
centos               latest    5d0da3dc9764   2 months ago    231MB

创建一个容器来运行haproxy镜像

[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
yangyonghu/haproxy   v0.3      d75fa67c9f17   5 minutes ago   399MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago      703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago      550MB
busybox              latest    d23834f29b38   12 days ago     1.24MB
centos               latest    5d0da3dc9764   2 months ago    231MB
[root@localhost ~]# docker run -dit --name haproxy -v /config/:/tmp -p 80:80 -p 8189:8189 d75fa67c9f17
ded46827fd7c4f590a80f52131065d8dc0b8bd02ddc90175dfabe1c81d59cf09
[root@localhost ~]#  docker exec -it haproxy /bin/bash
[root@ded46827fd7c haproxy]# ss -antl
State            Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port           Process           
LISTEN           0                 128                                0.0.0.0:80                              0.0.0.0:*                                
LISTEN           0                 128                                0.0.0.0:8189                            0.0.0.0:*                                
[root@ded46827fd7c haproxy]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
15: eth0@if16:  mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:58:01 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.88.1/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever

另起终端查看主机上的端口号

[root@localhost ~]# ss -antl
State               Recv-Q              Send-Q                           Local Address:Port                             Peer Address:Port              
LISTEN              0                   128                                    0.0.0.0:8189                                  0.0.0.0:*                 
LISTEN              0                   128                                    0.0.0.0:111                                   0.0.0.0:*                 
LISTEN              0                   128                                    0.0.0.0:80                                    0.0.0.0:*                 
LISTEN              0                   128                                    0.0.0.0:22                                    0.0.0.0:*                 
LISTEN              0                   128                                       [::]:8189                                     [::]:*                 
LISTEN              0                   128                                       [::]:111                                      [::]:*                 
LISTEN              0                   128                                       [::]:80                                       [::]:*                 
LISTEN              0                   128                                       [::]:22                                       [::]:*         

创建两个新的容器使用apache镜像来测试haproxy

[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
yangyonghu/haproxy   v0.3      d75fa67c9f17   9 minutes ago   399MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago      703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago      550MB
busybox              latest    d23834f29b38   12 days ago     1.24MB
centos               latest    5d0da3dc9764   2 months ago    231MB
[root@localhost ~]# docker run -dit --name httpd --rm 677843bb4e15
46a043a66ce7353e7c58d207ea6f9c9d8f4e432763afc05de5c707ff340e991d
[root@localhost ~]# docker exec -it httpd /bin/bash
[root@46a043a66ce7 src]# ss -antl
State            Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port           Process           
LISTEN           0                 128                                0.0.0.0:80                              0.0.0.0:*                                
[root@46a043a66ce7 src]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
17: eth0@if18:  mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:58:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.88.2/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever
[root@46a043a66ce7 src]# vi /usr/local/apache/htdocs/index.html
[root@46a043a66ce7 src]# cat /usr/local/apache/htdocs/index.html
RS1
[root@46a043a66ce7 src]# 

第二个

[root@localhost ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
yangyonghu/haproxy   v0.3      d75fa67c9f17   13 minutes ago   399MB
yangyonghu/apache    v0.5      677843bb4e15   3 days ago       703MB
yangyonghu/nginx     v0.2      2a83aeba09ed   8 days ago       550MB
busybox              latest    d23834f29b38   12 days ago      1.24MB
centos               latest    5d0da3dc9764   2 months ago     231MB
[root@localhost ~]# docker run -dit --name apache --rm 677843bb4e15
de053e2dc3055614936ccdbd61158fdea33818e44a064e00e01d3dbc0c3c938f
[root@localhost ~]# docker exec -it apache /bin/bash
[root@de053e2dc305 src]# ss -antl
State            Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port           Process           
LISTEN           0                 128                                0.0.0.0:80                              0.0.0.0:*                                
[root@de053e2dc305 src]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
19: eth0@if20:  mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:c0:a8:58:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.88.3/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever

[root@de053e2dc305 src]# vi /usr/local/apache/htdocs/index.html
[root@de053e2dc305 src]# cat /usr/local/apache/htdocs/index.html
RS2

访问页面做测试

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5669826.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存