Docker容器内应用服务自启动的方法示例容器内应用服务自启动的方法示例
主要介绍了Docker容器内应用服务自启动的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参
考。一起跟随小编过来看看吧
如果想把Docker容器内的应用服务随着容器开启时自启动。只需要将服务启动的脚本写在Dockerfile里,然后用Dockerfile重
构镜像即可实现:
编写应用服务自启动脚本
编写Dockerfile
重构镜像
开启容器
编写服务自启动脚本(编写服务自启动脚本(dockerd))
(tomcat为例)
#!/bin/bash
#
#
#
# chkconfig: 345 98 30
# description: tomcat program.
# processname: tomcat
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
#. /etc/sysconfig/network
prog="tomcat"
checkprocess()
{
chkret=`ps -ef |grep $prog |grep -v "grep" |wc -l`
return $chkret
}
start()
{
cd /usr/local/tomcat/bin/
./startup.sh
echo "Service $prog started!"
}
#/usr/sbin/nscd &
/usr/sbin/sshd -D &
while [ true ]
do
DNSIP=172.18.3.179
ping=`ping -c 3 $DNSIP|awk 'NR==7 {print $4}'`
if [[ $ping -eq "3" ]]
then
break
fi
sleep 3
done
while [ true ]
do
checkprocess
if [[ $chkret -eq "0" ]]
then
start
#nscd -i hosts
else
echo "Service $prog is running."
fi
sleep 60
done
exit 0
编写Dockerfile
FROM tomcat
MAINTAINER liu "350142639@qq.com"
评论0