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

Step 1: Pull and Run the Grafana Container

sudo docker run -d -p 3000:3000 --name=grafana grafana/grafana-oss
 

What each flag does:

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

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

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

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.

Related Articles

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles