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



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应用程序连接到我的dockerized db?

SQLALCHEMY_DATABASE_URI = 'sqlite:///db.sqlite'(我不确定要在这里连接到Docker镜像db)

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

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/'

最新更新