Back

How to run Grafana in Docker container

How to run Grafana in Docker container

Grafana is a leading open-source tool for visualizing time series data. It’s widely used to visualize data from various sources—such as Prometheus, InfluxDB, and Loki. A fast and flexible way to deploy Grafana is to use Docker, which lets you run it in a containerized environment. This makes it easy and convenient to install the tool without heavy operating system configuration.

In this tutorial, you’ll learn how to run Grafana with Docker. Specifically, you’ll see how to:

  • Download and start a Grafana Docker container with a single command.
  • Expose Grafana’s ports to access the dashboard via a browser.
  • Persist Grafana settings and dashboards across container restarts.
  • Configure Grafana using environment variables or a Dockerfile.
  • Simplify deployment using Docker Compose to orchestrate multiple containers.

Prerequisites

Before you begin, make sure your system meets the following requirements:

  • Docker installed: Docker Engine must be installed and running on your system.
  • Command line basics: You should be comfortable using a terminal and executing Docker commands.
  • Sudo privileges: On Linux, you’ll need to run Docker commands with sudo or add your user to the docker group.


Downloading the Grafana Docker Image

First, pull the Grafana image from Docker Hub. This image contains everything needed to install and run Grafana in a container. Use:

doc​ker pull grafana/grafana


If you don’t specify a version tag, Docker will automatically pull the latest stable version. If you want a specific version, for example 10.3.1:

doc​ker pull grafana/grafana:10.3.1

You can find all available Grafana tags on Docker Hub.


After running this command:

  • The official Grafana Docker image is downloaded and stored locally.
  • Docker can create containers from this image, including all required dependencies.


Running the Grafana Container

Once the image is downloaded, you can start a container with a single docker run command. This is the quickest way to run Grafana locally or on a server.

Start command:

doc​ker r​un -d -p 3000:3000 --name=grafana grafana/grafana


What this command does:

  • docker run - creates and starts a new container.
  • -d - runs the container in the background (detached mode).
  • -p 3000:3000 - maps port 3000 on your machine to port 3000 in the container (Grafana’s default port).
  • --name=grafana - gives the container a recognizable name.
    grafana/grafana - specifies which image to use.

Check that the container is running:

doc​ker p​s


In the output, you should see a running container named "grafana".

This approach lets you launch Grafana in seconds - no complex configuration required.


Accessing the Grafana Dashboard

After the container starts, open your browser and navigate to the host’s IP address (the machine running the Docker container) with the Grafana port:

ip-adresas:3000


On the login screen, use the default credentials:
User - admin
Password - admin

After you sign in, Grafana will prompt you to change the default password. It’s recommended to do this immediately, especially if the container is accessible from a public network or the internet.

You’ll then land on the main dashboard, where you can add data sources, create dashboards, and add panels.



Persisting Grafana Data

By default, all data inside a container is lost when it’s stopped or removed - including plugins, user settings, and dashboards. That’s fine for testing, but not for long-term use.
To persist data, mount a volume so Grafana’s data is stored outside the container.

Create a Docker volume:

doc​ker vol​ume create grafana-storage

This volume will back Grafana’s /var/lib/grafana directory in the container.


1. Check that the volume is created and not in use:

doc​ker vol​ume ls -f dangling=true

2. Stop and remove the existing container:

doc​ker st​op grafana
doc​ker r​m grafana

3. Start a new container with the volume mounted:

doc​ker r​un -d \
  -p 3000:3000 \
  --name=grafana \
  --vol​ume grafana-storage:/var/lib/grafana \
  grafana/grafana

4. Verify the volume is mounted:

doc​ker ins​pect --format '{{json .Mounts}}' grafana


You can now safely stop, start, or upgrade the container without losing settings. Docker will load files into Grafana’s data directory from the mounted volume, and Grafana will automatically restore your state.


Configuring Grafana with Environment Variables

Grafana supports numerous environment variables that let you tailor settings at container startup without editing configuration files. This is especially useful for automation, scripts, and consistent environments.


A few common variables:
GF_SECURITY_ADMIN_PASSWORD - sets the initial admin password.
GF_SERVER_ROOT_URL - defines the root URL; useful when using a reverse proxy.
GF_USERS_ALLOW_SIGN_UP - enables or disables self-service user signup.
GF_INSTALL_PLUGINS - installs plugins automatically at startup (comma-separated list).


Pass environment variables with -e:

doc​ker r​un -d \
  -p 3000:3000 \
  --name=grafana \
  -e GF_SECURITY_ADMIN_PASSWORD=Password \
  -e GF_SERVER_ROOT_URL=http://grafana.example.com \
  -e GF_USERS_ALLOW_SIGN_UP=false \
  grafana/grafana


In this example:

  • The default password is changed to Password.
  • Grafana is told that the root URL is http://grafana.example.com (behind a reverse proxy).
  • Self-service signup is disabled.


You can combine this with a mounted volume for a fully customized, persistent setup.
Environment variables are applied each time the container starts, which makes them perfect for version-controlled deployments or Docker Compose configurations.


Building a Custom Grafana Docker Image

Creating your own Grafana Docker image lets you:

  • Pre-install specific plugins
  • Include custom configuration files
  • Customize branding or themes
  • Set environment-specific defaults


1. Create a Dockerfile:

FR​OM grafana/grafana:latest
# Ins​tall one or more plu​gins
R​UN grafa​na-cli plu​gins ins​tall grafana-clock-panel
# Optional: add your configuration
# CO​PY custom.ini /etc/grafana/grafana.ini
2. Build the image:

doc​ker bu​ild -t my-grafana-custom .

3. Run the custom image:

doc​ker r​un -d \
  -p 3000:3000 \
  --name=grafana-custom \
  my-grafana-custom

This container behaves like a standard Grafana instance, but with your plugins and changes. You can still mount volumes and pass environment variables. Custom images are great for teams, CI/CD pipelines, and repeatable deployments, giving you full control with minimal setup.

Use a custom image if you:

  • Deploy Grafana frequently and want consistent state
  • Automate infrastructure and need a simple solution
  • Want plugins or configurations included without manual steps

Running Grafana with Docker Compose

Managing containers with docker run works for a single instance. But as your environment grows, or if you want a cleaner, repeatable deployment - use Docker Compose. It lets you describe the entire configuration in a single YAML file and start everything with one command.

Create a docker-compose.yml file:

version: '3.8'
services:
  grafana:
    image: grafana/grafana:latest
    container_name: grafana
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=Password
      - GF_USERS_ALLOW_SIGN_UP=false
    volumes:
      - grafana-storage:/var/lib/grafana
    restart: unless-stopped
volumes:
  grafana-storage:


Start the container:

doc​ker-com​pose up -d
To stop the container:

doc​ker-com​pose down


This configuration:

  • Pulls the image if needed.
  • Runs the container in the background (-d).
  • Automatically restarts the container after reboots (restart: unless-stopped).
  • Uses a named volume grafana-storage to persist data.


Grafana is a powerful tool, and with Docker you can launch a Grafana container in just a few commands. If you run into questions or issues, contact our live support, or email: [email protected]


Similar tutorials

How to install Docker on Linux OS Docker is an OS-level virtualization platform designed to deliver applications in a lightweight, isolated runtime environment - containers....

Read

How to list and delete iptables firewall rulesIptables is one of the main firewall management tools used in Linux systems, allowing you to filter network...

Read
Linux Tutorials