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

에이바*_*에이바 6 python sqlite docker docker-compose

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
Run Code Online (Sandbox Code Playgroud)

现在我如何让我的 Flask 应用程序连接到我的 dockerized 数据库?

SQLALCHEMY_DATABASE_URI = 'sqlite:///db.sqlite' (我不知道在这里放什么来连接到 Docker 镜像数据库)

mes*_*ati 9

您可以在容器之间共享卷。

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
Run Code Online (Sandbox Code Playgroud)

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

SQLALCHEMY_DATABASE_URI = 'sqlite:////my/sqlite/path/'
Run Code Online (Sandbox Code Playgroud)