镜像制作
This commit is contained in:
101
docker/Dockerfile.mysql
Normal file
101
docker/Dockerfile.mysql
Normal file
@@ -0,0 +1,101 @@
|
||||
# 校园新闻管理系统 - MySQL数据库镜像
|
||||
# 基于reInit.sh的数据库初始化方案
|
||||
FROM mysql:8.0
|
||||
|
||||
# 设置环境变量
|
||||
ENV LANG=C.UTF-8 \
|
||||
TZ=Asia/Shanghai
|
||||
|
||||
# 复制MySQL配置文件
|
||||
COPY docker/mysql/my.cnf /etc/mysql/conf.d/my.cnf
|
||||
|
||||
# 创建SQL目录
|
||||
RUN mkdir -p /docker-entrypoint-initdb.d /opt/sql
|
||||
|
||||
# 复制所有SQL文件(保持目录结构)
|
||||
COPY schoolNewsServ/.bin/mysql/sql/ /opt/sql/
|
||||
|
||||
# 复制并调整reInit.sh为Docker环境
|
||||
COPY schoolNewsServ/.bin/mysql/sql/reInit.sh /opt/sql/
|
||||
RUN sed -i 's/DB_HOST="localhost"/DB_HOST="localhost"/' /opt/sql/reInit.sh && \
|
||||
sed -i 's/DB_PORT="3306"/DB_PORT="3306"/' /opt/sql/reInit.sh && \
|
||||
sed -i 's/DB_USER="root"/DB_USER="root"/' /opt/sql/reInit.sh && \
|
||||
sed -i 's/DB_PASSWORD="123456"/DB_PASSWORD="${MYSQL_ROOT_PASSWORD}"/' /opt/sql/reInit.sh && \
|
||||
sed -i 's/DB_NAME="school_news"/DB_NAME="${MYSQL_DATABASE}"/' /opt/sql/reInit.sh && \
|
||||
chmod +x /opt/sql/reInit.sh
|
||||
|
||||
# 创建Docker初始化适配脚本
|
||||
RUN cat > /docker-entrypoint-initdb.d/01-init-database.sh <<'EOF'
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "=========================================="
|
||||
echo "校园新闻管理系统 - 数据库初始化"
|
||||
echo "使用 reInit.sh + Docker配置更新"
|
||||
echo "=========================================="
|
||||
|
||||
# 等待MySQL完全启动
|
||||
echo "等待MySQL启动..."
|
||||
until mysql -uroot -p"${MYSQL_ROOT_PASSWORD}" -e "SELECT 1" >/dev/null 2>&1; do
|
||||
sleep 1
|
||||
done
|
||||
echo "MySQL已就绪"
|
||||
|
||||
# 切换到SQL目录
|
||||
cd /opt/sql
|
||||
|
||||
# 设置环境变量供reInit.sh使用
|
||||
export DB_HOST="localhost"
|
||||
export DB_PORT="3306"
|
||||
export DB_USER="root"
|
||||
export DB_PASSWORD="${MYSQL_ROOT_PASSWORD}"
|
||||
export DB_NAME="${MYSQL_DATABASE}"
|
||||
export MYSQL_PWD="${MYSQL_ROOT_PASSWORD}"
|
||||
|
||||
# 直接调用reInit.sh的初始化函数
|
||||
echo "执行数据库初始化..."
|
||||
|
||||
# Source reInit.sh并调用其初始化函数
|
||||
source reInit.sh
|
||||
|
||||
# 调用reInit.sh的核心函数(跳过备份和删除)
|
||||
execute_init_script # 执行initAll.sql
|
||||
import_sensitive_words # 导入敏感词
|
||||
|
||||
# Docker环境特定配置:更新爬虫路径
|
||||
echo "更新Docker环境配置..."
|
||||
mysql -uroot "${MYSQL_DATABASE}" <<EOSQL
|
||||
-- 更新爬虫配置为Docker容器内路径
|
||||
UPDATE tb_sys_config
|
||||
SET config_value = '/usr/bin/python3'
|
||||
WHERE config_key = 'crawler.pythonPath';
|
||||
|
||||
UPDATE tb_sys_config
|
||||
SET config_value = '/app/crawler'
|
||||
WHERE config_key = 'crawler.basePath';
|
||||
|
||||
-- 如果配置不存在则插入
|
||||
INSERT IGNORE INTO tb_sys_config (config_key, config_value, config_desc, created_at)
|
||||
VALUES
|
||||
('crawler.pythonPath', '/usr/bin/python3', 'Docker容器内Python路径', NOW()),
|
||||
('crawler.basePath', '/app/crawler', 'Docker容器内爬虫脚本路径', NOW());
|
||||
|
||||
SELECT '✅ 数据库初始化完成!' AS message;
|
||||
SELECT '默认用户: admin, 密码: admin123' AS tip;
|
||||
SELECT '爬虫配置已更新为Docker容器路径' AS docker_config;
|
||||
EOSQL
|
||||
|
||||
echo "=========================================="
|
||||
echo "✅ 初始化完成!"
|
||||
echo "=========================================="
|
||||
EOF
|
||||
|
||||
# 设置执行权限
|
||||
RUN chmod +x /docker-entrypoint-initdb.d/01-init-database.sh
|
||||
|
||||
# 暴露端口
|
||||
EXPOSE 3306
|
||||
|
||||
# 健康检查
|
||||
HEALTHCHECK --interval=10s --timeout=5s --retries=5 --start-period=30s \
|
||||
CMD mysqladmin ping -h localhost -p${MYSQL_ROOT_PASSWORD} || exit 1
|
||||
Reference in New Issue
Block a user