-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerfile
69 lines (55 loc) · 2.02 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
FROM python:3.9-slim
WORKDIR /app
# 安装系统依赖
RUN apt-get update && apt-get install -y \
cron \
logrotate \
&& rm -rf /var/lib/apt/lists/*
# 安装 Python 依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt && \
pip install --no-cache-dir supervisor
# 复制应用代码
COPY . .
# 创建数据目录
RUN mkdir -p /app/data && \
chmod 777 /app/data
# 创建日志目录
RUN mkdir -p /app/logs && \
chmod 777 /app/logs
RUN mkdir -p /etc/supervisor/conf.d
# 添加清理脚本
COPY clean_empty_notes.py /app/
RUN chmod +x /app/clean_empty_notes.py
# 设置 crontab
RUN echo "0 3 * * * cd /app && python /app/clean_empty_notes.py >> /app/logs/cron.log 2>&1" > /etc/cron.d/clean-notes && \
chmod 0644 /etc/cron.d/clean-notes && \
crontab /etc/cron.d/clean-notes
# 配置日志轮转
RUN echo "/app/logs/*.log {\n\
daily\n\
rotate 7\n\
compress\n\
delaycompress\n\
missingok\n\
notifempty\n\
create 0644 root root\n\
sharedscripts\n\
postrotate\n\
supervisorctl signal HUP all >/dev/null 2>&1 || true\n\
endscript\n\
}" > /etc/logrotate.d/app-logs && \
chmod 0644 /etc/logrotate.d/app-logs && \
echo "0 0 * * * /usr/sbin/logrotate /etc/logrotate.d/app-logs --state /app/logrotate-state" >> /etc/cron.d/clean-notes
# 创建 supervisor 配置
RUN echo "[supervisord]\nnodaemon=true\n\n\
[program:flask]\ncommand=python app.py\ndirectory=/app\nautostart=true\nautorestart=true\nstdout_logfile=/app/logs/flask.log\nstderr_logfile=/app/logs/flask_error.log\n\n\
[program:cron]\ncommand=cron -f\nautostart=true\nautorestart=true\nstdout_logfile=/app/logs/cron.log\nstderr_logfile=/app/logs/cron_error.log" > /etc/supervisor/conf.d/supervisord.conf
# 设置环境变量
ENV SECRET_KEY="change_this_in_production"
ENV ENCRYPTION_KEY="change_this_in_production"
ENV ENCRYPTION_SALT="change_this_in_production"
# 暴露端口
EXPOSE 5005
# 启动 supervisor
CMD ["/usr/local/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]