Introduction
Grafana is a leading open-source platform for monitoring and observability. It allows you to query, visualize, alert on, and understand your metrics no matter where they are stored. With Grafana, you can create, explore, and share dashboards with your team and foster a data-driven culture.
In this lab, you will use Docker to quickly and easily set up a Grafana instance. You will start Prometheus as the data source before starting Grafana. Image downloads run visibly in Terminal so you can monitor progress and retry failures. Your goal is to install Grafana in its own container and connect it to this Prometheus instance, setting the stage for future data visualization tasks.
Prepare Prometheus and Pull Grafana Images
In this step, you will prepare the metrics service and download Grafana. Prometheus collects metrics; Grafana queries them for visualization. The initial Prometheus configuration file is prepared for you. You start the containers yourself.
Work in the project directory.
cd /home/labex/project
Each pinned tag selects the version used in this lab. Downloads can take several minutes. Wait for each command to return to the prompt. If a pull fails, rerun that same pull; Docker reuses completed layers. Do not continue while a download is incomplete.
docker pull prom/prometheus:v3.3.0
docker pull grafana/grafana:11.6.0
docker network create creates a network where containers can resolve each other by name. docker run -d starts a container in the background; --name names it, --network joins that network, and -p maps a host port to a container port. The read-only bind mount (-v ...:ro) lets Prometheus read the prepared configuration.
docker network create monitoring
docker run -d --name prometheus --network monitoring -p 9090:9090 -v /home/labex/project/prometheus.yml:/etc/prometheus/prometheus.yml:ro prom/prometheus:v3.3.0
The curl options report HTTP failures and retry temporary startup errors up to 30 times, two seconds apart. A running container can still be initializing, so wait for the health check to succeed before continuing.
curl --fail --silent --show-error --retry 30 --retry-delay 2 --retry-connrefused --retry-all-errors http://localhost:9090/-/ready
A successful response says Prometheus Server is Ready.. The Grafana image is now available for the next step.
Run Grafana Container on Host Port 8080
In this step, you will run the Grafana image as a container. We will use the docker run command, which creates and starts a new container from a specified image.
We will also use several flags to configure the container:
-d: Runs the container in detached mode (in the background).-p 8080:3000: Maps port 8080 of the host machine to port 3000 of the container. This makes the Grafana web UI accessible from your browser while avoiding conflicts with services already using port 3000.--name grafana: Assigns a custom name to the container for easy reference.--network monitoring: Connects the container to themonitoringnetwork. This is crucial as it allows this Grafana container to communicate with the Prometheus container (which is on the same network) using their container names.
Run the following command to start your Grafana container:
docker run -d -p 8080:3000 --name grafana --network monitoring grafana/grafana:11.6.0
The command will output a long string, which is the unique ID of the newly created container.
a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6
You can verify that the container is running with the docker ps command.
docker ps
You should see the grafana container listed with a status of Up.
The curl options report HTTP failures and retry temporary startup errors up to 30 times, two seconds apart. A running container can still be initializing, so wait for the health check to succeed before continuing.
curl --fail --silent --show-error --retry 30 --retry-delay 2 --retry-connrefused --retry-all-errors http://localhost:8080/api/health
The response must contain "database": "ok".
Open the Grafana Interface
In this step, you will open the service you started. Select the Grafana lab tab after the health check succeeds. If your browser blocks embedded login, open that tab in a new browser tab using its open-in-browser control.
You should see the Grafana login form. This interface opens Grafana running inside the VM; localhost in your own browser would refer to your computer, not the VM. Keep Terminal available for container checks.
First Login and Password Setup
In this step, you will log in to Grafana for the first time. A new Grafana instance offers a password-change screen after the initial login.
The default credentials are username admin and password admin. Enter them on the login form and click Log in.
On the password-change screen, click Skip. Keep the default credentials for this temporary lab because its verification checks use them to inspect your data source. In a production deployment, replace default credentials with a strong password.
You should now see the Grafana home page. You are ready to configure its connection to Prometheus.
Add Prometheus as Data Source in Grafana
In this step, you will connect Grafana to your Prometheus instance, which you started in the first step. This will allow Grafana to query metrics from Prometheus.
Follow these instructions within the Grafana UI:
On the left-hand menu, click the Connections icon (looks like a plug or connector).
In the Connections page, click on Data sources.
On the Data sources page, click the Add data source button.
From the list of available data source types, select Prometheus. Set Name to Prometheus.
Now you need to configure the connection settings. The most important setting is the URL.
- In the Prometheus server URL field, enter
http://prometheus:9090.
This URL works because both the Grafana and Prometheus containers are connected to the same Docker network (monitoring). Docker's internal DNS allows containers on the same network to resolve each other by their container name (prometheus in this case).
After entering the URL, scroll to the bottom of the page and click the Save & test button.
Grafana will attempt to connect to the Prometheus server. If the connection is successful, you will see a green notification that says "Successfully queried the Prometheus API".
You can now follow the guide to explore Grafana's features. After adding Prometheus as a data source, feel free to click on Explore in the left-hand menu. Here, you can run queries against your Prometheus data and visualize the results in real time. Try out different queries to get familiar with how Grafana interacts with your metrics!
Summary
Congratulations! You have successfully completed this lab.
In this lab, you have learned several fundamental skills for working with Grafana and Docker:
- How to pull an official Docker image from Docker Hub.
- How to run a Docker container with specific network and port configurations.
- How to access a web application running inside a container.
- How to perform the initial security setup for a new Grafana instance.
- How to connect Grafana to a Prometheus data source within a shared Docker network.
You now have a fully functional Grafana instance connected to a data source, ready for you to start building powerful and insightful dashboards to visualize your data.



