环境准备
Dockerfile所在的目录不能包含其它不要的文件 否则会连不要的文件一起打包的docker镜像中
FROM 192.168.30.113/laboratory/c7-systemd-ssh-3322:7.8.2003
RUN yum -y install net-tools libaio-devel.x86_64 numactl ntpdate openssl tar zip;yum clean all
RUN ntpdate time1.aliyun.com
WORKDIR /opt
ADD taishi-deploy-docker.tar.gz .
WORKDIR /opt/taishi-deploy-docker
RUN sh ./setfile.sh script
WORKDIR /opt/taishi-deploy-docker/script
CMD ./setup.sh
Dockerfile
sudo docker build -t 192.168.20.113/taishiganzhi/siem .
sudo docker push 192.168.20.113/taishiganzhi/siem
build.sh
1.把目录压缩成包
tar -zcvf taishi-deploy-docker.tar.gz ./taishi-deploy-docker/
2.把压缩包打成镜像并推送到仓库
sh build.sh
容器启动问题
在dockerfile中指定了CMD CMD执行的是自己的install脚本 install.sh在执行完成后容器默认就会自动停止掉
WORKDIR /opt/taishi-deploy-docker/script
CMD ./install.sh
造成的现象就是容器会被不断的重启
docker和supervisor一样都只能管理前台进程
要解决这个问题就必须要使install.sh一直处于执行状态并且日志一直要输出到前台终端中
容器处于running状态并不代表已经执行完CMD脚本 running的时候可以查看安装日志
这些标准输出都是可以通过docker logs来查看的
容器启动方案
1.接收启动环境变量
function UpdateIp()
{
sed -i 's/IP=__ip__/IP='$host_ip'/' ../../conf/install_config.ini
}
2.启动容器时候传递环境变量
3.获取带标点符号的环境变量
4.把前台进程变成后台进程
#执行总入口函数
function Install()
{
echo -e "\033[32m=========================================================================\033[0m"
echo -e "\033[32m\t Start to install 态势感知项目 \033[0m"
os_version=`cat /etc/redhat-release | grep -oE '[0-9]+\.[0-9\.]+'`
os_major_version=${os_version:0:1}
MemTotal=`free -m | grep Mem | awk '{print $2}'`
let MemTotal_G=$MemTotal/1024
echo -e "System: CentOS $os_version"
echo -e "Memory is: $MemTotal_G GB "
Check_OS_Version
Check_Python_Version
Handler_Config
#如果以root用户启动并且当前机器安装es,则声明es用户,用于安装es
if [[ ${USER} == "root" ]] && [[ ${MODULES} =~ .*elasticsearch.* ]]; then
export ES_USER="elasticsearch"
fi
Check_user
if [[ ${MODULES} =~ .*system_tools_config.* ]];then
Install_System_Tools_Config
fi
Install_Module
echo "Success taish" > ${INSTALL_DIR}/result
echo -e "\033[32m\t Congratulation, Installation completed!!!!! \033[0m"
echo -e "\033[32m=========================================================================\033[0m"
}
install.sh
4.在增加一个前台进程
#!/bin/bash
source ./utils.sh
__ReadINI ../conf/install_config.ini
nohup ./install.sh 1>./install.log 2>&1 & echo $! > command.pid
resFile=${INSTALL_DIR}/result
echo $resFile
while ((i<=20))
do
if [ ! -f "$resFile" ]; then
echo "starting........."
else
echo "start up siem"
ps -ef | grep elastic | awk '{print $2}' | xargs kill -9
servicectl start taishi
tail -f ./install.log
break
fi
sleep 30
((i++))
done
setup.sh
此方案适合容器自启动 但是部署在虚拟主机上的时候却不能正常
虚拟机服务自启动方案
由于虚拟机运行效率比较快 需要在执行的的时候设置一段时间的睡眠
#!/bin/bash
source ./utils.sh
__ReadINI ../conf/install_config.ini
nohup ./install.sh 1>./install.log 2>&1 & echo $! > command.pid
resFile=${INSTALL_DIR}/result
echo $resFile
while ((i<=20))
do
if [ ! -f "$resFile" ]; then
echo "starting........."
else
echo "start up siem"
ps -ef | grep elastic | awk '{print $2}' | xargs kill -9
servicectl start taishi
tail -f ./install.log
break
fi
sleep 30
((i++))
done
原脚本
#!/bin/bash
source ./utils.sh
__ReadINI ../conf/install_config.ini
nohup ./install.sh 1>./install.log 2>&1 & echo $! > command.pid
resFile=${INSTALL_DIR}/result
echo $resFile
while ((i<=20))
do
if [ ! -f "$resFile" ]; then
echo "starting........."
else
kill -9 `cat command.pid`
sleep 60
echo "start up siem"
ps -ef | grep elastic | awk '{print $2}' | xargs kill -9
sleep 60
systemctl start taishi
sleep 30
break
fi
sleep 30
((i++))
done
新脚本
成功登录
在同一个进程中kill其它进程的时候需要sleep一段时间 否则会由于执行速度太快导致kill的时机太超前造成一些异常问题
Docker镜像中安装python3
1.直接在Dockerfile中使用源码编译安装的话会需要先安装gcc等一系列依赖
2.使用在主机上已经安装好的
3.在Dockerfile中直接解压安装
jenkins容器中使用docker命令