Add second bucket using influx cli in docker-compose.yml

Hello

I’ve defined successfully an influxdb service (using docker-compose) with an initial bucket (telegraf)

version: "3"
services:
  influxdb:
    container_name: influxdb
    image: influxdb
    volumes:
      - influxdb-storage:/var/lib/influxdb2:rw
    env_file:
      - .env
    entrypoint: ["./entrypoint.sh"]
    ports:
      - ${DOCKER_INFLUXDB_INIT_PORT}:8086

But now I want to create a second bucket. I tried many ways, but I can’t create it.
Please note that I want that bucket to be created as soon as the container starts.
I know that I have to use the influx CLI.

What I’ve tried:

1st way - using the entrypoint file ./entrypoint.sh (see last line)

#!/bin/bash

# Protects script from continuing with an error
set -eu -o pipefail

# Ensures environment variables are set
export DOCKER_INFLUXDB_INIT_MODE=$DOCKER_INFLUXDB_INIT_MODE
....

# Conducts initial InfluxDB using the CLI
influx setup --skip-verify --bucket ${DOCKER_INFLUXDB_INIT_BUCKET} --retention ${DOCKER_INFLUXDB_INIT_RETENTION} --token ${DOCKER_INFLUXDB_INIT_ADMIN_TOKEN} --org ${DOCKER_INFLUXDB_INIT_ORG} --username ${DOCKER_INFLUXDB_INIT_USERNAME} --password ${DOCKER_INFLUXDB_INIT_PASSWORD} --host http://${DOCKER_INFLUXDB_INIT_HOST}:8086 --force
influx bucket create -n results -o org -r 120d

in this way nothing happens, no errors but also no bucket with name “results”

2nd way using “command” in docker-compose.yml
i’ve added

services:
  influxdb:
    ...
    command: >
      sh -c "influx bucket create -n results -o org -r 120d"

In this way i get this error
Error: failed to lookup org with name “org”: Get “http://localhost:8086/api/v2/orgs?org=org”: dial tcp 127.0.0.1:8086: connect: connection refused

What can I be doing wrong?

cheers
Ricardo

I figured it myself

in the documentation of the influxdb docker hub:

Custom Initialization Scripts

The InfluxDB image supports running arbitrary initialization scripts after initial system setup, on both the setup and upgrade paths. Scripts must have extension .sh, must have execute file permissions (chmod +x <yourscript.sh>) and be mounted inside of the /docker-entrypoint-initdb.d directory. When multiple scripts are present, they will be executed in lexical sort order by name.

this worked for me