Docker容器Tomcat无catalina.out日志文件
docker容器 tomcat输出日志到catalina.out
·
一、背景
Docker启动Tomcat默认是CMD ["catalina.sh", "run"]
,日志将会托管给Docker容器,没有生成日志文件,只能使用命令docker logs -f <CONTAINER_ID>
查看。
在Rancher平台部署服务,我们只能看到一部分日志,即使执行命令行进入tomcat的logs目录,也不到想要的日志,只能进入服务器查看容器日志。
只有使用startup.sh启动才会输出catalina.out。但是Docker如果以startup.sh启动,启动后会立即退出,这时Docker需要一个后台执行程序才会保持容器不退出。
二、开始制作Tomcat镜像
2.1 新建run.sh
文件
使用startup.sh
启动并一直tail -f catalina.out
就不会退出容器,run.sh
文件内容如下:
#! /bin/bash
bash /usr/local/tomcat/bin/startup.sh
tail -f /usr/local/tomcat/logs/catalina.out
2.2 新建Dockerfile
文件
FROM lucksoft/tomcat:9-jdk8
ENV TZ Asia/Shanghai
WORKDIR /usr/local/tomcat
RUN apt-get install -y wget
RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak
RUN echo "deb http://mirrors.aliyun.com/ubuntu/ xenial main >> /etc/apt/sources.list \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ xenial main >> /etc/apt/sources.list \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ vivid-security main restricted universe multiverse >> /etc/apt/sources.list \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ vivid-updates main restricted universe multiverse >> /etc/apt/sources.list \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ vivid-proposed main restricted universe multiverse >> /etc/apt/sources.list \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ vivid-backports main restricted universe multiverse12345678910 >> /etc/apt/sources.list \
#RUN apt-get update && apt-get clean && rm -rf /var/lib/apt/lists/*
RUN apt-get update && \
apt-get install -y --no-install-recommends \
cron && \
rm -rf /var/lib/apt/lists/* && \
apt-get clean
COPY cut_log.sh /usr/local/tomcat/bin/
# 复制文件到容器内部的指定目录
COPY run.sh /usr/local/tomcat/bin/
# 赋予权限
RUN chmod 755 /usr/local/tomcat/bin/run.sh
RUN chmod 755 /usr/local/tomcat/bin/cut_log.sh
# 运行一次,设置定时任务
RUN echo "59 23 * * * /usr/local/tomcat/bin/cut_log.sh" | crontab -
# run.sh启动
CMD ["/usr/local/tomcat/bin/run.sh"]
2.3 准备cut_log.sh
文件
将日志文件进行归档整理。
#!/bin/bash
cd `dirname $0`
DATE=`date +%Y-%m-%d`
DATE30=`date -d '30 days ago' +%Y-%m-%d`
cd /usr/local/tomcat/logs
ls others >/dev/null 2>&1
if [ $? != 0 ];then
mkdir others
fi
# 前一天的日志移动到归档目录
find . -type f -mtime +1 -exec mv {} /usr/local/tomcat/logs/others/ \;
# 删除30天前的日志
find /usr/local/tomcat/logs/others -type f -mtime +30 -exec rm -f {} \;
cp /usr/local/tomcat/logs/catalina.out /usr/local/tomcat/logs/catalina.out.${DATE}
#清空
truncate -s 0 /usr/local/tomcat/logs/catalina.out
将上面的文件都放到同一个目录下,然后使用docker builder
命令构建镜像即可。
更多推荐
已为社区贡献1条内容
所有评论(0)