InfluxDB Database in Docker Container
InfluxDB is a powerful time-series database that is often used for monitoring and analytics. In this article, we will explore how to run InfluxDB in a Docker container and connect to it using JavaScript and NodeJS to read and write data.
Running InfluxDB in Docker Container
To run InfluxDB in a Docker container, you can use the following command:
docker run -d -p 8086:8086 -v $PWD:/var/lib/influxdb --name influxdb influxdb
This command will start an InfluxDB container and map port 8086 on the host to port 8086 on the container. It will also create a volume for the data so that it persists even if the container is stopped and started again.
Connecting with JavaScript and NodeJS
Once InfluxDB is running in a Docker container, you can connect to it using JavaScript and NodeJS to read and write data. First, you will need to install the influx
package using npm:
npm install influx
After installing the package, you can use the following code to connect to InfluxDB and write data:
const Influx = require('influx'); const client = new Influx.InfluxDB({ host: 'localhost', database: 'mydb', schema: [ { measurement: 'my_measurement', fields: { value: Influx.FieldType.INTEGER }, tags: [ 'my_tag' ] } ] }); client.writePoints([ { measurement: 'my_measurement', tags: { my_tag: 'my_value' }, fields: { value: 10 } } ]);
This code creates a new InfluxDB client and writes a data point to the mydb
database. You can also use similar code to read data from InfluxDB using the query()
method.
Conclusion
InfluxDB is a powerful database for storing and querying time-series data. By running InfluxDB in a Docker container and connecting to it using JavaScript and NodeJS, you can easily read and write data to the database from your applications. This makes it a great choice for monitoring and analytics use cases.
Great tutorial, Was able to get things done quickly and efficiently. Very understandable. Thank you.
You said Environmental Variables at 5:52 🙂
Thanks! It was very well explained