From 7dcb74ab7c6df15ed96ad62d262ddac0e3ffa27e Mon Sep 17 00:00:00 2001 From: Paramtamtam Date: Sun, 3 May 2020 23:15:18 +0500 Subject: [PATCH] Default user and database creation on image starting added --- docker/server/Dockerfile | 2 ++ docker/server/README.md | 8 ++++++++ docker/server/init-defaults.sh | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100755 docker/server/init-defaults.sh diff --git a/docker/server/Dockerfile b/docker/server/Dockerfile index faa5a593eb2..44bac53b879 100644 --- a/docker/server/Dockerfile +++ b/docker/server/Dockerfile @@ -39,9 +39,11 @@ RUN mkdir /docker-entrypoint-initdb.d COPY docker_related_config.xml /etc/clickhouse-server/config.d/ COPY entrypoint.sh /entrypoint.sh +COPY init-defaults.sh /docker-entrypoint-initdb.d/init-defaults.sh RUN chmod +x \ /entrypoint.sh \ + /docker-entrypoint-initdb.d/init-defaults.sh \ /bin/gosu EXPOSE 9000 8123 9009 diff --git a/docker/server/README.md b/docker/server/README.md index b36bbe40dc7..892fe2d7035 100644 --- a/docker/server/README.md +++ b/docker/server/README.md @@ -45,6 +45,14 @@ When you use the image with mounting local directories inside you probably would $ docker run --rm -e CLICKHOUSE_UID=0 -e CLICKHOUSE_GID=0 --name clickhouse-server-userns -v "$(pwd)/logs/clickhouse:/var/log/clickhouse-server" -v "$(pwd)/data/clickhouse:/var/lib/clickhouse" yandex/clickhouse-server ``` +### How to create default database and user on starting + +Sometimes you may want to create default user and database on image starting. You can do it using environment variables `CLICKHOUSE_DEFAULT_DB`, `CLICKHOUSE_DEFAULT_USER` and `CLICKHOUSE_DEFAULT_PASSWORD`: + +``` +$ docker run --rm -e CLICKHOUSE_DEFAULT_DB=my_database -e CLICKHOUSE_DEFAULT_USER=username -e CLICKHOUSE_DEFAULT_PASSWORD=password -p 9000:9000/tcp yandex/clickhouse-server +``` + ## How to extend this image If you would like to do additional initialization in an image derived from this one, add one or more `*.sql`, `*.sql.gz`, or `*.sh` scripts under `/docker-entrypoint-initdb.d`. After the entrypoint calls `initdb` it will run any `*.sql` files, run any executable `*.sh` scripts, and source any non-executable `*.sh` scripts found in that directory to do further initialization before starting the service. diff --git a/docker/server/init-defaults.sh b/docker/server/init-defaults.sh new file mode 100755 index 00000000000..6c2005bf67a --- /dev/null +++ b/docker/server/init-defaults.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env sh + +# define defaults +CLICKHOUSE_DEFAULT_DB="${CLICKHOUSE_DEFAULT_DB:-}"; +CLICKHOUSE_DEFAULT_USER="${CLICKHOUSE_DEFAULT_USER:-}"; +CLICKHOUSE_DEFAULT_PASSWORD="${CLICKHOUSE_DEFAULT_PASSWORD:-secret}"; + +# if default user is defined - create it +if [ -n "$CLICKHOUSE_DEFAULT_USER" ]; then + cat <> /etc/clickhouse-server/users.d/default-user.xml + + + + <${CLICKHOUSE_DEFAULT_USER}> + default + + ::/0 + + ${CLICKHOUSE_DEFAULT_PASSWORD} + default + + + +EOT +fi + +# create default database, if defined +if [ -n "$CLICKHOUSE_DEFAULT_DB" ]; then + clickhouse-client --query "CREATE DATABASE IF NOT EXISTS ${CLICKHOUSE_DEFAULT_DB}"; +fi