How to connect Flask app to SQLite DB running in Docker?

Viewed 3546

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

Now how do I get my Flask app to connect to my dockerized db?

SQLALCHEMY_DATABASE_URI = 'sqlite:///db.sqlite' (I'm not sure what to put here to connect to the Docker image db)

1 Answers

You can share the volumes between containers.

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

Now the files inside ./db/ directory is accessible from your python image too, so you can set URI like this:

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