Search code examples
dockerinfluxdbinfluxdb-2

Initialize influxdb2 buckets with entrypoint script


I am working on a deployment of an influxdb for storing of real-time data. I have been using 1.8.4 for some time now and recently decided to update to v2.

My docker-compose.yml file looks something like this:

influxdb:
    image: influxdb:2.0.4-alpine
    ports:
      - "8086:8086"
    volumes:
      - ./influxdb/data:/var/lib/influxdb2
      - ./influxdb/config:/etc/influxdb2
      - ./influxdb/scripts:/docker-entrypoint-initdb.d
    environment:
      - DOCKER_INFLUXDB_INIT_MODE=setup
      - DOCKER_INFLUXDB_INIT_USERNAME=${INFLUXDB_USR}
      - DOCKER_INFLUXDB_INIT_PASSWORD=${INFLUXDB_PWD}
      - DOCKER_INFLUXDB_INIT_ORG=Org0
      - DOCKER_INFLUXDB_INIT_BUCKET=bucket0

which indeed creates an initial bucket named bucket0.

That said I would like to have a script in order to initialize further buckets, write some data or add auth. In my ./influxdb/scripts directory I have a script init.sh which would look like this:

#!/bin/bash
set -e
influx bucket create -n bucket1 -d "Bucket 1"

Then I would continue to use influx write and influx auth and all the nice stuff that influx cli provides, but the script above seems to not have any effect creating the bucket.

I have also tried to use the -c /etc/influxdb2/influx-configs option or the --token to no avail.

Doing a docker exec -it <container> /bin/bash and then executing the exact same command created the bucket as expected.

Any ideas ? Thanks a lot !


Solution

  • I managed to run the following script at influxdb startup :

    #!/bin/sh
    
    set -e
    influx bucket create -n first -r 7d
    influx bucket create -n second -r 7d
    
    • even if the influxdb docker image supports bash, you should prefer writing sh scripts for alpine.
    • is your script executable ? (chmod +x init.sh)
    • the init.sh script is executed only the first time influxdb is launched. Once initialized, you must clean your docker volumes associated with this image. If you use docker-compose : docker-compose down -v

    Best regards