docker镜像制作(二)

一、基于alpine 基础镜像制作nginx镜像

[root@localhost7B alpine]#docker pull alpine
[root@localhost7B alpine]# cd /data/dockerfile/system/alpine/
[root@localhost7B alpine]# ls
build_alpine_nginx.sh  Dockerfile  index.html  nginx-1.14.2.tar.gz  nginx.conf  repositories
[root@localhost7B alpine]# 
[root@localhost7B alpine]# cat index.html 
alpine  test nginx 
[root@localhost7B alpine]#
cat repositories https://mirrors.ustc.edu.cn/alpine/latest-stable/main https://mirrors.ustc.edu.cn/alpine/latest-stable/community
#配置文件 [root@localhost7B alpine]# cat nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /data/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } #dockerfile 文件 [root@localhost7B alpine]# cat Dockerfile #alpine nginx编译安装 FROM alpine:latest maintainer yuanbangchen "www.abc.com" # 软件源地址 COPY repositories /etc/apk/repositories RUN apk update && apk add iotop gcc libgcc libc-dev libcurl libc-utils pcre-dev zlib-dev libnfs make pcre pcre2 zip unzip net-tools pstree wget libevent libevent-dev iproute2 ADD nginx-1.14.2.tar.gz /opt/ RUN cd /opt/nginx-1.14.2 && ./configure --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/bin/ RUN addgroup -g 2019 -S nginx && adduser -s /sbin/nologin -S -D -u 2019 -G nginx nginx COPY nginx.conf /apps/nginx/conf/nginx.conf ADD index.html /data/nginx/html/ RUN chown nginx.nginx /data/nginx/ /apps/nginx/ -R EXPOSE 80 443 ENTRYPOINT ["nginx"] CMD ["-g","daemon off;"]
[root@localhost7B alpine]# cat build_alpine_nginx.sh
#!/bin/bash
docker build -t alpine_ningx:test .

#构造
[root@localhost7B alpine]# chmod  +x build_alpine_nginx.sh

 [root@localhost7B alpine]# ./build_alpine_nginx.sh 


[root@localhost7B alpine]# docker images
REPOSITORY    TAG    IMAGE ID        CREATED    SIZE
alpine_ningx    test   32dad8880e31    4 hours ago    208MB
alpine        latest    9c6f07244728    2 months ago    5.54MB

#运行容器测试

[root@localhost7B alpine]# docker run  -it  -d  --name nginxA  -p 80:80 alpine_ningx:test

[root@localhost7A ~]# curl 192.168.80.110
alpine test nginx

 

 

二、基于Ubuntu 基础镜像制作nginx镜像

[root@localhost7B ubuntu]# docker pull ubuntu
[root@localhost7B ubuntu]# pwd
/data/dockerfile/system/ubuntu

[root@localhost7B ubuntu]#
ls build_ubuntu_nginx.sh Dockerfile index.html nginx-1.14.2.tar.gz nginx.conf sources.list [root@localhost7B ubuntu]# cat index.html ubuntu test nginx
#配置文件 [root@localhost7B ubuntu]#
cat sources.list deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse # deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse # deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
#配置文件 [root@localhost7B ubuntu]# cat nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /data/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

