Grafana Installation on Ubuntu using Docker
Grafana turns metrics into dashboards — you point it at a data source (Prometheus, InfluxDB, PostgreSQL) and build visual panels on top. Running it with Docker is the fastest install on Ubuntu: one command, no repository setup. This guide covers the container itself, the verification steps, and — most importantly — the volume mount that keeps your dashboards alive when the container is recreated, which is the step most guides leave out.
Prerequisites
- Docker installed and the daemon running (test with
docker ps) - A free port 3000 on your host
Step 1: Pull and Run the Grafana Container
sudo docker run -d -p 3000:3000 --name=grafana grafana/grafana-oss
What each flag does:
-d— run detached (in the background)-p 3000:3000— map host port 3000 to the container's port 3000, exposing the web interface--name=grafana— give the container a stable name so later commands are easy to typegrafana/grafana-oss— the open-source edition image
Step 2: Verify the Container Is Running
sudo docker ps | grep grafana
Expected output — STATUS should show Up (and (healthy) once the healthcheck
passes):
CONTAINER ID IMAGE STATUS PORTS NAMES
abc123def456 grafana/grafana-oss Up 2 minutes 0.0.0.0:3000->3000/tcp grafana
If the container is missing from the list, check the logs — they usually name the problem directly:
sudo docker logs grafana
Step 3: Access Grafana
Open your browser and navigate to http://localhost:3000. If Grafana runs on a remote server,
use its IP or hostname instead of localhost.
Step 4: Initial Setup and Password Change
- First login: username
admin, passwordadmin. - Grafana immediately forces you to set a new admin password — this is mandatory, not optional.
- If you skipped it or want to change it later: click the user icon (lower-left) → Admin → Profile → Change password.
The default credentials are public knowledge, so on any machine reachable from a network, change the password before doing anything else.
Step 5 (Important): Make Your Data Persistent
A bare docker run stores Grafana's database inside the container. The moment you stop and
remove the container, your dashboards and data sources are gone. To keep them, mount a host directory into
the container's data path:
sudo mkdir -p /var/lib/grafana-data
sudo chown 472:472 /var/lib/grafana-data
sudo docker run -d -p 3000:3000 --name=grafana \
-v /var/lib/grafana-data:/var/lib/grafana \
grafana/grafana-oss
472 is the UID of the grafana user inside the container; without the
chown, Grafana cannot write and the container may exit.
If you prefer Compose, see the Docker Compose introduction — the volume line is the same idea.
Useful Commands
- Restart:
sudo docker restart grafana - Logs:
sudo docker logs grafana - List running containers:
sudo docker ps— see the docker ps guide for reading the output - Stop and remove:
sudo docker stop grafana && sudo docker rm grafana - Clean unused resources:
sudo docker system prune
Common Problems
Problem 1: "Bind for 0.0.0.0:3000 failed: port is already allocated"
Cause: another process (or another Grafana) already uses port 3000.
Solution: find the occupant with sudo netstat -tulpn | grep 3000 (see the
netstat guide) and either stop it or publish Grafana on a different host
port: -p 3001:3000.
Problem 2: container starts then immediately exits
Cause: usually the data directory permission (Grafana runs as UID 472) or a corrupted volume.
Solution: read sudo docker logs grafana; if the error mentions
mkdir /var/lib/grafana: permission denied, fix ownership:
sudo chown 472:472 /var/lib/grafana-data.
Problem 3: Grafana loads but says it cannot connect to the data source
Cause: the data source is on the host, and localhost inside the container is the container itself, not your machine.
Solution: use host.docker.internal (or your host IP) as the data source
URL instead of localhost. For monitoring stacks, see
Grafana + Prometheus + Node Exporter.
Problem 4: forgot the admin password
Solution: reset it through the Grafana CLI inside the running container:
sudo docker exec -it grafana grafana-cli admin reset-admin-password <newpassword>
Best Practices
- Always mount a volume for
/var/lib/grafana— dashboards are work, not disposable data. - Change the default
admin/adminpassword immediately. - Use
docker logs grafanaas your first diagnostic when the web UI does not come up. - Pin the image version (
grafana/grafana-oss:11.x) for reproducible setups. - Persist configuration in environment variables or a compose file instead of editing files inside the container — edits are lost on recreate.
Conclusion
We ran the Grafana container, verified it with docker ps and docker logs, logged
in to change the default password, and added a volume so dashboards survive container recreation. The
important takeaway: the container itself is disposable — the data directory you mount is what you are
actually running.