Default user and database creation on image starting added

This commit is contained in:
Paramtamtam 2020-05-03 23:15:18 +05:00
parent 615104c820
commit 7dcb74ab7c
No known key found for this signature in database
GPG Key ID: D261715E1D37D6C8
3 changed files with 40 additions and 0 deletions

View File

@ -39,9 +39,11 @@ RUN mkdir /docker-entrypoint-initdb.d
COPY docker_related_config.xml /etc/clickhouse-server/config.d/ COPY docker_related_config.xml /etc/clickhouse-server/config.d/
COPY entrypoint.sh /entrypoint.sh COPY entrypoint.sh /entrypoint.sh
COPY init-defaults.sh /docker-entrypoint-initdb.d/init-defaults.sh
RUN chmod +x \ RUN chmod +x \
/entrypoint.sh \ /entrypoint.sh \
/docker-entrypoint-initdb.d/init-defaults.sh \
/bin/gosu /bin/gosu
EXPOSE 9000 8123 9009 EXPOSE 9000 8123 9009

View File

@ -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 $ 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 ## 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. 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.

30
docker/server/init-defaults.sh Executable file
View File

@ -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 <<EOT >> /etc/clickhouse-server/users.d/default-user.xml
<yandex>
<!-- Docs: <https://clickhouse.tech/docs/en/operations/settings/settings_users/> -->
<users>
<${CLICKHOUSE_DEFAULT_USER}>
<profile>default</profile>
<networks>
<ip>::/0</ip>
</networks>
<password>${CLICKHOUSE_DEFAULT_PASSWORD}</password>
<quota>default</quota>
</${CLICKHOUSE_DEFAULT_USER}>
</users>
</yandex>
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