docker基于alpine制作haproxy镜像

docker基于alpine制作haproxy镜像

1.目录结构

[root@localhost ~]# tree haproxy2
haproxy2
├── Dockerfile
└── files
    ├── haproxy-2.4.0.tar.gz
    ├── install.sh
    ├── run_haproxy.sh
    └── sysctl.conf

1 directory, 5 files
[root@localhost ~]# 

2.查看结构内容

[root@localhost ~]# cd haproxy2
[root@localhost haproxy2]# 
[root@localhost haproxy2]# ls
Dockerfile  files
[root@localhost haproxy2]# cat Dockerfile 
FROM alpine

LABEL MAINTAINER "Lfei 1314@163.com"

ENV version 2.4.0

ADD files/haproxy-${version}.tar.gz /tmp/

ADD files/install.sh /tmp/

ADD files/run_haproxy.sh /tmp/

ADD files/sysctl.conf /tmp/

RUN /tmp/install.sh

ENTRYPOINT /tmp/run_haproxy.sh
[root@localhost haproxy2]# 

3.查看files目录里得文件内容

[root@localhost files]# cat 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 /tmp/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 /tmp/haproxy-${version}/* /tmp/install.sh
[root@localhost files]# 
[root@localhost files]# cat run_haproxy.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

by=1
for RS in $(cat /opt/RS.text);do
cat >> /etc/haproxy/haproxy.cfg << EOF
    server web$by $RS:80 check inter 2000 fall 5
EOF
let by++

done

/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg -db


[root@localhost files]# 
[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
[root@localhost files]# 

4.查看变量

[root@localhost files]# cat  /opt/RS.text 
172.17.0.3
172.17.0.4
[root@localhost files]# 

5.制作镜像

[root@localhost haproxy2]# docker build -t test:v0.1
[root@localhost haproxy2]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
test                 v0.1      7ed0f6f258d0   6 seconds ago   81MB
3199560936/haprxoy   v0.2      aed3938de260   3 hours ago     432MB
3199560936/httpd     v0.4      37f8ea813545   4 days ago      702MB
3199560936/httpd     v0.2      026478daf0c7   5 days ago      702MB
3199560936/nginx     v0.1      c98f0d7db627   9 days ago      579MB
busybox              latest    d23834f29b38   12 days ago     1.24MB
alpine               latest    c059bfaa849c   2 weeks ago     5.59MB
centos               latest    5d0da3dc9764   2 months ago    231MB

6.利用镜像启动容器并映射端口和目录

[root@localhost haproxy2]# docker run -d --name by -p 1314:80 -v /opt:/opt 7ed0f6f258d0
8e0adebbe5b21e185f833a221bc5fc761a38383626dc4575dc91639e4c48815c
[root@localhost haproxy2]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                   NAMES
8e0adebbe5b2   7ed0f6f258d0   "/bin/sh -c /tmp/run…"   5 seconds ago   Up 3 seconds   0.0.0.0:1314->80/tcp, :::1314->80/tcp   by
[root@localhost haproxy2]#

7.在已有的镜像中启动httpd和nginx两个容器

[root@localhost haproxy2]# docker run -itd --name httpd 37f8ea813545
7173ab00bb36863bf26f3e32002022d9abf662815b25d16b1f10ec39664d0942
[root@localhost haproxy2]# docker run -itd --name nginx c98f0d7db627
2cdcb7813f1dbf0dbe07796d8f08d3323831f8249666fc042e72f2da0215f0e2
[root@localhost haproxy2]# 
[root@localhost haproxy2]# docker ps -a 
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS                     PORTS                                   NAMES
2cdcb7813f1d   c98f0d7db627   "bin/bash start.sh"      4 seconds ago        Exited (0) 3 seconds ago                                           nginx
7173ab00bb36   37f8ea813545   "/usr/local/apache/b…"   19 seconds ago       Up 18 seconds              80/tcp, 443/tcp                         httpd
8e0adebbe5b2   7ed0f6f258d0   "/bin/sh -c /tmp/run…"   About a minute ago   Up About a minute          0.0.0.0:1314->80/tcp, :::1314->80/tcp   by
[root@localhost haproxy2]# 

8.利用宿主机的ip进行访问

注意: 我们的映射端口是1314
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. 安装Docker 在CentOS上安装Docker,可以使用官方的安装脚本。打开终端并运行以下命令: ``` $ curl -fsSL https://get.docker.com/ | sh ``` 如果您的系统上没有curl,请先安装它: ``` $ yum install curl ``` 2. 下载PostgreSQL的Dockerfile 我们可以从官方的Docker Hub上下载PostgreSQL的Dockerfile。使用以下命令: ``` $ curl -O https://raw.githubusercontent.com/docker-library/postgres/master/13/alpine/Dockerfile ``` 3. 编辑Dockerfile 使用vim或nano等编辑器打开下载下来的Dockerfile文件,进行以下配置: ``` FROM centos:latest ENV POSTGRES_USER postgres ENV POSTGRES_PASSWORD postgres ENV POSTGRES_DB postgres RUN yum update -y && \ yum install -y postgresql-server postgresql-contrib && \ yum clean all USER postgres RUN initdb --encoding=UTF8 --locale=C -D /var/lib/pgsql/data && \ pg_ctl -D /var/lib/pgsql/data -l logfile start && \ psql --command "CREATE USER postgres WITH SUPERUSER PASSWORD 'postgres';" && \ createdb -O postgres postgres VOLUME ["/var/lib/pgsql/data"] EXPOSE 5432 CMD ["postgres", "-D", "/var/lib/pgsql/data", "-c", "config_file=/var/lib/pgsql/data/postgresql.conf"] ``` 4. 构建Docker镜像 使用以下命令构建Docker镜像: ``` $ docker build -t my_postgresql . ``` 这将构建一个名为“my_postgresql”的新Docker镜像。 5. 运行PostgreSQL容器 使用以下命令运行PostgreSQL容器: ``` $ docker run -d -p 5432:5432 --name my_postgresql_container my_postgresql ``` 这将创建一个名为“my_postgresql_container”的新容器,并将容器的端口5432映射到主机的端口5432。 6. 测试PostgreSQL容器 为了测试新的PostgreSQL容器,请使用以下命令: ``` $ psql -h localhost -U postgres -d postgres ``` 您应该现在可以通过psql连接到PostgreSQL容器。 现在您已经成功地使用Docker创建了一个基于CentOS的PostgreSQL镜像,并运行了一个新的PostgreSQL容器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lfei5120

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值