Search code examples
dockerdocker-composedocker-volume

docker-compose, mount live database from host (live sync)


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?


Solution

  • 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.