Signup/Sign In

How to Run Grafana in Docker Container

Posted in Programming   LAST UPDATED: APRIL 11, 2023

    What is Grafana?

    Are you looking for an efficient way to visualize and analyze data over a period of time? Then you must try using Grafana.

    Grafana is software that is used for visualization and analytics. Grafana enables us to develop plugins to integrate with different data sources.

    Grafana helps us to analyse and monitor over a period of time which is also called time series analytics. It has a dashboard that pulls from plugged-in data sources such as Graphite, Prometheus, Influx DB, ElasticSearch, MySQL, PostgreSQL, etc.

    After this tutorial, you will be able to run Grafana software in your docker container. Let's start it.

    grafama

    How to run Grafana in Docker Container?

    We can simply create a docker container using the following command:

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

    Visit http://localhost:3000 and you will be able to access the Grafana server. Login as username - admin, password - admin.

    Alright, now you can add data sources and different dashboards to create your visualizations using Grafana. Press the “Add your first data source” button on the Grafana server to connect Grafana to a new source then fill in the details so Grafana can access the data from the sources.

    Now, you need to create the dashboard, click on “Create your first dashboard.” and choose the visualization type you want. Select the appropriate metrics by using the query pane from your data. Now, you can see your visualizations on the Grafana dashboard.

    Select the image Variant

    There are multiple Grafana versions available such as Alpine and Ubuntu. Alpine is slimmer and it is focused on providing a Grafana-compatible environment so it is preferred in most deployment situations.

    You can select the OS by writing its name along with the Grafana version in an image tag -

    grafana/grafana:8.3.0-ubuntu

    Create the Container

    In order to create a container, you need to mount the Docker Volume to store your persistent data and then bind it to a host port. Now you can access the service.

    Mount the config file into the container or inject environment variables so the settings can be supplied.

    docker run -d --name grafana -p 9000:3000 
        -v grafana-data:/var/lib/grafana
        grafana/grafana:8.3.0

    Above example will start a Grafana container that is named grafana. It listens on port 9000 on the docker host that is bound to 3000.

    Injecting the Configuration

    Capitalize the key name and prepend GF_: to change the keys in Grafana's INI-format config files. You can also override Grafana’s config keys by setting environment variables when you start your container.

    # INI file
    instance_name = my-grafana
    
    [security]
    admin_user = demo
    admin_password = grafana
    
    ---
    
    # Corresponding environment variables
    GF_DEFAULT_INSTANCE_NAME=my-grafana
    GF_SECURITY_ADMIN_USER=demo
    GF_SECURITY_ADMIN_PASSWORD=grafana

    Include the implicit DEFAULT section name when you are changing the value of a top-level variable in the config file.

    Grafana supports file-based configuration with which the value of the target environment variable becomes the path to a file. Grafana will read the file and obtain the setting's real value.

    You can use this approach and modify any environment variable by suffixing __FILE to its regular name:

    docker run -d --name grafana -p 9000:3000 
        -e GF_DEFAULT_INSTANCE_NAME=my-grafana
        -e GF_SECURITY_ADMIN_USER=demo
        -e GF_SECURITY_ADMIN_PASSWORD__FILE=/run/secrets/password
        -v grafana-data:/var/lib/grafana
        grafana/grafana:8.3.0

    Overriding the Config File

    The config file is located at /etc/grafana/grafana.ini. You can use the Docker Bind Mount to mount a replacement to the expected path.

    docker run -d --name grafana -p 9000:3000 
        -v ./grafana.ini:/etc/grafana/grafana.ini
        -v grafana-data:/var/lib/grafana
        grafana/grafana:8.3.0

    Managing the Grafana Plugins

    You can add extra data sources and provide pre-built dashboard panels for Grafana installations using plugins. The Docker image has a helper utility that allows you to add plugins to a new container by setting a special environment variable.

    You can add a plugins that is listed in the Grafana's catalog -

    docker run -d --name grafana -p 9000:3000 
        -e GF_INSTALL_PLUGINS=grafana-simple-json-datasource
        -v grafana-data:/var/lib/grafana
        grafana/grafana:8.3.0

    When the container is started, the GF_INSTALL_PLUGINS variable expects a list of plugin names that is to be installed.

    These names will be passed to the grafana-cli plugins install command.-

    docker run -d --name grafana -p 9000:3000 
        -e GF_INSTALL_PLUGINS=https://example.com/grafana-plugin.zip
        -v grafana-data:/var/lib/grafana
        grafana/grafana:8.3.0

    Conclusion

    Now, you will be able to run Grafana in your docker container. Running it provides a flexible and scalable solution for visualizing and analysing data.

    By following the steps mentioned in this article, you can easily create a Grafana container and connect it to various data sources to create custom dashboards. Also, You can start new Grafana instances by binding a host port, mounting a data volume, and supplying config values via environment variables. With these tools, you will have a powerful toolset for monitoring and analyzing data for your applications.

    Frequently Asked Questions

    1. How do I access Grafana in a Docker container?

    You can access Grafana in a Docker container by using the "docker run" command, you can access Grafana by navigating to http://localhost:3000 in your web browser. You will need to login with the default username and password. You can then add data sources and create dashboards to visualize your data.

    2. How do I persist data in a Grafana container?

    To persist data in a Grafana container, you can use Docker volumes. A volume is a directory on the host machine that is mounted into the container, allowing data to be shared between the container and the host.

    3. Can I run multiple instances of Grafana in Docker?

    Yes, you can run multiple instances of Grafana in Docker by using different port mappings for each instance. For example, you can run two instances of Grafana by mapping ports 3000 to 3000 for the first and 3001 to 3000 for the second instance.

    4. Can I use a custom Grafana configuration file with Docker?

    Yes, you can use a custom Grafana configuration file with Docker by using the Docker Bind Mount to mount a replacement to the expected path.

    You May Also Like :

    About the author:
    Proficient in Java, Python, and web development; has a knack for writing clearly about complex topics. Dedicated to giving readers the tools they need to learn more about computer science and technology.
    Tags:cloud-serviceslinuxdockergrafana
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS