Skip to content

Commit

Permalink
初版 特权模式可选 可独立设置防火墙
Browse files Browse the repository at this point in the history
  • Loading branch information
eyunzhu committed Nov 7, 2024
0 parents commit 196c18d
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 排除项目中的所有文件
*
# 仅保留 scripts 目录下的文件
!scripts/
!scripts/*
99 changes: 99 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# 宝塔面板Docker镜像

基于Debian12构建的宝塔面板镜像,为dockerfile使用宝塔官方脚本自动构建,无人工干预,安全有保障,dockerfile公开可自定义构建

面板版本随官方安装脚本更新

可使用host网络模式部署,也可使用macvlan网络模式部署作为独立主机(在特权模式下可设置单独的防火墙)


## 镜像简介


1. `baota:minimal`

仅安装了最新版宝塔面板

2. `baota:lnmp`

基于`baota:minimal`,安装了完整的LNMP环境(nginx1.24,mysql5.7,php7.4/8.2,phpmyadmin5.1,redis7.2)

## 镜像使用

1. 镜像运行命令

```bash
# 普通模式
docker run -d eyunzhu/baota:lnmp

# 特权模式 可以单独使用防火墙
docker run -d --privileged --entrypoint="/bin/bash" eyunzhu/baota:lnmp -c "/usr/local/bin/boot.sh & exec /lib/systemd/systemd"
```
2. 面板基本信息
1. 面板管理地址:http://您的ip地址:8888/btpanel
2. 默认用户:username
3. 默认密码:password
3. 目录 (宝塔默认未更改)
1. 容器里面的网站数据目录:`/www/wwwroot`
2. MySQL数据目录:`/www/server/data`
3. vhost文件路径:`/www/server/panel/vhost`
4. 常用部署命令记录
```bash
# 普通模式
docker run -d --name='bt_1' --net macvlan-net --ip 192.168.1.211 eyunzhu/baota:lnmp

# 特权模式 使用macvlan
docker run -d --privileged --name='bt_2' --net macvlan-net --ip 192.168.1.201 --entrypoint="/bin/bash" eyunzhu/baota:lnmp -c "/usr/local/bin/boot.sh & exec /lib/systemd/systemd"
```


## 自主构建镜像方法
可自行修改dockerfile文件,自定义构建镜像

1. 项目目录结构

```plaintext
.
├── dockerfiles
│ ├── dockerfile.baota # Baota 面板基础镜像 dockerfile
│ └── dockerfile.baota-lnmp # Baota 面板 LNMP 镜像 dockerfile
├── references # 参考目录
├── scripts
│ ├── boot.sh # 自定义初始化脚本
│ └── iptables-rules.sh # IPTables 规则脚本
├── .dockerignore # Docker 镜像忽略文件
└── README.md
```
2. 构建命令
在项目根目录下执行构建

1. `baota:minimal`
```bash
docker build -t eyunzhu/baota:minimal -f ./dockerfiles/dockerfile.baota .

# 或者后台运行构建
nohup docker build --network macvlan-net -t eyunzhu/baota:minimal -f ./dockerfiles/dockerfile.baota . > 1.log 2>&1 &
```
2. `baota:lnmp`
```bash
docker build -t eyunzhu/baota:lnmp -f ./dockerfiles/dockerfile.baota-lnmp .
# 或者后台运行构建
nohup docker build --network macvlan-net -t eyunzhu/baota:lnmp -f ./dockerfiles/dockerfile.baota-lnmp . > 2.log 2>&1 &
```

## 问题注意
1. 防火墙
- 一般部署不需要注意防火墙,若使用macvlan网络模式部署容器作为独立主机使用需要注意
- 此镜像防火墙仅安装了iptables

修改防火墙请在容器`/usr/local/bin/iptables-rules.sh`脚本中

修改完成后运行一次`/usr/local/bin/iptables-rules.sh`脚本即可

不要在面板管理规则,且面板中端口只显示ipv4的规则(面板调用的是ufw,镜像未安装,测试ufw在容器中有问题)





56 changes: 56 additions & 0 deletions dockerfiles/dockerfile.baota
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
FROM debian:12
LABEL maintainer="eyunzhu <[email protected]>"

ENV DEBIAN_FRONTEND=noninteractive

COPY scripts/boot.sh /usr/local/bin/boot.sh
COPY scripts/iptables-rules.sh /usr/local/bin/iptables-rules.sh

RUN sed -i 's|deb.debian.org|mirrors.aliyun.com|g' /etc/apt/sources.list.d/debian.sources && \
apt-get update && \
apt-get install -y --no-install-recommends \
sudo \
# curl \
wget \
vim \
iproute2 \
iputils-ping \
procps \
# htop \
# lsof \
# dnsutils \
iptables \
cron \
locales \
systemd \
dbus \
net-tools \
openssh-server \
libc-ares2 \
libonig5 \
&& wget -4 --no-check-certificate -O install.sh https://download.bt.cn/install/install_lts.sh \
# 取消宝塔安装检查
&& sed -i '/Ready_Check$/ s/^/#/' install.sh \
# 取消宝塔防火墙设置
&& sed -i '/Set_Firewall$/ s/^/#/' install.sh \
&& bash install.sh -P 8888 --safe-path "btpanel" --ssl-disable -y \
&& echo username|bt 6 && echo password|bt 5 \
# 开启ipv6访问面板
&& echo '::' > /www/server/panel/data/ipv6.pl \
# 将防火墙设置为 iptables-legacy
&& update-alternatives --set iptables /usr/sbin/iptables-legacy \
&& update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy \
&& sed -i 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \
&& locale-gen \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
# 设置boot.sh脚本权限
&& chmod +x /usr/local/bin/boot.sh \
# 清理
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /www/reserve_space.pl \
&& rm -rf /var/log/*

STOPSIGNAL SIGRTMIN+3
ENTRYPOINT ["/usr/local/bin/boot.sh"]
30 changes: 30 additions & 0 deletions dockerfiles/dockerfile.baota-lnmp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM eyunzhu/baota:minimal
LABEL maintainer="eyunzhu <[email protected]>"

ENV DEBIAN_FRONTEND=noninteractive

# 直接极速安装php成功但会启动失败,需要提前安装库:libc-ares2 libonig5 已在minimal提前安装过此库
# 编译安装PHP需要提前安装cmake
# nginx快速安装会判断inodes空间,而我在nas的docker中inodes检测不正常,所以需要这里使用编译安装

RUN cd /www/server/panel/install \
&& bash install_soft.sh 4 install php 7.4 \
&& bash install_soft.sh 4 install php 8.2 \
&& bash install_soft.sh 4 install redis 7.2 \
&& bash install_soft.sh 0 install nginx 1.24 \
&& bash install_soft.sh 4 install mysql 5.7 \
&& bash install_soft.sh 4 install phpmyadmin 5.1 \
# 备份并清空数据库data目录,一方面可以减少镜像大小,另一方面可以解决挂载目录时data被宿主机覆盖的问题
&& /etc/init.d/mysqld stop \
&& cd /www/server \
&& tar czf /www/server/data_backup.tar.gz -C /www/server data \
&& rm -rf /www/server/data/* \
# 清理
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /www/reserve_space.pl \
&& rm -rf /var/log/* \
&& rm -rf /tmp/*

STOPSIGNAL SIGRTMIN+3
CMD ["/lib/systemd/systemd"]
75 changes: 75 additions & 0 deletions references/bt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

init_path=/etc/init.d
Root_Path=`cat /var/bt_setupPath.conf`
Setup_Path=$Root_Path/server/mysql
Data_Path=$Root_Path/server/data
O_pl=$(cat /www/server/panel/data/o.pl)

soft_start(){
# 扫描并启动所有服务
init_scripts=$(ls ${init_path})
for script in ${init_scripts}; do
case "${script}" in
"bt"|"mysqld"|"nginx"|"httpd")
continue
;;
esac

${init_path}/${script} start
done

if [ -f ${init_path}/nginx ]; then
${init_path}/nginx start
elif [ -f ${init_path}/httpd ]; then
${init_path}/httpd start
fi

${init_path}/bt stop
${init_path}/bt start

pkill crond
/sbin/crond

chmod 600 /etc/ssh/ssh_host_*
/usr/sbin/sshd -D &
}

init_mysql(){
if [ "${O_pl}" != "dk_lamp_lts_c7" ] && [ "${O_pl}" != "dk_lnmp_lts_c7" ];then
return
fi
if [ -d "${Data_Path}" ]; then
check_z=$(ls "${Data_Path}")
echo "check_z:"
echo ${check_z}
if [[ ! -z "${check_z}" ]]; then
echo "check_z is not empty"
return
fi
fi
if [ -f /init_mysql.sh ] && [ -d "${Setup_Path}" ];then
sh /init_mysql.sh
rm -f /init_mysql.sh
fi
}

is_empty_Data(){
return "$(ls -A ${Data_Path}/|wc -w)"
}

start_mysql(){
if [ -d "${Setup_Path}" ] && [ -f "${init_path}/mysqld" ];then
chown -R mysql:mysql ${Data_Path}
chgrp -R mysql ${Setup_Path}/.
${init_path}/mysqld start
fi
}

is_empty_Data > /dev/null
init_mysql > /dev/null
start_mysql > /dev/null
soft_start > /dev/null
#tail -f /dev/null
41 changes: 41 additions & 0 deletions references/init_mysql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

Root_Path=`cat /var/bt_setupPath.conf`
Setup_Path=$Root_Path/server/mysql
Data_Path=$Root_Path/server/data

Mysql_Initialize(){
if [ -d "${Data_Path}" ]; then
check_z=$(ls "${Data_Path}")
if [[ ! -z "${check_z}" ]]; then
return
fi
fi

mkdir -p ${Data_Path}
chown -R mysql:mysql ${Data_Path}
chgrp -R mysql ${Setup_Path}/.

${Setup_Path}/bin/mysqld --initialize-insecure --basedir=${Setup_Path} --datadir=${Data_Path} --user=mysql

cat > /etc/ld.so.conf.d/mysql.conf<<EOF
${Setup_Path}/lib
EOF
ldconfig
ln -sf ${Setup_Path}/lib/mysql /usr/lib/mysql
ln -sf ${Setup_Path}/include/mysql /usr/include/mysql
/etc/init.d/mysqld start

mysqlpwd=`cat /dev/urandom | head -n 16 | md5sum | head -c 16`
${Setup_Path}/bin/mysqladmin -u root password "${mysqlpwd}"

cd "${Setup_Path}"
rm -f src.tar.gz
rm -rf src
/etc/init.d/mysqld start
rm -rf /init_mysql.sh
}

Mysql_Initialize
34 changes: 34 additions & 0 deletions scripts/boot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

# 当前脚本会在容器启动时执行
# 脚本位置:/usr/local/bin/boot.sh


# 初始化:还原mysql数据目录 此处为解决安装了数据库的镜像在构建时备份并清空了数据目录,一方面可以减少镜像大小,另一方面可以解决挂载目录时data被宿主机覆盖的问题
if [ -d /www/server/data ] && [ ! "$(ls -A /www/server/data)" ] && [ -f /www/server/data_backup.tar.gz ]; then
# 数据库目录为空 并且/www/server/data_backup.tar.gz存在
# 还原数据
echo "初始化:还原mysql数据目录"
tar xzf /www/server/data_backup.tar.gz -C /www/server
rm -rf /www/server/data_backup.tar.gz
/etc/init.d/mysqld start
fi


# 启动面板
/usr/bin/bt stop
/usr/bin/bt start


# 运行防火墙规则
# 检查 /lib/systemd/systemd 是否在运行
if ps -p 1 | grep -q "systemd"; then
echo "运行systemd"
chmod +x /usr/local/bin/iptables-rules.sh
/usr/local/bin/iptables-rules.sh
else
echo "未运行systemd"
fi


tail -f /dev/null
Loading

0 comments on commit 196c18d

Please sign in to comment.