If you use the official InfluxDB docker image to create a new InfluxDB instance, you may have to execute some initialization commands like creating a database or a retention policy.
Manually executing initialization commands could be very annoying, we can build a new InfluxDB image that can automatically execute initialization commands when a docker instance is created.
Create a new file called setup_influxdb.sh
. Its content is:
# replace "db1" with your real database name
influx -execute "CREATE DATABASE db1"
# retention policy example: only save data of recent 30 days
influx -execute "CREATE RETENTION POLICY one_month ON db1 DURATION 30d REPLICATION 1 DEFAULT"
Create a new file called Dockerfile
. Its content is:
# based on the official image
FROM influxdb
# copy the initialization script to /docker-entrypoint-initdb.d/
COPY setup_influxdb.sh /docker-entrypoint-initdb.d/setup_influxdb.sh
Scripts in /docker-entrypoint-initdb.d/
will be executed when a new container is created.
Make sure that setup_influxdb.sh
and Dockerfile
are in the same directory. cd
to this directory and execute:
# build a new image, do not omit the trailing dot
docker build -t new_influxdb_image .
# create a new instance and setup_influxdb.sh will be automatically executed
docker run -d new_influxdb_image