如何将Flask应用程序连接到Docker中运行的SQLite DB?

2024-10-02 02:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

docker-compose.yml

version: "3"

services:
  sqlite3:
    image: nouchka/sqlite3:latest
    stdin_open: true
    tty: true
    volumes:
      - ./db/:/root/db/
  app:
    build:
      context: ../
      dockerfile: build/Dockerfile
    ports:
      - "5000:5000"
    volumes:
      - ../:/app
    command: pipenv run gunicorn --bind=0.0.0.0:5000 --reload app:app

现在,我如何让我的Flask应用程序连接到我的停靠数据库

SQLALCHEMY_DATABASE_URI = 'sqlite:///db.sqlite'(我不确定在这里放什么来连接Docker映像数据库)


Tags: composedockerimagebuild数据库trueappdb
1条回答
网友
1楼 · 发布于 2024-10-02 02:28:35

您可以在容器之间共享卷

version: "3"

services:
  sqlite3:
    image: nouchka/sqlite3:latest
    stdin_open: true
    tty: true
    volumes:
      - ./db/:/root/db/
  app:
    build:
      context: ../
      dockerfile: build/Dockerfile
    ports:
      - "5000:5000"
    volumes:
      - ../:/app
      - ./db/:/my/sqlite/path/ # Here is the change
    command: pipenv run gunicorn  bind=0.0.0.0:5000  reload app:app

现在./db/目录中的文件也可以从python映像访问,因此您可以如下设置URI:

SQLALCHEMY_DATABASE_URI = 'sqlite:////my/sqlite/path/'

相关问题 更多 >

    热门问题