# 1.启动文件

base_run.sh 文件内容如下

#!/bin/sh

uvicorn ServerApp:app --host 0.0.0.0 --port 8080 --loop asyncio
1
2
3

# 2.Dockerfile

FROM python:3.11.5-slim

WORKDIR /kg-qa/

COPY .env .env
COPY ./base ./base
COPY ./manager ./manager
#COPY ./sqlalchemy_manager ./sqlalchemy_manager
COPY ./config/baseline.yaml ./config/baseline.yaml
COPY ./config/logger.ini ./config/
COPY ./base_run.sh ./ServerApp.py ./__init__.py ./
COPY ./requirements.txt ./requirements.txt

# 安装 Python 依赖
RUN pip install --upgrade pip
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

# 将应用代码复制到工作目录
COPY . /kg-qa/

ENTRYPOINT ["sh", "./base_run.sh"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 3..env

# 使用本地 MySQL 数据库
DATABASE=mysql
DATABASE_URL=mysql://root:156716282221qyj.@43.139.90.182:3306/kg

# logger相关配置
LOGGER_BACKCOUNT= 7
# S 秒
# M 分
# H 小时、
# D 天、
# W 每星期(interval==0时代表星期一)
# midnight 每天凌晨
LOGGER_INTERVAL= midnight
LOGGER_DATEFMT= %Y-%m-%d %H:%M:%S
# 日志级别 # ERROR = 40, WARNING = 30, INFO = 20, DEBUG = 10
LOGGER_LEVEL= DEBUG
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 4.startup.sh

服务器启动文件

#!/bin/sh

# 定义镜像名称和标签
IMAGE_NAME="kg-qa:latest"
CONTAINER_NAME="kg-qa"

# 切换到项目目录
cd /data/kg/kg-qa

# 切换到开发分支并拉取最新代码
echo "Checking out to development branch and pulling latest changes..."
git checkout dev
git pull

# 构建Docker镜像
echo "Building Docker image..."
docker build -t $IMAGE_NAME .

# 检查容器是否正在运行
if docker ps -a | grep -q $CONTAINER_NAME; then
    echo "Stopping and removing existing container..."
    docker stop $CONTAINER_NAME
    docker rm $CONTAINER_NAME
fi

# 启动Docker容器
echo "Starting container..."
docker run -d \
    --name $CONTAINER_NAME \
    -p 8080:8080 \
    --restart=always \
    --privileged=true \
    -e TZ=Asia/Shanghai \
    -v /etc/localtime:/etc/localtime:ro \
    -v /data/kg/config/env.properties:/kg-qa/.env \
    -v /data/kg/config:/kg-qa/config \
    $IMAGE_NAME

echo "Container started successfully!"

# 打印容器日志
echo "Container logs:"
docker logs -f $CONTAINER_NAME
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
上次更新: 11/3/2024, 10:37:37 AM