Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Added SQLite to the App Store. #7784

Closed
justincnn opened this issue Jan 30, 2025 · 3 comments
Closed

[Feature] Added SQLite to the App Store. #7784

justincnn opened this issue Jan 30, 2025 · 3 comments
Assignees

Comments

@justincnn
Copy link

1Panel Version

v1.10.23-lts

Please describe your needs or suggestions for improvements

对于一些小型的vps,mysql的体积和资源需求太大了,可否考虑增加SQLlite的商店支持,感谢

Please describe the solution you suggest

No response

Additional Information

No response

@zhengkunwang223
Copy link
Member

感谢建议,我们会在后续版本考虑对应用使用 sqlite的支持

@wanghe-fit2cloud
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Thanks for the suggestion, we will consider supporting the use of SQLite in the subsequent version

@wanghe-fit2cloud wanghe-fit2cloud changed the title [Feature] 商店增加SQLlite? [Feature] Added SQLite to the App Store. Jan 30, 2025
@wanghe-fit2cloud
Copy link
Member

以下是使用 Docker Compose 部署 SQLite3 的简明指南。由于 SQLite 是一个文件型数据库(无需独立服务),通常直接通过挂载数据库文件到容器中实现数据持久化。以下示例展示如何将 SQLite 与一个简单应用(如 Python)结合部署:


1. 项目结构

sqlite-app/
├── app/
│   ├── main.py         # 应用代码
│   ├── requirements.txt
│   └── data/           # SQLite 数据库目录(空目录,容器自动创建)
├── docker-compose.yml
└── Dockerfile

2. 核心文件配置

2.1 Dockerfile

FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]

2.2 docker-compose.yml

version: '3.8'
services:
  sqlite-app:
    build: .
    volumes:
      - ./app/data:/app/data  # 挂载数据库目录(关键!)
    environment:
      - SQLITE_DB_PATH=/app/data/mydb.db  # 定义数据库路径

2.3 main.py(示例应用)

import sqlite3
import os

# 从环境变量获取数据库路径
db_path = os.getenv("SQLITE_DB_PATH", "default.db")

# 初始化数据库
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
  CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY,
    name TEXT
  )
''')
conn.commit()

# 插入测试数据
cursor.execute("INSERT INTO users (name) VALUES ('Alice')")
conn.commit()

# 查询数据
cursor.execute("SELECT * FROM users")
print("Users:", cursor.fetchall())

conn.close()

2.4 requirements.txt

sqlite3  # 通常 Python 已内置,此处仅为占位符

3. 启动服务

# 创建挂载目录(避免权限问题)
mkdir -p app/data

# 构建并运行容器
docker-compose up --build

关键说明

  1. 数据持久化
    通过 volumes 将宿主机的 ./app/data 挂载到容器内,确保数据库文件 mydb.db 在容器重启后保留。

  2. 数据库路径
    使用环境变量 SQLITE_DB_PATH 动态配置路径,方便不同环境切换。

  3. 权限问题
    如果遇到数据库写入失败,尝试在宿主机手动创建目录并赋予权限:

    chmod -R a+rw app/data
  4. 多容器共享
    若多个容器需访问同一 SQLite 文件,需确保:

    • 所有容器挂载同一宿主机目录
    • 使用文件锁(SQLite 默认支持,但高并发场景仍建议用 MySQL/PostgreSQL)

验证操作

  1. 查看宿主机数据库文件:

    ls app/data/mydb.db
  2. 进入容器检查数据:

    docker-compose exec sqlite-app bash
    sqlite3 /app/data/mydb.db "SELECT * FROM users;"

常见问题

  • 数据库文件未生成:检查挂载路径权限,或尝试在代码中主动创建目录:
    os.makedirs(os.path.dirname(db_path), exist_ok=True)
  • 并发写入冲突:SQLite 适合低频读写场景,高并发请换用客户端-服务器型数据库。

如果需要更复杂的配置,请进一步描述具体需求!

@wanghe-fit2cloud wanghe-fit2cloud closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants