composebuilder

Docker Compose: ClickHouse 25.8 Analytics Database

Docker Compose stack for ClickHouse 25.8 with a dedicated user, a healthcheck, raised file limits and HTTP and native ports on localhost for analytical queries.

When to use this stack

ClickHouse is a column-oriented analytical database. It scans and aggregates large volumes of event, log and metric data very quickly, using compressed columns and a MergeTree storage engine. It is the right tool for dashboards that group millions of rows, for product analytics and for log exploration. This stack runs ClickHouse 25.8 with a dedicated user, a database created on first start, a healthcheck that runs a query through clickhouse-client, and raised file descriptor limits, which the server needs under load.

Use this stack when you have append-heavy data and need answers to aggregate questions in seconds, or when you want a separate analytics store next to an operational database. Do not use it for workloads that depend on frequent single-row updates, strict multi-statement transactions or many small writes from many clients. ClickHouse also needs more RAM and disk throughput than a typical web database, so size the host before you commit. Both the HTTP port and the native port are bound to 127.0.0.1, so remote tools must reach them through an SSH tunnel or a proxy.

docker-compose.yml

services:
  clickhouse:
    image: clickhouse/clickhouse-server:25.8
    restart: unless-stopped
    environment:
      CLICKHOUSE_DB: ${CLICKHOUSE_DB}
      CLICKHOUSE_USER: ${CLICKHOUSE_USER}
      CLICKHOUSE_PASSWORD: ${CLICKHOUSE_PASSWORD}
      CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: "1"
    ulimits:
      nofile:
        soft: 262144
        hard: 262144
    volumes:
      - clickhouse_data:/var/lib/clickhouse
    ports:
      - "127.0.0.1:8123:8123"
      - "127.0.0.1:9000:9000"
    healthcheck:
      test: ["CMD-SHELL", "clickhouse-client --user $$CLICKHOUSE_USER --password $$CLICKHOUSE_PASSWORD --query 'SELECT 1' || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s

volumes:
  clickhouse_data:

.env

.env
# Database created on the first start
CLICKHOUSE_DB=analytics

# Dedicated account used by applications and tools
CLICKHOUSE_USER=analyst
CLICKHOUSE_PASSWORD=CHANGE_ME_CLICKHOUSE_PASSWORD

Start and verify

Source the variables for the checks below. Compose reads .env on its own. The first start creates the user and the database, so the healthcheck may report starting for a short time before it turns healthy.

Shell
set -a; . ./.env; set +a
docker compose config --quiet
docker compose up -d
docker compose ps
docker compose logs --tail=40 clickhouse
curl -s http://127.0.0.1:8123/ping
echo "CREATE TABLE IF NOT EXISTS events (ts DateTime, name String) ENGINE = MergeTree ORDER BY ts" | curl -s -u "$CLICKHOUSE_USER:$CLICKHOUSE_PASSWORD" "http://127.0.0.1:8123/?database=$CLICKHOUSE_DB" --data-binary @-
echo "INSERT INTO events VALUES (now(), 'signup')" | curl -s -u "$CLICKHOUSE_USER:$CLICKHOUSE_PASSWORD" "http://127.0.0.1:8123/?database=$CLICKHOUSE_DB" --data-binary @-
echo "SELECT count() FROM events" | curl -s -u "$CLICKHOUSE_USER:$CLICKHOUSE_PASSWORD" "http://127.0.0.1:8123/?database=$CLICKHOUSE_DB" --data-binary @-

The ping should return Ok., and the final query should return the number of rows inserted so far.

Production checklist

  • Replace the CHANGE_ME password with a long random value, and use the dedicated account instead of the default user.
  • Keep ports 8123 and 9000 on localhost, and place an authenticated proxy in front when remote dashboards need access.
  • Create read-only accounts with GRANT SELECT for dashboards and BI tools, and keep the admin account for schema changes.
  • Batch inserts into chunks of thousands of rows, and avoid one INSERT per event.
  • Choose ORDER BY keys around the columns you filter on most, and keep PARTITION BY coarse, for example by month.
  • Set a memory limit on the container and per-query memory caps, so a heavy query cannot take down the host.
  • Configure a backup destination with the BACKUP statement, and test restores into a separate container.
  • Use fast SSD storage for the data volume, because merges and scans are I/O heavy.

Common problems

  • Symptom: inserts fail with a message about too many parts. Cause: the application sends many small inserts, which create parts faster than the background merges can combine them. Fix: batch the rows on the client, or enable async_insert for clients that cannot batch.

  • Symptom: the default user cannot log in over the network. Cause: the image disables network access for the default user unless a user is configured. Fix: connect with CLICKHOUSE_USER and CLICKHOUSE_PASSWORD from .env.

  • Symptom: the logs show too many open files. Cause: the default file descriptor limit is too low for the server under load. Fix: keep the nofile ulimit block in the compose file, and restart the container after changing it.

  • Symptom: the container is killed by the operating system during a large GROUP BY. Cause: the query needs more memory than the container limit allows. Fix: raise the memory limit, set max_memory_usage per query, and allow external aggregation to spill to disk for very large groups.

Variations

  • Replicated cluster: run several ClickHouse nodes together with ClickHouse Keeper for coordination, which is the standard path to replication and high availability.
  • BI dashboards: connect Metabase, Grafana or another BI tool with a read-only account over the HTTP port, from a container on the same network.
  • Worker ingestion: let a small worker container collect events from a queue and send them to the HTTP interface in batches.

Questions

Why does ClickHouse reject the default user?

The official image disables network access for the default user unless you set CLICKHOUSE_USER, CLICKHOUSE_PASSWORD or the access management flag. This file creates a dedicated user, so connect with that account.

Why is ClickHouse slow when I insert one row at a time?

Every INSERT creates a new data part, and many small parts slow down merges and reads. Send inserts in batches of thousands of rows, or enable asynchronous inserts when the client cannot batch.

Is ClickHouse a good replacement for PostgreSQL?

It is built for large scans, aggregations and append-heavy event data, not for frequent small updates and transactions. Keep your transactional data in PostgreSQL and send analytical copies to ClickHouse.

How much memory does this container need?

It depends on the size of the queries. Start with at least 4 GB for light workloads, watch memory during heavy GROUP BY queries, and set per-query limits so one query cannot starve the host.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • Elasticsearch 8 + Kibana Search Stack

    Docker Compose stack for Elasticsearch 8.19 and Kibana 8.19 with security enabled, a persistent volume, a healthcheck and localhost-only ports.

    • elasticsearch
    • kibana
    Difficulty: Advanced
  • FastAPI + MongoDB 7 + Mongo Express

    Docker Compose stack for a Python FastAPI API on MongoDB 7 with authentication, a healthcheck, persistent storage and a protected Mongo Express UI on localhost.

    • api
    • mongodb
    • mongo-express
    Difficulty: Intermediate
  • MariaDB 11.4 LTS + Adminer 5

    Docker Compose stack with MariaDB 11.4 LTS, a healthcheck, a named volume and Adminer 5 on localhost. A lightweight admin UI for MySQL-compatible databases.

    • mariadb
    • adminer
    Difficulty: Beginner
  • Meilisearch v1 + Python FastAPI Search API

    Docker Compose stack for Meilisearch v1 in production mode with a master key, a healthcheck and persistent data, plus a FastAPI search API on localhost.

    • meilisearch
    • api
    Difficulty: Intermediate

Browse more Databases & Search stacks or the full library.