dockerdocker-composegrafanaprometheus

docker-compose opening storage failed: permission denied error on running


I'm trying to execute my docker-compose.yml file which contains prometheus and grafana configurations.

Here is my docker-compose.yml file:

version: '2'
services:
  prometheus:
    image: prom/prometheus
    ports:
      - 9090:9090
    volumes:
      - /prometheus:/prometheus
    command: 
      - --config.file=/etc/prometheus/prometheus.yml 


  grafana:
    image: grafana/grafana
    ports:
      - "3000:3000"
    volumes:
      - /var/lib/grafana:/var/lib/grafana

Whenever I enter docker-compose -f docker-compose.yml up command to run it, I face with these kind of errors about permission:

prometheus_1  | level=error ts=2019-06-30T16:14:42.690Z caller=main.go:723 err="opening storage failed: lock DB directory: open /prometheus/lock: permission denied" 
prometheus_1  | level=error ts=2019-06-30T16:26:11.897Z caller=main.go:723 err="opening storage failed: mkdir data/: permission denied"

I don't know how to solve this problem, I already have searched over github issues and the other stackoverflow's questions, but unfortuntely none of them help!


Solution

  • If you don't need access from the host to these volume files, use a named volume instead of a host mount. Docker will initialize the contents of the named volume, including the permissions and ownership, avoiding permission issues like this:

    version: '2'
    
    volumes:
      prometheus:
      grafana:
    
    services:
      prometheus:
        image: prom/prometheus
        ports:
          - 9090:9090
        volumes:
          - prometheus:/prometheus
        command: 
          - --config.file=/etc/prometheus/prometheus.yml 
    
      grafana:
        image: grafana/grafana
        ports:
          - "3000:3000"
        volumes:
          - grafana:/var/lib/grafana
    

    For a general solution to solve permission issues with host mounts, the fix-perms script in my docker-base image can be used, along with an entrypoint and changes to how the container is started, to dynamically adjust the userid inside the container to match that of the volume mount.