2

My goal is to have a program running on the host machine that writes data to a sqlite db that is then transferred (mounted) to a docker-compose running Grafana.

It is possible to do this with the following configuration

grafana:
    container_name: grafana
    networks:
      - backend
    image: grafana/grafana:latest
    volumes:
      - ../database/database.sqlite:/home/grafana/database.sqlite
      - ./grafana/grafana.ini:/etc/grafana/grafana.ini
      - ./grafana/datasource.yml:/etc/grafana/provisioning/datasources/datasource.yaml
    ports:
      - 3000:3000

networks:
  backend:

volumes:
  grafana_data:
    external: true

However, this will only mount the DB at the time of creation, any new changes written to the db will not be reflected on the container.

How can I solve this?

Corfucinas
  • 353
  • 2
  • 11
  • The bind mount is supposed to reflect writes on both the container and the host (and this is most true on native Linux where they are literally the same file). You might find a standalone relational database like PostgreSQL to be a little easier to interact with and a little less fragile, though. – David Maze Oct 21 '22 at 10:17
  • @DavidMaze I tried playing around with that, but never got it to work. Do you have a working example by any chance? – Corfucinas Oct 21 '22 at 12:45
  • The [Docker samples](https://docs.docker.com/samples/) include several database-setup examples, as do a significant fraction of the [tag:docker] questions. – David Maze Oct 21 '22 at 12:56
  • Thanks @DavidMaze, I'll revisit the bind mount and if I come with the solution I'll share it everyone. Thanks for your input – Corfucinas Oct 21 '22 at 13:28
  • I'd take a look at [this](https://stackoverflow.com/a/1063768) and [this](https://stackoverflow.com/q/10325683) StackOverflow questions. Having multiple concurrent connections to an SQLite database is not trivial. – Itai Steinherz Oct 21 '22 at 17:25
  • Thank you for the comment @ItaiSteinherz, the host will be the only one writing to the DB, the docker image only needs `ro` access and they can wait for a `lock` without any issues (display some metrics on Grafana can be deferred). Going to go over your links in a moment as well – Corfucinas Oct 22 '22 at 11:23

1 Answers1

1

It is possible to mount a live db (in this case I'm using Sqlite for some dashboard visualization in Grafana. The docker-compose.yml is as follows:

The sqlite db will exist, be written and read from the host machine, this in turn will be binded to the docker container.

grafana:
    container_name: grafana
    networks:
      - backend
    image: grafana/grafana:latest
    volumes:
      - type: bind
        source: database/database.sqlite
        target: /home/grafana/database.sqlite # needs to be absolute
      - ./grafana/grafana.ini:/etc/grafana/grafana.ini
      - ./grafana/datasource.yml:/etc/grafana/provisioning/datasources/datasource.yaml
    ports:
      - 3000:3000

networks:
  backend:

volumes:
  grafana_data:
    external: true

Keep in mind that if you're using PRAGMA journal_mode=WAL;, the docker container will not reflect the changes until the journal is closed.

Corfucinas
  • 353
  • 2
  • 11