Deploying InfluxDB 2.0 Using Docker
InfluxDB 2.0 is generally available. This version of InfluxDB provides the time-series database and a set of tools to create beautiful dashboards using either the query builder or writing the Flux query manually. You can also schedule tasks and configure alerts when a measurement exceeds a certain threshold.
Months ago, I always deploy InfluxDB along with Grafana or Chronograf for visualization. Now I only need to deploy InfluxDB 2.0 image alongside the applications that I developed.
Let’s get started!
First of all, InfluxData provides a comprehensive guide for deploying InfluxDB 2.0 using docker. So, I’m not going to describe everything in detail. Instead, I’m going to describe how the configuration or setup process can be automated using a bash script.
You will need to pull the image from the docker hub using the following command:
docker pull influxdb:2.0.7
If you want to customize the configuration, you will need to create the config.yml file and mount it as a volume to the docker container
docker run --rm influxdb:2.0.7 influxd print-config > config.yml
A config.yml file will be created inside your working directory.
Now, if you want to persist the database on your host machine, you can create a directory and mount it to /var/lib/influxdb2 on the container.
So, let’s create the container
docker run --name influxdb -d \
-p 8086:8086 \
--volume `pwd`/influxdb2:/var/lib/influxdb2 \
--volume `pwd`/config.yml:/etc/influxdb2/config.yml \
influxdb:2.0.7
The command above will create a container named influxdb
and mount the config.yml file and influxdb2 directory inside your working directory. It also binds the 8086 port of your host machine to the port 8086 of the container, assuming that you don’t have any software running and using that port on your host machine.
Remember that our goal is to create a bash script that can automate the configuration process. But we’ll walk through every process manually.
Next, we’re going to set up an account that we can use to access the dashboard, writing, and querying data from the database. To do this, we need to use the setup
command of influx
command-line interface tool.
You can see the complete flags for that command here. But we’re going to use some of the flags as shown in the following command:
docker exec influxdb influx setup \
--bucket BUCKET_NAME \
--org ORG_NAME \
--password PASSWORD \
--username USERNAME \
--force
A bucket is a group of measurements. You can think of a bucket as a database in a relational database and measurements as the tables. You need to provide the name of the bucket and replace the BUCKET_NAME
.
Next, you have to input the name of your organization and replace the ORG_NAME
.
Then, you need to provide the username and password of the owner of the organization. This username and password will be used as your log-in to the dashboard.
If you are going to explore the features of InfluxDB 2.0 without coding any software to write or query the data from the database, then you are done. You can visit http://localhost:8086, log in, and then explore the features.
But, if you already have an application that can write and/or query data, and you need to get the access token programmatically then you can continue reading.
The token that I mentioned is used to authenticate and authorize a client that is going to write and/or query data. It is available on the dashboard, but we’re not gonna open the dashboard just for that purpose.
You can use the influx
CLI to retrieve the token using the following command:
docker exec influxdb influx auth list
The command will produce the following output:
ID Description Token User Name User ID Permissions07ec335d5c118000 admin's Token TOKEN_REDACTED admin 07ec335d32118000 [read:authorizations write:authorizations read:buckets write:buckets read:dashboards write:dashboards read:orgs write:orgs read:sources write:sources read:tasks write:tasks read:telegrafs write:telegrafs read:users write:users read:variables write:variables read:scrapers write:scrapers read:secrets write:secrets read:labels write:labels read:views write:views read:documents write:documents read:notificationRules write:notificationRules read:notificationEndpoints write:notificationEndpoints read:checks write:checks read:dbrp write:dbrp]
As you can see, there’s a lot of information from the output. For example, we have an account with admin
as the username. This account is assigned as the owner of an organization, that’s why there’s a lot of permissions assigned to that account.
But, we’re here for the token, right? Yes, you can see the token from that output. But you might need to narrow your vision to look for the token.
You may have already noticed that the output is formatted like a table. If we split each line of the output by using space into an array-like type by using awk
, then the token is stored as the 4th entry. But this isn’t always true.
The index of the token depends on the content of the description. By default, the description of the token follows the following format:
USERNAME's Token
In this case, it’s admin’s Token
. If we try visualizing the array, here how it will look like:
["07ec335d5c118000", "admin's", "Token", "REDACTED_TOKEN", "admin", ...]
You can confirm this by using awk
docker exec influxdb influx auth list | awk '/admin/ {print $4 " "}'
The command above will print the token in a line of text that has admin
in it. We have the token now.
Are we done? No, not yet.
At this point, we’ve done everything we need to do (excluding putting all of the commands into a single bash script file), though. But there’s one last thing that I want to highlight by asking this question: What if the username is not admin
?
Of course, you can change the admin
in the command line with something else. But once everything is in a bash script file, you will need to change the file for different users. You don’t want to do this, trust me.
No need to panic, there’s an easy way for this.
You can use an environment variable to provide the username and assign it to awk
variable and use it as a pattern to match the lines in the output ofinflux auth list
.
For example, we’re going to use INFLUXDB_USERNAME
environment variable to store the username, and pass the value to username
variable in awk
.
export INFLUXDB_USERNAME=otheruser
docker exec influxdb influx auth list | awk -v username=$INFLUXDB_USERNAME '$5 ~ username {print $4 " "}'
Note that we’re testing the username
variable against the 5th element in the array which contains the username listed in the output of influx CLI as shown below:
$5 ~ username
So, now you don’t need to change the command line every time you are using a different username. You just need to change the value of the INFLUXDB_USERNAME
environment variable.
Let’s put everything into a bash script file.
#!/bin/bash# create config filedocker run --rm influxdb:2.0.7 influxd print-config > config.yml# create the containerdocker run --name influxdb -d \
-p 8086:8086 \
--volume `pwd`/influxdb2:/var/lib/influxdb2 \
--volume `pwd`/config.yml:/etc/influxdb2/config.yml \
influxdb:2.0.7# configure influxdbdocker exec influxdb influx setup \
--bucket $INFLUXDB_BUCKET \
--org $INFLUXDB_ORG \
--password $INFLUXDB_PASSWORD \
--username $INFLUXDB_USERNAME \
--force# get the tokendocker exec influxdb influx auth list | \
awk -v username=$INFLUXDB_USERNAME '$5 ~ username {print $4 " "}'
Note that we use environment variables to provide the information for influx setup
command.
We’re done! But I think we might miss something here.
What if the database server is not ready when we run the setup command?
How can we handle this case?
Relax, we can use the influx ping
command to check the server status.
until docker exec influxdb influx ping
do
echo "Retrying..."
sleep 5
done
If the server is not ready yet or experiencing a fatal error, the docker exec influxdb influx ping
command will fail, and the commands inside the until
block will be executed. This way, we can try something like waiting for five seconds before retrying.
Let’s put everything together
#!/bin/bash# create config filedocker run --rm influxdb:2.0.7 influxd print-config > config.yml# create the containerdocker run --name influxdb -d \
-p 8086:8086 \
--volume `pwd`/influxdb2:/var/lib/influxdb2 \
--volume `pwd`/config.yml:/etc/influxdb2/config.yml \
influxdb:2.0.7# wait until the database server is readyuntil docker exec influxdb influx ping
do
echo "Retrying..."
sleep 5
done# configure influxdbdocker exec influxdb influx setup \
--bucket $INFLUXDB_BUCKET \
--org $INFLUXDB_ORG \
--password $INFLUXDB_PASSWORD \
--username $INFLUXDB_USERNAME \
--force# get the tokendocker exec influxdb influx auth list | \
awk -v username=$INFLUXDB_USERNAME '$5 ~ username {print $4 " "}'
We’re finally done!
Thank you for reading! Stay safe and healthy!
Update January 24th, 2022
Added missing -d
flag for the docker run
command as pointed out by Ruslan Valiyev.