#dockerfile文件 [root@localhost7B ubuntu]#
cat Dockerfile #ubuntu nginx编译安装 FROM ubuntu:20.04 maintainer yuanbangchen "www.abc.com" # 软件源地址 COPY sources.list /etc/apt/sources.list RUN apt update && apt install -y iproute2 nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute gcc openssh-server net-tools vim iotop unzip zip make && touch /tmp/linux.txt ADD nginx-1.14.2.tar.gz /usr/local/src RUN cd /usr/local/src/nginx-1.14.2 && ./configure --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/bin && rm -rf /usr/local/src/nginx-1.16.1 && rm -rf /usr/local/src/nginx-1.16.1.tar.gz ADD nginx.conf /apps/nginx/conf/nginx.conf ADD index.html /data/nginx/html/ RUN groupadd -g 2019 nginx && useradd -g nginx -s /usr/sbin/nologin -u 2019 nginx RUN chown -R nginx.nginx /apps/nginx /data/nginx EXPOSE 80 443 ENTRYPOINT ["nginx"] CMD ["-g","daemon off;"] #构造 [root@localhost7B ubuntu]# cat build_ubuntu_nginx.sh #!/bin/bash docker build -t ubuntu_ningx:test . [root@localhost7B ubuntu]# [root@localhost7B ubuntu]# chmod +x build_ubuntu_nginx.sh [root@localhost7B ubuntu]# [root@localhost7B ubuntu]# ./build_ubuntu_nginx.sh
[root@localhost7B ubuntu]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu_ningx test decfdb992cc3 28 minutes ago 503MB
alpine_ningx test 32dad8880e31 4 hours ago 208MB
ubuntu 20.04 680e5dfb52c7 7 days ago 72.8MB
alpine latest 9c6f07244728 2 months ago 5.54MB

#运行容器测试 [root@localhost7B ubuntu]# docker run
-it -d -p 80:80 ubuntu_ningx:test
[root@localhost7A ~]# curl 192.168.80.110
ubuntu test nginx

[root@localhost7B ubuntu]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu_ningx test decfdb992cc3 28 minutes ago 503MB
alpine_ningx test 32dad8880e31 4 hours ago 208MB
ubuntu 20.04 680e5dfb52c7 7 days ago 72.8MB
alpine latest 9c6f07244728 2 months ago 5.54MB

 

三、基于cenots 基础镜像制作nginx镜像

1.1.镜像的分层结构目录,按照业务类型或系统类型等方式划分创建目录环境,方便后期镜像比较多的时候进行分类
mkdir /data/dockerfile/{web/{nginx,tomcat,jdk},system/{centos,ubuntu,redhat}} -pv
[root@localhost7B nginx]# tree  /data/dockerfile/
/data/dockerfile/
├── system
│   ├── centos
│   │   ├── build-Centos.sh
│   │   └── Dockerfile
│   ├── redhat
│   └── ubuntu
└── web
    ├── jdk
    ├── nginx
    │   ├── build-centos-nginx.sh
    │   ├── Dockerfile
    │   └── index.html
    └── tomcat


1.2.下载centos镜像为基础
[root@localhost7B ~]# docker pull centos:7

1.3.编写Dockerfile文件,在centos基础镜像上添加基本软件程序
[root@localhost7B ~]#cd /data/dockerfile/centos/
[root@localhost7B centos]# cat  Dockerfile 
# 基础镜像
FROM centos:7
MAINTAINER yuanbangchen 12345678@qq.com
#安装基本软件
RUN yum install wget  -y
RUN wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN yum install -y vim  tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
#添加系统账户
RUN groupadd www -g 2020 && useradd www -u 2020 -g www
RUN yum clean all && rm -f /etc/localtime && ln -s ../usr/share/zoneinfo/Asia/Shanghai /etc/localtime
1.4.构造镜像,名为centos-base:v1
[root@localhost7B centos]# cat build-Centos.sh 
#!/bin/bash
docker build -t centos-base:v1 .

1.5.构造过程
[root@localhost7B centos]# chmod + x build-Centos.sh 
[root@localhost7B centos]# ./build-Centos.sh 
Sending build context to Docker daemon  3.584kB
Step 1/7 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/7 : MAINTAINER yuanbangchen 12345678@qq.com
...
省略
...
Step 7/7 : RUN groupadd www -g 2020 && useradd www -u 2020 -g www
 ---> Running in 94b0cd1b1ece
Removing intermediate container 94b0cd1b1ece
 ---> 568629c634fc
Successfully built 568629c634fc
Successfully tagged centos-base:v1

1.6.查看镜像
[root@localhost7B centos]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos-base         v1                  568629c634fc        7 minutes ago       782MB
centos              7                   eeb6ee3f44bd        13 months ago       204MB

1.7.测试
[root@localhost7B centos]# docker run  -it  --rm  centos-base:v1  bash
[root@ab17aab986bd /]# 
[root@ab17aab986bd /]# cat /etc/passwd  | grep www
www:x:2020:2020::/home/www:/bin/bash


2.镜像的分层结构目录,按照业务类型或系统类型等方式划分创建目录环境,方便后期镜像比较多的时候进行分类
2.1业务镜像
[root@localhost7B ~]#cd /data/dockerfile/web/nginx/

2.2 编写Dockerfile文件
[root@localhost7B nginx]# vim Dockerfile 
#基准镜像
FROM centos-base:v1
#作者信息
MAINTAINER "zzhzzjol"
#工作目录
WORKDIR /usr/local/src/
#定义环境变量
ENV NG_VERSION nginx-1.21.0
#安装epel仓库
RUN yum -y install epel-release
#安装wget
RUN yum -y install wget
#下载nginx文件并解压
RUN wget http://nginx.org/download/$NG_VERSION.tar.gz && tar xzvf $NG_VERSION.tar.gz
#安装编译依赖包
RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel && yum install -y pcre-devel libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data
#清理仓库
RUN yum clean all
#创建nginx用户
RUN useradd -M -s /sbin/nologin nginx
#切换工作目录
WORKDIR /usr/local/src/$NG_VERSION
#编译安装nginx
RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
#复制测试页面到容器中
ADD index.html /usr/local/nginx/html
#设置容器中要挂在到宿主机的目录
VOLUME /usr/local/nginx/html
#设置sbin环境变量
ENV PATH /usr/local/nginx/sbin:$PATH
#暴露容器80端口
EXPOSE 80/tcp
#当ENTRYPOINT和CMD连用时,CMD的命令是ENTRYPOINT命令的参数,两者连用相当于nginx -g "daemon off;"而当一起连用的时候命令格式最好一致(这里选择的都是json格式的是成功的,如果都是sh模式可以试一下)
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]

2.3测试页面
[root@localhost7B nginx]# cat index.html 
dockerfile nginx test


2.4构造镜像,名为nginx:v1
[root@localhost7B nginx]# cat build-centos-nginx.sh 
#!/bin/bash
docker build -t nginx:v1 .

[root@localhost7B nginx]# chmod +x build-centos-nginx.sh 
[root@localhost7B nginx]# ./build-centos-nginx.sh 
Step 11/16 : ADD index.html /usr/local/nginx/html
 .....
 .....
Step 16/16 : CMD ["-g","daemon off;"]
 ---> Running in 5a0135bfef6d
Removing intermediate container 5a0135bfef6d
 ---> e6b2d5a5a6b4
Successfully built e6b2d5a5a6b4
Successfully tagged nginx:v1


