Bekjan Juma
Bekjan Juma

Reputation: 563

Creating default user for influxdb 2.0 in docker-compose

In previous versions of influxdb we could create an admin user by using the environment variables

influxdb:
    ...
    environment:
        INFLUXDB_HOSTNAME: "${INFLUXDB_HOSTNAME}"
        INFLUXDB_USERNAME: "${INFLUXDB_USERNAME}"
        INFLUXDB_PASSWORD: "${INFLUXDB_PASSWORD}"
    ...

But in version 2.0 these env. variables are removed

What I want to achieve is, when I run the docker-compose up my_influx_db

It should create a default admin user, if it doesn't exist

What I tried so far is:

my_influx_db:
  image: quay.io/influxdb/influxdb:v2.0.3
  hostname: my_influx_db
  container_name: my_influx_db
  ports:
    - 8086:8086
  command: /bin/sh -c "exec influxd && sleep 10 && influx setup -o test_org -b test_bucket -u user1 -p testpassword -f"

docker-compose up my_influx_db starts the influxdb, but doesn't run the setup script after 10 secs

What is the right way to create default admin user?

Edit: I managed to run it with the following configuration, but is it the right way to do it?

command: /bin/sh -c "(sleep 10 && echo setting up user && influx setup -o test_org -b test-bucket -u influxdb -p influxdb -f) & influxd"

Upvotes: 9

Views: 15525

Answers (2)

Bekjan Juma
Bekjan Juma

Reputation: 563

The latest version: 2.0.7 of influxdb made it easier providing the ability to create an initial admin username and password

https://docs.influxdata.com/influxdb/v2.0/upgrade/v1-to-v2/docker/#influxdb-2x-initialization-credentials

environments:
  - DOCKER_INFLUXDB_INIT_MODE=setup
  - DOCKER_INFLUXDB_INIT_USERNAME=yourInfluxAccount
  - DOCKER_INFLUXDB_INIT_PASSWORD=yourInfluxPassword
  - DOCKER_INFLUXDB_INIT_ORG=chooseOrgName
  - DOCKER_INFLUXDB_INIT_BUCKET=chooseBucketName
  - DOCKER_INFLUXDB_INIT_RETENTION=1w
  - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=randomTokenValue

Upvotes: 14

Amerousful
Amerousful

Reputation: 2545

You can up another container which will try to create a user. So it's my docker-compose.yml:

version: '3'
services:
  influxdb:
    image: quay.io/influxdb/influxdb:v2.0.4
    container_name: influxdb
    volumes:
      - ./influxdbv2:/root/.influxdbv2
    ports:
      - "8086:8086"
  influxdb_cli:
    links:
      - influxdb
    image: quay.io/influxdb/influxdb:v2.0.4
    entrypoint: influx setup --bucket test_bucket -t test_token -o test_org --username=test_username --password=test_password --host=http://influxdb:8086 -f

    restart: on-failure:20
    depends_on:
      - influxdb
volumes:
  influxdbv2:

Upvotes: 8

Related Questions