Where it comes from
Grafana runs inside the container as uid 472. On a fresh server ./grafana doesn’t exist yet, so Docker creates it
owned by root (A missing volume path). Grafana can’t write there and crashes; Docker restarts it in a loop, and
docker compose ps shows Restarting (1):
GF_PATHS_DATA='/var/lib/grafana' is not writable.
mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
The usual fix is sudo chown 472:0 grafana on the host — a manual step that gets forgotten on the next server.
How to write it
services:
grafana-init:
image: busybox:1.37
user: root
command: chown -R 472:0 /var/lib/grafana
volumes:
- ./grafana:/var/lib/grafana
grafana:
image: grafana/grafana:13.3.0
restart: unless-stopped
depends_on:
grafana-init:
condition: service_completed_successfully
volumes:
- ./grafana:/var/lib/grafana
grafana-init mounts the same directory, runs chown as root, and exits. The service_completed_successfully
condition holds Grafana back until grafana-init exits with code 0. The one-off service runs on every up -d —
running chown again breaks nothing. It doesn’t appear in docker compose ps, and ps -a shows Exited (0).
After a server reboot, Docker starts Grafana on its own without the init service: the ownership is already on disk.
Get the numeric IDs for chown from the image:
docker run --rm --entrypoint id grafana/grafana:13.3.0
# uid=472(grafana) gid=0(root) groups=0(root),0(root)
Why not post_start
Compose has a post_start hook that runs a command inside the container after startup, so putting chown there looks
tempting. The hook runs concurrently with the app, with no ordering guarantee. Grafana crashes within a second, before
the hook finishes:
Error response from daemon: Container … is restarting, wait until the container is running
up -d exits with code 1. After that Docker restarts the container itself and the hook never runs again: hooks are
triggered only by Compose on up, start, and restart. Nothing runs them on docker restart or after a dockerd
restart.
Use post_start only when the app can survive its first few seconds without what the hook sets up.
If it doesn’t work
up -d: service "grafana-init" didn't complete successfully: exit 1, Grafana stuck in Created
- Cause
chownfailed — usually the path in the init service doesn't match Grafana's path. Permissions weren't fixed, so Compose doesn't start Grafana.- Fix
- Check
docker compose logs grafana-initand compare thevolumeslines on both services.
Still Permission denied after chown
- Cause
- Wrong uid: every image has its own user.
.Config.Usercan be a username, andchownin busybox doesn't know usernames from another image. - Fix
- Get the numeric IDs:
docker run --rm --entrypoint id <image>— foradminer:5that'suid=100 gid=101.
The post_start hook prints hook exited with status 127: … executable file not found in $PATH
- Cause
- A hook command runs inside the app image, and minimal images have neither
chownnor a shell. - Fix
- Move the command into a one-off
busyboxservice.
How to check
ls -ldn grafana
# drwxr-xr-x 3 472 0 4096 … grafana
docker compose ps --format '{{.Service}} {{.Status}}'
# grafana Up 12 seconds
The status is Up without Restarting, and the directory owner is 472 0. The login page responds after roughly
30 s: Grafana creates its database on the first boot.