influxdata / influxdata-docker

Official docker images for the influxdata stack
327 stars 248 forks source link

More than one database creation with env.variables #251

Open lestatk0 opened 6 years ago

lestatk0 commented 6 years ago

Could I create more than one database with the help of env.variables. Ex.:

docker run --rm -e INFLUXDB_DB=db0  -e INFLUXDB_DB=db1 influxdb /init-influxdb.sh

In this case only the last one will be created, how can I create both of them

psreekala commented 6 years ago

Would like to create multiple databases..did this work?

lestatk0 commented 6 years ago

seems that the question is still opened

danieldides commented 5 years ago

The value of INFLUXDB_DB is just passed off to this exec command: INFLUX_CMD="influx -host 127.0.0.1 -port $INFLUXDB_INIT_PORT -execute " with the value of CREATE_DB_QUERY="CREATE DATABASE $INFLUXDB_DB", so a quick (hacky) work around to allow you to create multiple databases is to inject additional queries into this command via the environment variable. For instance:

version: '3'

services:
  influx:
    image: influxdb:1.6
    ports:
      - "8086:8086"
    environment:
      INFLUXDB_DB: one; CREATE DATABASE two;

has allowed me to create both a "one" database and a "two" database at container start time.

This is obviously a terrible hack, but it works in a pinch 😉

The relevant source for these commands I referenced is here: https://github.com/influxdata/influxdata-docker/blob/d76a6a184dec6a82978964882641a743adb76989/influxdb/1.7/init-influxd

zimmertr commented 5 years ago

Hm, this does not appear to be working anymore with the latest image. At least not with this declaration in my Kubernetes deployment manifest:

                  env:
                      - name: INFLUXDB_DB
                        value: "telegraf; CREATE DATABASE proxmox; CREATE DATABASE unifi"

telegraf and proxmox both exist. Telegraf was originally created when the container was first spawned with only that value being set. proxmox was automatically created when I configured the influxdb.conf to listen for udp connections on the proxmox database. But modifying my environment variable to the above configuration and recreating the deployment did not successfully create the unifi database.

zimmertr commented 5 years ago

Figured out a way actually. With Kubernetes at least. By providing a lifecycle policy you can execute a command on the pod after it has been launched. For example:

                  lifecycle:
                      postStart:
                          exec:
                              command:
                                  - "/bin/sh"
                                  - "-c"
                                  - >
                                      until curl -sL -I localhost:8086/ping > /dev/null; do
                                          echo 'InfluxDB is not yet ready.'
                                          sleep 1
                                      done

                                      echo 'InfluxDB is now ready.'
                                      influx -password '${INFLUXDB_ADMIN_PASSWORD}' -username '${INFLUXDB_ADMIN_USER}' -execute '{{ influxdb_cmds }}'

Where the INFLUXDB_ADMIN_PASSWORD and INFLUXDB_ADMIN_USER variables are configured in the env section of my Kubernetest manifest and the influxdb_cmds variable is injected via Ansible as a jinja template.

influxdb_cmds: 'CREATE DATABASE proxmox; CREATE DATABASE unifi'

zimmertr commented 5 years ago

Actually, never mind. There is a different official way to accomplish this. The docker container supports dropping .sh files to configure the database server. Search the docs here for /docker-entrypoint-initdb.d

https://hub.docker.com/_/influxdb

NutCr4cker12 commented 5 years ago

Was about to comment the same as @zimmertr . And for newbies with docker like me, add line docker-compose:

Volumes:
    -./influx_init.iql:/docker-entrypoint-initdb.d/influx_init.iql/

influx_init.iql:

CREATE DATABASE "testDB"
CREATE DATABASE "productionDB"

This way you can initialize as many databases as you wan't and one could add users with privileges attached to them.

NoxInmortus commented 5 years ago

That could be great, i'm also interrested by this feature. Gonna try NutCr4cker12 stuff.

zimmertr commented 5 years ago

That's what I'm doing, works well to create 5 more databases on launch here.

psio3123 commented 4 years ago

Two additions from my side:

  1. I think there is one slash to much in the volumes section of docker-compose.yml of NutCr4cker12 :
Volumes:
    -./influx_init.iql:/docker-entrypoint-initdb.d/influx_init.iql
  1. The influx_init.iql is only executed if no databases exists yet, not every time the container restarts. So for adding a database to an existing container you either have to delete the files in folder /var/lib/influxdb to start over empty or rerun the init script from outside the container with a command like:
docker-compose exec influx /usr/bin/influx \
    -import -path /docker-entrypoint-initdb.d/influx_init.iql
malith1992 commented 3 years ago

Two additions from my side:

1. I think there is one slash to much in the volumes section of docker-compose.yml of NutCr4cker12 :
Volumes:
    -./influx_init.iql:/docker-entrypoint-initdb.d/influx_init.iql
1. The influx_init.iql is only executed if no databases exists yet, not every time the container restarts. So for adding a database to an existing container you either have to delete the files in folder /var/lib/influxdb to start over empty or rerun the init script from outside the container with a command like:
docker-compose exec influx /usr/bin/influx \
    -import -path /docker-entrypoint-initdb.d/influx_init.iql

Isn't there a solution yet to add a database to an existing container without deleting the files in folder /var/lib/influxdb ?