2022-07-05 15:27:58 +00:00
# ClickHouse Server Docker Image
2016-06-16 10:25:05 +00:00
## What is ClickHouse?
2022-07-06 18:06:53 +00:00
We are the creators of the popular open-source column-oriented DBMS (columnar database management system) for online analytical processing (OLAP) which allows users to generate analytical reports using SQL queries in real-time.
2016-06-16 10:25:05 +00:00
2022-07-06 18:06:53 +00:00
ClickHouse works 100-1000x faster than traditional database management systems, and processes hundreds of millions to over a billion rows and tens of gigabytes of data per server per second. With a widespread user base around the globe, the technology has received praise for its reliability, ease of use, and fault tolerance.
2016-06-16 10:25:05 +00:00
2021-09-22 00:22:57 +00:00
For more information and documentation see https://clickhouse.com/.
2016-06-16 10:25:05 +00:00
## How to use this image
### start server instance
2022-07-03 22:42:54 +00:00
2016-06-16 10:25:05 +00:00
```bash
2022-07-03 22:42:54 +00:00
docker run -d --name some-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server
2016-06-16 10:25:05 +00:00
```
2022-07-03 22:42:54 +00:00
By default, ClickHouse will be accessible only via the Docker network. See the [networking section below ](#networking ).
2020-12-14 15:50:48 +00:00
2022-07-03 22:42:54 +00:00
By default, starting above server instance will be run as the `default` user without a password.
2021-12-28 01:42:49 +00:00
2016-06-16 10:25:05 +00:00
### connect to it from a native client
2022-07-03 22:42:54 +00:00
2016-06-16 10:25:05 +00:00
```bash
2022-07-03 22:42:54 +00:00
docker run -it --rm --link some-clickhouse-server:clickhouse-server --entrypoint clickhouse-client clickhouse/clickhouse-server --host clickhouse-server
2022-06-13 10:03:11 +00:00
# OR
2022-07-03 22:42:54 +00:00
docker exec -it some-clickhouse-server clickhouse-client
2016-06-16 10:25:05 +00:00
```
2022-07-03 22:42:54 +00:00
More information about the [ClickHouse client ](https://clickhouse.com/docs/en/interfaces/cli/ ).
2016-06-16 10:25:05 +00:00
2020-12-02 15:26:16 +00:00
### connect to it using curl
```bash
2020-12-14 15:50:48 +00:00
echo "SELECT 'Hello, ClickHouse!'" | docker run -i --rm --link some-clickhouse-server:clickhouse-server curlimages/curl 'http://clickhouse-server:8123/?query=' -s --data-binary @-
2020-12-02 15:26:16 +00:00
```
2022-07-03 22:42:54 +00:00
2021-09-22 00:22:57 +00:00
More information about [ClickHouse HTTP Interface ](https://clickhouse.com/docs/en/interfaces/http/ ).
2020-12-02 15:26:16 +00:00
2022-07-03 22:42:54 +00:00
### stopping / removing the container
2020-12-14 15:50:48 +00:00
```bash
2022-07-03 22:42:54 +00:00
docker stop some-clickhouse-server
docker rm some-clickhouse-server
2020-12-14 15:50:48 +00:00
```
### networking
2022-07-03 22:42:54 +00:00
You can expose your ClickHouse running in docker by [mapping a particular port ](https://docs.docker.com/config/containers/container-networking/ ) from inside the container using host ports:
2020-12-14 15:50:48 +00:00
```bash
2022-07-03 22:42:54 +00:00
docker run -d -p 18123:8123 -p19000:9000 --name some-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server
echo 'SELECT version()' | curl 'http://localhost:18123/' --data-binary @-
2022-07-06 18:06:53 +00:00
```
```response
22.6.3.35
2020-12-14 15:50:48 +00:00
```
2022-07-03 22:42:54 +00:00
or by allowing the container to use [host ports directly ](https://docs.docker.com/network/host/ ) using `--network=host` (also allows archiving better network performance):
2020-12-14 15:50:48 +00:00
```bash
2022-07-03 22:42:54 +00:00
docker run -d --network=host --name some-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server
echo 'SELECT version()' | curl 'http://localhost:8123/' --data-binary @-
2022-07-06 18:06:53 +00:00
```
```response
22.6.3.35
2020-12-14 15:50:48 +00:00
```
2021-02-05 16:39:05 +00:00
### Volumes
2020-12-14 15:50:48 +00:00
2022-07-05 15:39:38 +00:00
Typically you may want to mount the following folders inside your container to achieve persistency:
2020-12-14 15:50:48 +00:00
* `/var/lib/clickhouse/` - main folder where ClickHouse stores the data
2022-07-03 22:42:54 +00:00
* `/var/log/clickhouse-server/` - logs
2020-12-14 15:50:48 +00:00
```bash
2022-07-03 22:42:54 +00:00
docker run -d \
-v $(realpath ./ch_data):/var/lib/clickhouse/ \
-v $(realpath ./ch_logs):/var/log/clickhouse-server/ \
--name some-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server
2020-12-14 15:50:48 +00:00
```
You may also want to mount:
* `/etc/clickhouse-server/config.d/*.xml` - files with server configuration adjustmenets
2022-07-03 22:42:54 +00:00
* `/etc/clickhouse-server/users.d/*.xml` - files with user settings adjustmenets
2020-12-14 15:50:48 +00:00
* `/docker-entrypoint-initdb.d/` - folder with database initialization scripts (see below).
2021-02-05 16:39:05 +00:00
### Linux capabilities
2020-12-14 15:50:48 +00:00
2022-07-03 22:42:54 +00:00
ClickHouse has some advanced functionality, which requires enabling several [Linux capabilities ](https://man7.org/linux/man-pages/man7/capabilities.7.html ).
2020-12-14 15:50:48 +00:00
2022-07-05 15:39:38 +00:00
These are optional and can be enabled using the following [docker command-line arguments ](https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities ):
2020-12-14 15:50:48 +00:00
```bash
2022-07-03 22:42:54 +00:00
docker run -d \
--cap-add=SYS_NICE --cap-add=NET_ADMIN --cap-add=IPC_LOCK \
--name some-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server
2020-12-14 15:50:48 +00:00
```
2016-06-16 10:25:05 +00:00
## Configuration
2022-07-03 22:42:54 +00:00
The container exposes port 8123 for the [HTTP interface ](https://clickhouse.com/docs/en/interfaces/http_interface/ ) and port 9000 for the [native client ](https://clickhouse.com/docs/en/interfaces/tcp/ ).
2016-06-16 10:25:05 +00:00
2022-07-03 22:42:54 +00:00
ClickHouse configuration is represented with a file "config.xml" ([documentation](https://clickhouse.com/docs/en/operations/configuration_files/))
2016-06-16 10:25:05 +00:00
2020-01-26 16:14:13 +00:00
### Start server instance with custom configuration
2022-07-03 22:42:54 +00:00
2016-06-16 10:25:05 +00:00
```bash
2022-07-03 22:42:54 +00:00
docker run -d --name some-clickhouse-server --ulimit nofile=262144:262144 -v /path/to/your/config.xml:/etc/clickhouse-server/config.xml clickhouse/clickhouse-server
2016-06-16 10:25:05 +00:00
```
2022-07-05 14:20:55 +00:00
### Start server as a custom user
2022-07-03 22:42:54 +00:00
```bash
2019-03-21 15:10:47 +00:00
# $(pwd)/data/clickhouse should exist and be owned by current user
2022-07-03 22:42:54 +00:00
docker run --rm --user ${UID}:${GID} --name some-clickhouse-server --ulimit nofile=262144:262144 -v "$(pwd)/logs/clickhouse:/var/log/clickhouse-server" -v "$(pwd)/data/clickhouse:/var/lib/clickhouse" clickhouse/clickhouse-server
2019-03-21 15:10:47 +00:00
```
2022-07-03 22:42:54 +00:00
2022-07-05 15:39:38 +00:00
When you use the image with local directories mounted, you probably want to specify the user to maintain the proper file ownership. Use the `--user` argument and mount `/var/lib/clickhouse` and `/var/log/clickhouse-server` inside the container. Otherwise, the image will complain and not start.
2019-03-21 15:10:47 +00:00
2020-01-26 16:14:13 +00:00
### Start server from root (useful in case of userns enabled)
2022-07-03 22:42:54 +00:00
```bash
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" clickhouse/clickhouse-server
2020-01-26 16:14:13 +00:00
```
2020-05-03 18:15:18 +00:00
### How to create default database and user on starting
2022-07-03 22:42:54 +00:00
Sometimes you may want to create a user (user named `default` is used by default) and database on image start. You can do it using environment variables `CLICKHOUSE_DB` , `CLICKHOUSE_USER` , `CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT` and `CLICKHOUSE_PASSWORD` :
2020-05-03 18:15:18 +00:00
2022-07-03 22:42:54 +00:00
```bash
docker run --rm -e CLICKHOUSE_DB=my_database -e CLICKHOUSE_USER=username -e CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1 -e CLICKHOUSE_PASSWORD=password -p 9000:9000/tcp clickhouse/clickhouse-server
2020-05-03 18:15:18 +00:00
```
2018-11-28 19:55:34 +00:00
## How to extend this image
2022-07-05 15:39:38 +00:00
To perform 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.
2022-07-03 22:42:54 +00:00
Also, you can provide environment variables `CLICKHOUSE_USER` & `CLICKHOUSE_PASSWORD` that will be used for clickhouse-client during initialization.
2018-11-28 19:55:34 +00:00
For example, to add an additional user and database, add the following to `/docker-entrypoint-initdb.d/init-db.sh` :
```bash
#!/bin/bash
set -e
clickhouse client -n < < -EOSQL
2022-07-03 22:42:54 +00:00
CREATE DATABASE docker;
CREATE TABLE docker.docker (x Int32) ENGINE = Log;
2018-11-28 19:55:34 +00:00
EOSQL
```
2016-06-16 10:25:05 +00:00
## License
2019-09-23 16:18:19 +00:00
View [license information ](https://github.com/ClickHouse/ClickHouse/blob/master/LICENSE ) for the software contained in this image.
2022-07-05 15:39:38 +00:00