从 docker 撰写 YAML 文件创建两个 influxdb

Create two influxdb from docker compose YAML file

我想在 运行 docker-compose up 命令中创建两个数据库。 以下是我试过但没有用的解决方案

version: '3.2'
services:
  influxdb:
    image: influxdb
    env_file: configuration.env
    ports:
      - '8086:8086'
    volumes:
      - 'influxdb:/var/lib/influxdb'
    environment:
      - INFLUXDB_DB=testDB
    command: sh -c Sample.sh

我遇到的错误 influxdb_1_170f324e55e3 | sh: 1: Sample.sh: not found 在 Sample.sh 我有 curl 命令,当独立执行时创建另一个数据库。

你不应该覆盖 influx 数据库容器的 运行 命令,如果你覆盖 CMD 那么你还需要启动 influxd 进程。所以最好使用 init.db 脚本并在 运行 时间填充脚本。

初始化文件

If the Docker image finds any files with the extensions .sh or .iql inside of the /docker-entrypoint-initdb.d folder, it will execute them. The order they are executed in is determined by the shell. This is usually alphabetical order.

手动初始化数据库

To manually initialize the database and exit, the /init-influxdb.sh script can be used directly. It takes the same parameters as the influxd run command. As an example:

$ docker run --rm \
      -e INFLUXDB_DB=db0 -e INFLUXDB_ADMIN_ENABLED=true \
      -e INFLUXDB_ADMIN_USER=admin -e INFLUXDB_ADMIN_PASSWORD=supersecretpassword \
      -e INFLUXDB_USER=telegraf -e INFLUXDB_USER_PASSWORD=secretpassword \
      -v $PWD:/var/lib/influxdb \
      influxdb /init-influxdb.sh

当您从官方页面查看 influx DB offical image and you can explore the database initialization 的入口点时。

因此您需要将脚本放在 .iql.sh 中并将该位置安装在 docker-compose.

    volumes:
      - 'influxdb:/var/lib/influxdb'
      - init.db/init.iql:/docker-entrypoint-initdb.d/

最好使用 InfluxQL 创建,将以下行添加到您的脚本并另存为 init.iql

CREATE DATABASE "NOAA_water_database"

您还需要更新 Dockerfile。

FROM influxdb
COPY init.iql  /docker-entrypoint-initdb.d/

现在您可以从 CMD 中删除命令,它应该会创建 DB

version: '3.2'
services:
  influxdb:
    build: .
    env_file: configuration.env
    ports:
      - '8086:8086'
    volumes:
      - 'influxdb:/var/lib/influxdb'
    environment:
      - INFLUXDB_DB=testDB