File size: 1,574 Bytes
18b2392
71f6342
 
18b2392
71f6342
 
 
 
 
 
7f49ed9
18b2392
 
 
71f6342
 
 
 
fc5ef51
7f49ed9
71f6342
 
 
 
7f49ed9
 
 
 
 
 
 
18b2392
71f6342
fc5ef51
 
71f6342
 
 
 
 
18b2392
71f6342
 
7f49ed9
18b2392
 
71f6342
 
 
 
 
 
7f49ed9
71f6342
7f49ed9
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
# 使用 PostgreSQL 官方镜像
FROM postgres:latest

# 设置环境变量,用于数据库配置
ENV POSTGRES_USER=myuser \
    POSTGRES_PASSWORD=mypassword \
    POSTGRES_DB=mydatabase \
    VIRTUAL_ENV=/opt/venv \
    PATH="$VIRTUAL_ENV/bin:$PATH"

# 切换到 root 用户进行安装和用户修改
USER root

# 更新包管理器并安装必要软件包,包括 Python3、venv 和 curl
RUN apt-get update && apt-get install -y \
    python3 \
    python3-pip \
    python3-venv \
    curl \
    gosu \
    --no-install-recommends && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# 更改现有的 postgres 用户 UID 和 GID 为 1000
RUN usermod -u 1000 postgres && groupmod -g 1000 postgres

# 修正数据库目录的所有者和组,确保新 UID 和 GID 能正常访问
RUN chown -R postgres:postgres /var/lib/postgresql && \
    chown -R postgres:postgres /var/run/postgresql

# 创建虚拟环境并安装 Python 包
RUN python3 -m venv $VIRTUAL_ENV && \
    $VIRTUAL_ENV/bin/pip install --upgrade pip && \
    $VIRTUAL_ENV/bin/pip install Flask psycopg2-binary

# 将应用程序代码和启动脚本复制到容器中
COPY app.py /app/app.py
COPY run.sh /app/run.sh

# 设置脚本可执行权限
RUN chmod +x /app/run.sh

# 切换到更改后的 postgres 用户
USER postgres

# 设置工作目录
WORKDIR /app

# 启动容器时执行run.sh脚本
CMD ["./run.sh"]

# 设置健康检查以确保 Flask 应用正常运行
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
    CMD curl -f http://localhost:7860/ || exit 1