2.5查看测试
[root@localhost7B nginx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
nginx               v1                  e6b2d5a5a6b4        About a minute ago   1.16GB
centos-base         v1                  568629c634fc        51 minutes ago       782MB
centos              7                   eeb6ee3f44bd        13 months ago        204MB

[root@localhost7B nginx]# docker run  -it -d  --name nginx  -p 80:80  nginx:v1

[root@localhost7B nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
0c879980959f        nginx:v1            "nginx -g 'daemon of…"   20 seconds ago      Up 18 seconds       0.0.0.0:80->80/tcp   nginx

[root@localhost7B nginx]# docker exec  -it nginx  bash

[root@localhost7A ~]# curl 192.168.80.110:80
dockerfile nginx test

 

四.基于dockerfile实现单机版的Haproxy+Tomcat

 基于cenots 基础镜像制作tomcat +  haproxy镜像 

一.在Cenots基础上制作JDK镜像
[root@localhost7B jdk]# ls
build-command.sh  Dockerfile  jdk  jdk-8u291-linux-x64.tar.gz

1.配置文件
[root@localhost7B jdk]# cat  jdk
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=/usr/local/jdk/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH
 
 2.Dockerfile文件
[root@localhost7B jdk]# cat Dockerfile 
#在Centos基础镜像+基础软件上安装JDK
FROM centos-base:v1 
MAINTAINER yuanbangchen  "123456@qq.com" 
ADD jdk-8u291-linux-x64.tar.gz /usr/local/src/ 
RUN ln -sv /usr/local/src/jdk1.8.0_291  /usr/local/jdk 
#直接定义变量或在/etc/profile/添加变量,这里定义的全局用户都可以使用.如果tomcat 使用的是www 用户,要确保此用户能使用这些变量。建议定义在/etc/profile
ADD jdk  /etc/profile    
#测试失败,定义在/etc/profile.d/下,使用www用户无法使用变量,因为进入容器时,而不是进入终端,不加载profile 下的文件。所以要定义ENV变量。
#ADD jdkv1  /etc/profile.d/jdk    
#RUN source /etc/profile.d/jdk
#定义变量
ENV JAVA_HOME  /usr/local/jdk 
ENV JRE_HOME  $JAVA_HOME/jre 
ENV CLASSPATH  $JAVA_HOME/lib/:$JRE_HOME/lib/ 
ENV PATH  $PATH:$JAVA_HOME/bin
#设置时区:
RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

3.构造镜像
[root@localhost7B jdk]# chmod +x  build-command.sh 
[root@localhost7B jdk]# cat build-command.sh 
#!/bin/bash 
docker  build -t jdk-base:v1.8.0_291 .

4.查看
[root@localhost7B jdk]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
jdk-base            v1.8.0_291          5b431104f188        2 days ago          1.14GB
centos-base         v1                  568629c634fc        2 weeks ago         782MB
centos              7                   eeb6ee3f44bd        13 months ago       204MB



二.在JDK基础上制作tomcat镜像
[root@localhost7B tomcat]# ls
apache-tomcat-8.5.69.tar.gz  build-command.sh  Dockerfile  

1.Dockerfile文件
[root@localhost7B tomcat]# cat Dockerfile 
#在Centos基础镜像+基础软件 + JDK 上安装 tomcat
FROM jdk-base:v1.8.0_291
MAINTAINER yuanbangchen  "123456@qq.com" 
#env 
ENV TZ "Asia/Shanghai" 
ENV LANG en_US.UTF-8 
ENV TERM xterm 
ENV TOMCAT_MAJOR_VERSION 8 
ENV TOMCAT_MINOR_VERSION 8.5.69 
ENV CATALINA_HOME /apps/tomcat 
ENV APP_DIR ${CATALINA_HOME}/webapps 
#tomcat 
RUN mkdir /apps 
ADD apache-tomcat-8.5.69.tar.gz  /apps 
RUN ln -sv /apps/apache-tomcat-8.5.69  /apps/tomcat
# 运行tomcat ,可以放在业务镜像中执行
#RUN /apps/tomcat/bin/startup.sh start


2.构造镜像
[root@localhost7B tomcat]# chmod +x  build-command.sh 
[root@localhost7B tomcat]# cat build-command.sh 
#!/bin/bash 
docker  build -t tomcat-base:8.5.69 .


三.tomcat镜像制作业务镜像1.
[root@localhost7B tomcat]# mkdir  tomcat-app1  tomcat-app2
[root@localhost7B tomcat]# tar xf apache-tomcat-8.5.69       
[root@localhost7B tomcat]# cp apache-tomcat-8.5.69/conf/server.xml  ./tomcat-app1
[root@localhost7B tomcat]# cp apache-tomcat-8.5.69/conf/server.xml  ./tomcat-app2

1.路径说明
[root@localhost7B tomcat]# cd tomcat-app1
[root@localhost7B tomcat-app1]# mkdir myapp
[root@localhost7B tomcat-app1]# ls
build-command.sh  Dockerfile  myapp  run_tomcat.sh  server.xml

2.配置文件
[root@localhost7B tomcat-app1]# cat myapp/index.html 
tomcat web page app1

3.启动脚本
[root@localhost7B tomcat-app1]# cat run_tomcat.sh 
#!/bin/bash
echo "1.1.1.1 abc.test.com" >> /etc/hosts 
echo "nameserver 223.5.5.5" >> /etc/resolv.conf 
su - www -c "/apps/tomcat/bin/catalina.sh  start"
su  - www -c "tail -f /etc/hosts"

4.Dockerifle文件
[root@localhost7B tomcat-app1]# cat Dockerfile 
#在Centos基础镜像+基础软件 + JDK + tomcat 加  项目或业务
FROM tomcat-base:8.5.69
MAINTAINER yuanbangchen  "123456@qq.com" 
#注意 :此脚本在宿主机上要用"执行"权限  chmod a+x *.sh
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh 
ADD myapp/*  /data/tomcat/webapps/myapp/ 
ADD server.xml /apps/tomcat/conf/server.xml
# www之前构造镜像时添加过
RUN chown www.www /apps/ -R 
EXPOSE 8080 8009 
# 运行tomcat 
CMD ["/apps/tomcat/bin/run_tomcat.sh"]


5.配置文件说明
[root@localhost7B tomcat-app1]# cat server.xml
      <Host name="localhost"  appBase="/data/tomcat/webapps"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>


5.构造镜像
[root@localhost7B tomcat-app1]# chmod +x build-command.sh
[root@localhost7B tomcat-app1]# chmod +x run_tomcat.sh

[root@localhost7B tomcat-app1]# cat build-command.sh 
#!/bin/bash 
docker  build -t tomcat-web:app1 .



三.tomcat镜像制作业务镜像2

1.其它设置同上
[root@localhost7B tomcat]# cd tomcat-app2 
[root@localhost7B tomcat-app2]# ls
build-command.sh  Dockerfile  myapp  run_tomcat.sh  server.xml

2.配置文件
[root@localhost7B tomcat-app2]# cat myapp/index.html 
Tomcat Page in app2

3.树德镜像
[root@localhost7B tomcat-app2]# chmod +x build-command.sh
[root@localhost7B tomcat-app2]# chmod +x run_tomcat.sh
[root@localhost7B tomcat-app2]# cat build-command.sh 
#!/bin/bash 
docker  build -t tomcat-web:app2 .

4.查看镜像
[root@localhost7B jdk]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat-base         8.5.69              d3dc7abdf6fc        2 days ago          1.16GB
jdk-base            v1.8.0_291          5b431104f188        2 days ago          1.14GB
centos              7                   eeb6ee3f44bd        13 months ago       204MB
tomcat-web          app2                7636e0ebf626        About an hour ago   1.17GB
tomcat-web          app1                0664c36f0f8d        2 hours ago         1.17GB



6.文件结构
[root@localhost7B tomcat]# tree 
.
├── apache-tomcat-8.5.69.tar.gz
├── build-command.sh
├── Dockerfile
├── tomcat-app1
│   ├── build-command.sh
│   ├── Dockerfile
│   ├── myapp
│   │   └── index.html
│   ├── run_tomcat.sh
│   └── server.xml
└── tomcat-app2
    ├── build-command.sh
    ├── Dockerfile
    ├── myapp
    │   └── index.html
    ├── run_tomcat.sh
    └── server.xml



五.在Cenots基础上制作Harpoxy镜像
[root@localhost7B web]# cd haproxy/

[root@localhost7B haproxy]# ls
build-command.sh  Dockerfile  haproxy-2.0.22.tar.gz  haproxy.cfg  run_haproxy.sh

1.配置文件
[root@localhost7B haproxy]# cat haproxy.cfg 
global 
chroot /usr/local/haproxy 
#stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin 
uid 99 
gid 99 
daemon 
nbproc 1 
pidfile /usr/local/haproxy/run/haproxy.pid 
log 127.0.0.1 local3 info 

defaults 
option http-keep-alive 
option forwardfor 
mode http
timeout connect 300000ms 
timeout client 300000ms 
timeout server 300000ms 

listen stats 
  mode http 
  bind 0.0.0.0:9999 
  stats enable 
  log global 
  stats uri  /haproxy-status 
  stats auth haadmin:123456 

listen web_port 
  bind 0.0.0.0:80 
  mode http 
  log global 
  balance roundrobin 
  server web1 192.168.80.110:8080 check inter 3000 fall 2 rise 5 
  server web2 192.168.80.110:8081 check inter 3000 fall 2 rise 5
  
 2.启动脚本 
[root@localhost7B haproxy]# cat run_haproxy.sh 
#!/bin/bash
/usr/local/haproxy/sbin/haproxy  -f   /etc/haproxy/haproxy.cfg
tail -f /etc/hosts 

 
3.Dcokerfile文件
[root@localhost7B haproxy]# cat Dockerfile 
#在Centos基础镜像+基础软件上安装haproxy
FROM centos-base:v1 
MAINTAINER yuanbangchen  "123456@qq.com" 
RUN yum install -y yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl  openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate 
ADD haproxy-2.0.22.tar.gz  /usr/local/src/ 
RUN cd /usr/local/src/haproxy-2.0.22  && make ARCH=x86_64 TARGET=linuxglibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1  USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy 
RUN  cp /usr/local/haproxy/sbin/haproxy /usr/sbin/  &&  mkdir /usr/local/haproxy/run
#/usr/local/haproxy/sbin/haproxy /usr/sbin/
ADD haproxy.cfg /etc/haproxy/ 
ADD run_haproxy.sh /usr/bin/
EXPOSE 80 9999 
CMD ["/usr/bin/run_haproxy.sh"]
#设置时区:
RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime


4.树德镜像
[root@localhost7B haproxy]# chmod +x build-command.sh
[root@localhost7B haproxy]# chmod +x run_haproxy.sh
[root@localhost7B haproxy]# cat build-command.sh 
#!/bin/bash 
docker  build -t  centos-harpoxy .

5.目录结构
[root@localhost7B web]# tree haproxy/
haproxy/
├── build-command.sh
├── Dockerfile
├── haproxy-2.0.22.tar.gz
├── haproxy.cfg
└── run_haproxy.sh

 
6.查看镜像
[root@localhost7B jdk]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos-harpoxy      latest              453f769d8e92        About an hour ago   1.11GB
tomcat-base         8.5.69              d3dc7abdf6fc        2 days ago          1.16GB
jdk-base            v1.8.0_291          5b431104f188        2 days ago          1.14GB
centos              7                   eeb6ee3f44bd        13 months ago       204MB
tomcat-web          app2                7636e0ebf626        About an hour ago   1.17GB
tomcat-web          app1                0664c36f0f8d        2 hours ago         1.17GB
 
 
 
六.启动容器测试 
docker run  -it  -d  -p 8080:8080 --name tomcatA tomcat-web:app1 
docker run  -it  -d  -p 8081:8080 --name tomcatB tomcat-web:app2
docker run  -it  -d -p 80:80  -p 9999:9999  --name haproxyA   centos-harpoxy:latest 

[root@localhost7B web]# docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                        NAMES
fb8b9df8e69f        centos-harpoxy:latest   "/usr/bin/run_haprox…"   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp, 0.0.0.0:9999->9999/tcp   haproxyA
bfff0f937081        tomcat-web:app2         "/apps/tomcat/bin/ru…"   2 hours ago         Up 2 hours          8009/tcp, 0.0.0.0:8081->8080/tcp             tomcatB
62fbb6275ff1        tomcat-web:app1         "/apps/tomcat/bin/ru…"   3 hours ago         Up About an hour    8009/tcp, 0.0.0.0:8080->8080/tcp             tomcatA


#使用docker stop tomcatA 测试

[root@localhost7A ~]# curl 192.168.80.110:80/myapp/
tomcat web page app1
[root@localhost7A ~]# curl 192.168.80.110:80/myapp/
Tomcat Page in app2

 


 



 



posted @ 2022-10-28 16:10  yuanbangchen  阅读(108)  评论(0编辑  收藏  举报