如果需要一个定时任务的模板系统,可以使用如下的Dockerfile构建镜像
对大小有一些要求,可以试试基于 Alpine 构建的镜像 docker-alpine-cron
# Backup Server
# Version 1.0
# Explain: crond
# Author: dongdong
# Date: 2017-12-29
FROM centos
USER root
# 使用阿里源替换默认源
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum clean all && yum makecache
# 安装 cronie
RUN yum -y install cronie && yum clean all && rm -rf /var/cache/yum
# 调整时区
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 按照正常方式启动crond时,会报错:docker cron FAILED to open PAM security session
RUN sed -i -e '/pam_loginuid.so/s/^/#/' /etc/pam.d/crond
# 添加定时任务
RUN chmod 0644 /etc/crontab
RUN echo "*/1 * * * * root /bin/echo 'it works' >> /root/test.log" >> /etc/crontab
CMD crond && tail -f /dev/null
在定时任务镜像的基础上,扩充功能,实现使用数据库客户端备份数据的目的
PS:需要先有一个正常工作的备份数据库shell脚本
# Backup Server
# Version 1.0
# Explain: oracle & mysql backup with crond
# Author: dongdong
# Date: 2017-12-29
FROM centos
USER root
# 使用阿里源替换默认源
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum clean all && yum makecache
# 安装一些常用软件
#RUN yum -y install epel-release && yum makecache
RUN yum -y install vim unzip cronie expect openssh-clients libaio && yum clean all && rm -rf /var/cache/yum
# 调整时区
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 添加本地目录的文件到镜像中
ADD sh /home/sh
ADD downloads /home/downloads
ADD conf /home/conf
RUN mkdir /home/log && mkdir /home/data
# 安装 MySQL
RUN ["rpm", "-ivh", "/home/downloads/mysql-community-common-5.7.19-1.el7.x86_64.rpm"]
RUN ["rpm", "-ivh", "/home/downloads/mysql-community-libs-5.7.19-1.el7.x86_64.rpm"]
RUN ["rpm", "-ivh", "/home/downloads/mysql-community-client-5.7.19-1.el7.x86_64.rpm"]
# 安装 Oracle 客户端
RUN unzip /home/downloads/oracle-instantclient11.2-basic-11.2.0.1.0-1.x86_64.zip -d /home/oracle
RUN unzip /home/downloads/oracle-instantclient11.2-jdbc-11.2.0.1.0-1.x86_64.zip -d /home/oracle
RUN unzip /home/downloads/oracle-instantclient11.2-sdk-11.2.0.1.0-1.x86_64.zip -d /home/oracle
RUN unzip /home/downloads/oracle-instantclient11.2-sqlplus-11.2.0.1.0-1.x86_64.zip -d /home/oracle
RUN cp -R /home/downloads/instantclient_11_2_exp_imp/* /home/oracle/instantclient_11_2
RUN cp /home/conf/tnsnames.ora /home/oracle/instantclient_11_2
RUN chmod +x /home/oracle/instantclient_11_2/imp /home/oracle/instantclient_11_2/exp
# 安装完成客户端后删除安装包
RUN rm -rf /home/downloads
# 设置 Oracle 的环境变量
RUN echo $'export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK\n\
export ORACLE_IC_HOME=/home/oracle/instantclient_11_2\n\
export ORACLE_HOME=$ORACLE_IC_HOME\n\
export TNS_ADMIN=$ORACLE_IC_HOME\n\
export PATH=$PATH:$HOME/bin:$ORACLE_IC_HOME\n\
export LD_LIBRARY_PATH=$ORACLE_IC_HOME:/usr/lib\n\
export ORACLE_SID=ZFGFH' >> /root/.bash_profile
#RUN ["/bin/bash", "-c", "source /root/.bash_profile"]
# comment out pam
RUN sed -i -e '/pam_loginuid.so/s/^/#/' /etc/pam.d/crond
RUN chmod 0644 /etc/crontab
# 添加定时任务
RUN echo "*/1 * * * * root /bin/echo 'it works' >> /root/test.log" >> /etc/crontab
RUN echo "30 22 * * * root source /root/.bash_profile; /bin/expect /home/sh/backup_zfgfh.sh >> /home/log/zfgfh.log" >> /etc/crontab
CMD crond && tail -f /dev/null