forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-init.cpp
More file actions
1078 lines (959 loc) · 39 KB
/
Copy pathdocker-init.cpp
File metadata and controls
1078 lines (959 loc) · 39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// clickhouse docker-init — Docker entrypoint for distroless ClickHouse images.
/// Replaces entrypoint.sh in shell-free environments (no bash, no coreutils).
///
/// Usage:
/// clickhouse docker-init [--keeper] [-- <extra-server-args>...]
///
/// Environment variables (same as entrypoint.sh):
/// CLICKHOUSE_CONFIG, CLICKHOUSE_RUN_AS_ROOT, CLICKHOUSE_DO_NOT_CHOWN,
/// CLICKHOUSE_UID, CLICKHOUSE_GID, CLICKHOUSE_USER, CLICKHOUSE_PASSWORD,
/// CLICKHOUSE_PASSWORD_FILE, CLICKHOUSE_DB, CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT,
/// CLICKHOUSE_SKIP_USER_SETUP, CLICKHOUSE_ALWAYS_RUN_INITDB_SCRIPTS,
/// CLICKHOUSE_INIT_TIMEOUT, CLICKHOUSE_WATCHDOG_ENABLE, KEEPER_CONFIG
#include <algorithm>
#include <cerrno>
#include <csignal>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
namespace fs = std::filesystem;
namespace
{
/// Path to the clickhouse multi-tool binary, derived from argv[0].
/// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::string g_clickhouse_binary;
/// Set by the SIGTERM/SIGINT handler during init to request graceful shutdown.
/// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
volatile sig_atomic_t g_shutdown_requested = 0;
/// PID of the temporary init server, so the signal handler can forward SIGTERM.
/// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
volatile pid_t g_init_server_pid = 0;
void shutdownHandler(int sig)
{
g_shutdown_requested = 1;
/// Forward the signal to the temporary server if one is running.
pid_t pid = g_init_server_pid;
if (pid > 0)
kill(pid, sig);
}
/// Get an environment variable value, returning default_value if not set.
std::string getEnv(const char * name, const std::string & default_value = "")
{
const char * val = std::getenv(name); // NOLINT(concurrency-mt-unsafe)
return val ? std::string(val) : default_value;
}
/// Build an execvp-compatible argv array from a vector of strings.
std::vector<char *> buildArgv(const std::vector<std::string> & args)
{
std::vector<char *> argv;
argv.reserve(args.size() + 1);
for (const auto & a : args)
argv.push_back(const_cast<char *>(a.c_str())); // NOLINT(cppcoreguidelines-pro-type-const-cast)
argv.push_back(nullptr);
return argv;
}
/// Run a command and wait for it. Returns the exit code (or -1 on error).
int runCommand(const std::vector<std::string> & args)
{
pid_t pid = fork();
if (pid < 0)
return -1;
if (pid == 0)
{
auto argv = buildArgv(args);
execvp(argv[0], argv.data());
_exit(127);
}
int status = 0;
while (waitpid(pid, &status, 0) < 0)
if (errno != EINTR)
return -1;
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
}
/// Run a command, capture its stdout, return {exit_code, output_lines}.
std::pair<int, std::vector<std::string>> captureCommand(const std::vector<std::string> & args)
{
int pipefd[2];
if (pipe(pipefd) < 0)
return {-1, {}};
pid_t pid = fork();
if (pid < 0)
{
(void)close(pipefd[0]);
(void)close(pipefd[1]);
return {-1, {}};
}
if (pid == 0)
{
(void)close(pipefd[0]);
if (dup2(pipefd[1], STDOUT_FILENO) < 0)
_exit(127);
(void)close(pipefd[1]);
/// Suppress stderr to avoid noise from --try extractions.
int devnull = open("/dev/null", O_WRONLY);
if (devnull >= 0)
{
(void)dup2(devnull, STDERR_FILENO);
(void)close(devnull);
}
auto argv = buildArgv(args);
execvp(argv[0], argv.data());
_exit(127);
}
(void)close(pipefd[1]);
std::string output;
char buf[4096];
ssize_t n = 0;
while ((n = read(pipefd[0], buf, sizeof(buf))) > 0)
output.append(buf, static_cast<size_t>(n));
(void)close(pipefd[0]);
int status = 0;
while (waitpid(pid, &status, 0) < 0)
if (errno != EINTR)
return {-1, {}};
/// Split output into non-empty lines.
std::vector<std::string> lines;
{
size_t pos = 0;
while (pos < output.size())
{
size_t found = output.find('\n', pos);
if (found == std::string::npos)
found = output.size();
std::string line = output.substr(pos, found - pos);
if (!line.empty() && line.back() == '\r')
line.pop_back();
if (!line.empty())
lines.push_back(std::move(line));
pos = found + 1;
}
}
return {WIFEXITED(status) ? WEXITSTATUS(status) : -1, std::move(lines)};
}
/// Run two commands connected by a pipe: lhs | rhs.
/// Returns the exit code of the rhs process.
int runPipeline(const std::vector<std::string> & lhs, const std::vector<std::string> & rhs)
{
int pipefd[2];
if (pipe(pipefd) < 0)
return -1;
pid_t lhs_pid = fork();
if (lhs_pid < 0)
{
(void)close(pipefd[0]);
(void)close(pipefd[1]);
return -1;
}
if (lhs_pid == 0)
{
(void)close(pipefd[0]);
(void)dup2(pipefd[1], STDOUT_FILENO);
(void)close(pipefd[1]);
auto argv = buildArgv(lhs);
execvp(argv[0], argv.data());
_exit(127);
}
pid_t rhs_pid = fork();
if (rhs_pid < 0)
{
(void)close(pipefd[0]);
(void)close(pipefd[1]);
kill(lhs_pid, SIGTERM);
while (waitpid(lhs_pid, nullptr, 0) < 0 && errno == EINTR) {}
return -1;
}
if (rhs_pid == 0)
{
(void)close(pipefd[1]);
(void)dup2(pipefd[0], STDIN_FILENO);
(void)close(pipefd[0]);
auto argv = buildArgv(rhs);
execvp(argv[0], argv.data());
_exit(127);
}
(void)close(pipefd[0]);
(void)close(pipefd[1]);
int lhs_status = 0;
int rhs_status = 0;
while (waitpid(lhs_pid, &lhs_status, 0) < 0 && errno == EINTR) {}
while (waitpid(rhs_pid, &rhs_status, 0) < 0 && errno == EINTR) {}
return WIFEXITED(rhs_status) ? WEXITSTATUS(rhs_status) : -1;
}
/// Returns true if the string is a safe ClickHouse identifier:
/// alphanumeric + underscore, not starting with a digit.
/// Used to validate CLICKHOUSE_USER and CLICKHOUSE_DB before embedding in SQL/XML.
bool isValidIdentifier(const std::string & s)
{
if (s.empty())
return false;
if (std::isdigit(static_cast<unsigned char>(s[0])))
return false;
for (unsigned char c : s)
if (!std::isalnum(c) && c != '_')
return false;
return true;
}
/// Extract a single value from a ClickHouse config key via `clickhouse extract-from-config`.
/// Returns an empty string if the key is absent (--try flag suppresses errors).
std::string extractConfigValue(const std::string & config_file, const std::string & key, bool use_users = false)
{
std::vector<std::string> args = {
g_clickhouse_binary, "extract-from-config",
"--config-file", config_file,
"--key", key,
"--try",
};
if (use_users)
args.emplace_back("--users");
auto [code, lines] = captureCommand(args);
return (code == 0 && !lines.empty()) ? lines[0] : std::string{};
}
/// Extract multiple values from a ClickHouse config key (wildcard patterns return multiple lines).
std::vector<std::string> extractConfigValues(const std::string & config_file, const std::string & key)
{
auto [code, lines] = captureCommand({
g_clickhouse_binary, "extract-from-config",
"--config-file", config_file,
"--key", key,
"--try",
});
return (code == 0) ? std::move(lines) : std::vector<std::string>{};
}
/// Recursively chown a path. Logs warnings but does not abort on failure.
void recursiveChown(const std::string & path_str, uid_t uid, gid_t gid)
{
if (lchown(path_str.c_str(), uid, gid) < 0)
std::cerr << "docker-init: warning: lchown " << path_str << ": " << strerror(errno) << "\n"; // NOLINT(concurrency-mt-unsafe)
std::error_code ec;
if (!fs::is_directory(path_str, ec))
return;
for (const auto & entry : fs::recursive_directory_iterator(path_str, fs::directory_options::skip_permission_denied, ec))
{
if (lchown(entry.path().c_str(), uid, gid) < 0)
std::cerr << "docker-init: warning: lchown " << entry.path() << ": " << strerror(errno) << "\n"; // NOLINT(concurrency-mt-unsafe)
}
}
/// Create a directory (and all parents) and optionally chown it.
///
/// Three cases:
/// 1. do_chown=true (root, normal mode): create with fs::create_directories, then chown.
/// 2. do_chown=false, running as root (CLICKHOUSE_DO_NOT_CHOWN=1): delegate to
/// `clickhouse su UID:GID` so the directory is created as the target user.
/// This handles NFS mounts where root is mapped to nobody.
/// 3. do_chown=false, running as non-root: create directly — we are already the target user.
bool createDirectoryAndChown(const std::string & dir, uid_t uid, gid_t gid, bool do_chown)
{
if (dir.empty())
return true;
if (do_chown)
{
std::error_code ec;
fs::create_directories(dir, ec);
if (ec)
{
std::cerr << "docker-init: couldn't create directory " << dir << ": " << ec.message() << "\n";
return false;
}
/// Chown only if the owner needs to change (avoids slow recursive chown on already-correct dirs).
struct stat st{};
if (stat(dir.c_str(), &st) == 0 && (st.st_uid != uid || st.st_gid != gid))
recursiveChown(dir, uid, gid);
return true;
}
if (getuid() == 0)
{
/// Running as root with CLICKHOUSE_DO_NOT_CHOWN or CLICKHOUSE_RUN_AS_ROOT.
/// On NFS mounts root may be remapped to nobody, so create the directory as
/// the target user. Fork a child that drops privileges before calling
/// fs::create_directories — distroless has no mkdir binary.
pid_t pid = fork();
if (pid < 0)
{
std::cerr << "docker-init: fork failed for directory creation: " << strerror(errno) << "\n"; // NOLINT(concurrency-mt-unsafe)
return false;
}
if (pid == 0)
{
if (setgroups(0, nullptr) < 0 || setgid(gid) < 0 || setuid(uid) < 0)
_exit(1);
std::error_code ec;
fs::create_directories(dir, ec);
_exit(ec ? 1 : 0);
}
int status = 0;
while (waitpid(pid, &status, 0) < 0 && errno == EINTR) {}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
{
/// Fallback: try direct creation (works when root is not remapped).
std::error_code ec;
fs::create_directories(dir, ec);
if (ec)
{
std::cerr << "docker-init: couldn't create directory " << dir << ": " << ec.message() << "\n";
return false;
}
}
return true;
}
/// Non-root: we are already running as UID:GID, so create the directory directly.
std::error_code ec;
fs::create_directories(dir, ec);
if (ec)
{
std::cerr << "docker-init: couldn't create directory " << dir << ": " << ec.message() << "\n";
return false;
}
return true;
}
/// Write the user management XML to `/etc/clickhouse-server/users.d/default-user.xml`.
/// Returns false if a user-requested setup (CLICKHOUSE_USER/PASSWORD/ACCESS_MANAGEMENT)
/// is invalid — caller should treat this as fatal.
bool manageClickHouseUser(
const std::string & config_file,
const std::string & clickhouse_user,
const std::string & clickhouse_password,
const std::string & access_management,
bool skip_user_setup)
{
if (skip_user_setup)
{
std::cerr << "docker-init: explicitly skip changing user 'default'\n";
return true;
}
const std::string users_d_dir = "/etc/clickhouse-server/users.d";
const std::string default_user_xml = users_d_dir + "/default-user.xml";
std::error_code ec;
fs::create_directories(users_d_dir, ec);
/// Detect whether the default user was customised via a mounted config file.
bool clickhouse_default_changed = false;
std::string users_xml_path = extractConfigValue(config_file, "user_directories.users_xml.path");
if (!users_xml_path.empty())
{
std::string abs_users_xml = (users_xml_path[0] == '/')
? users_xml_path
: (fs::path(config_file).parent_path() / users_xml_path).string();
auto join = [](const std::vector<std::string> & v)
{
std::string s;
for (const auto & line : v)
s += line + "\n";
return s;
};
auto [c1, original] = captureCommand({
g_clickhouse_binary, "extract-from-config",
"--config-file", abs_users_xml,
"--key", "users.default", "--try",
});
auto [c2, processed] = captureCommand({
g_clickhouse_binary, "extract-from-config",
"--config-file", config_file,
"--users", "--key", "users.default", "--try",
});
if (c1 == 0 && c2 == 0 && join(original) != join(processed))
clickhouse_default_changed = true;
}
bool has_custom_user = !clickhouse_user.empty() && clickhouse_user != "default";
bool has_password = !clickhouse_password.empty();
bool has_access_mgmt = access_management != "0";
if (has_custom_user || has_password || has_access_mgmt)
{
if (access_management != "0" && access_management != "1")
{
std::cerr << "docker-init: error: CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT must be '0' or '1', got '"
<< access_management << "'\n";
return false;
}
if (!isValidIdentifier(clickhouse_user))
{
std::cerr << "docker-init: error: CLICKHOUSE_USER '" << clickhouse_user
<< "' contains characters not allowed in an XML element name; "
"use only alphanumeric characters and underscores\n";
return false;
}
std::cerr << "docker-init: create new user '" << clickhouse_user << "' instead 'default'\n";
/// Escape CDATA end marker: ]]> → ]]]]><![CDATA[>
std::string escaped_password;
{
std::string_view src = clickhouse_password;
const std::string_view needle = "]]>";
const std::string_view replacement = "]]]]><![CDATA[>";
size_t pos = 0;
size_t found = 0;
while ((found = src.find(needle, pos)) != std::string_view::npos)
{
escaped_password.append(src, pos, found - pos);
escaped_password += replacement;
pos = found + needle.size();
}
escaped_password.append(src, pos, src.size() - pos);
}
{
std::ofstream f(default_user_xml);
f << "<clickhouse>\n"
<< " <!-- Docs: <https://clickhouse.com/docs/operations/settings/settings_users/> -->\n"
<< " <users>\n"
<< " <!-- Remove default user -->\n"
<< " <default remove=\"remove\">\n"
<< " </default>\n"
<< "\n"
<< " <" << clickhouse_user << ">\n"
<< " <profile>default</profile>\n"
<< " <networks>\n"
<< " <ip>::/0</ip>\n"
<< " </networks>\n"
<< " <password><![CDATA[" << escaped_password << "]]></password>\n"
<< " <quota>default</quota>\n"
<< " <access_management>" << access_management << "</access_management>\n"
<< " </" << clickhouse_user << ">\n"
<< " </users>\n"
<< "</clickhouse>\n";
if (!f.good())
std::cerr << "docker-init: error: failed to write " << default_user_xml << "\n";
}
}
else if (clickhouse_default_changed)
{
/// A mounted config already customised the user — leave it as-is.
}
else
{
std::cerr << "docker-init: neither CLICKHOUSE_USER nor CLICKHOUSE_PASSWORD is set, "
"disabling network access for user 'default'\n";
{
std::ofstream f(default_user_xml);
f << "<clickhouse>\n"
<< " <!-- Docs: <https://clickhouse.com/docs/operations/settings/settings_users/> -->\n"
<< " <users>\n"
<< " <default>\n"
<< " <!-- User default is available only locally -->\n"
<< " <networks>\n"
<< " <ip>::1</ip>\n"
<< " <ip>127.0.0.1</ip>\n"
<< " </networks>\n"
<< " </default>\n"
<< " </users>\n"
<< "</clickhouse>\n";
if (!f.good())
std::cerr << "docker-init: error: failed to write " << default_user_xml << "\n";
}
}
return true;
}
/// Returns false on first error (fail-fast, matches shell `set -e`).
bool createClickHouseDatabase(
const std::vector<std::string> & client_base,
const std::string & clickhouse_db)
{
if (clickhouse_db.empty())
return true;
if (!isValidIdentifier(clickhouse_db))
{
std::cerr << "docker-init: error: CLICKHOUSE_DB '" << clickhouse_db
<< "' contains characters not safe for use in SQL; "
"use only alphanumeric characters and underscores\n";
return false;
}
std::cerr << "docker-init: create database '" << clickhouse_db << "'\n";
std::vector<std::string> args = client_base;
args.insert(args.end(), {"-q", "CREATE DATABASE IF NOT EXISTS " + clickhouse_db});
if (runCommand(args) != 0)
{
std::cerr << "docker-init: error: failed to create database '" << clickhouse_db << "'\n";
return false;
}
return true;
}
/// Returns false on first script failure (fail-fast, matches shell `set -e`).
bool runInitScripts(const std::vector<std::string> & client_base)
{
std::error_code ec;
if (!fs::is_directory("/docker-entrypoint-initdb.d", ec))
return true;
std::vector<fs::path> init_files;
for (const auto & entry : fs::directory_iterator("/docker-entrypoint-initdb.d", ec))
init_files.push_back(entry.path());
std::sort(init_files.begin(), init_files.end());
for (const auto & path : init_files)
{
std::string filename = path.filename().string();
if (filename.ends_with(".sql") && !filename.ends_with(".sql.gz"))
{
std::cerr << "docker-init: running " << path << "\n";
std::vector<std::string> args = client_base;
args.emplace_back("--queries-file");
args.push_back(path.string());
if (runCommand(args) != 0)
{
std::cerr << "docker-init: error: init script " << path << " failed\n";
return false;
}
}
else if (filename.ends_with(".sql.gz"))
{
std::cerr << "docker-init: running " << path << " (decompressing)\n";
/// Decompress via clickhouse-local (auto-detects .gz) and pipe to clickhouse-client.
/// Escape single quotes in the path to prevent SQL injection via crafted filenames.
std::string escaped_path;
for (char c : path.string())
{
if (c == '\'')
escaped_path += "''";
else
escaped_path += c;
}
std::vector<std::string> decompress_args = {
g_clickhouse_binary, "local",
"--query",
"SELECT * FROM file('" + escaped_path + "', RawBLOB) FORMAT RawBLOB",
};
if (runPipeline(decompress_args, client_base) != 0)
{
std::cerr << "docker-init: error: init script " << path << " failed\n";
return false;
}
}
else if (filename.ends_with(".sh"))
{
std::cerr << "docker-init: WARNING: shell scripts cannot run in a distroless "
"environment, skipping " << path << "\n";
}
else
{
std::cerr << "docker-init: ignoring " << path << "\n";
}
}
return true;
}
/// Start a temporary ClickHouse server, run init scripts, then stop it.
bool initClickHouseDB(
const std::string & config_file,
const std::string & data_dir,
const std::string & clickhouse_user,
const std::string & clickhouse_password,
uid_t run_uid,
gid_t run_gid,
const std::vector<std::string> & extra_server_args,
bool always_run_initdb)
{
/// Skip if data directory is already initialised and CLICKHOUSE_ALWAYS_RUN_INITDB_SCRIPTS is unset.
bool database_exists = fs::is_directory(data_dir + "/data");
if (!always_run_initdb && database_exists)
{
std::cerr << "docker-init: ClickHouse data directory appears to contain a database; "
"skipping initialization\n";
return true;
}
std::string clickhouse_db = getEnv("CLICKHOUSE_DB");
/// Check whether /docker-entrypoint-initdb.d has any files.
std::error_code ec;
bool has_init_files = fs::is_directory("/docker-entrypoint-initdb.d", ec)
&& fs::directory_iterator("/docker-entrypoint-initdb.d", ec) != fs::directory_iterator{};
if (!has_init_files && clickhouse_db.empty())
return true;
std::string native_port = extractConfigValue(config_file, "tcp_port");
if (native_port.empty())
native_port = "9000";
std::string run_as = std::to_string(run_uid) + ":" + std::to_string(run_gid);
/// Start a temporary server bound only to localhost.
/// The "--" separator is required: positional arguments after "--" override config.xml
/// properties (e.g. --listen_host=127.0.0.1), while options before "--" are parsed by
/// Poco and must be registered. Without "--", Poco rejects "--listen_host" as unknown.
std::vector<std::string> server_args = {
g_clickhouse_binary, "su", run_as, "clickhouse-server",
"--config-file=" + config_file,
"--", "--listen_host=127.0.0.1",
};
for (const auto & arg : extra_server_args)
server_args.push_back(arg);
if (g_shutdown_requested)
return true;
pid_t server_pid = fork();
if (server_pid < 0)
{
std::cerr << "docker-init: failed to fork temporary server\n";
return false;
}
if (server_pid == 0)
{
auto argv = buildArgv(server_args);
execvp(argv[0], argv.data());
_exit(127);
}
/// Allow the signal handler to forward SIGTERM to the temp server.
g_init_server_pid = server_pid;
/// Poll until the server accepts connections.
/// This is a service-readiness wait, not a race condition workaround.
int tries = 1000;
{
std::string timeout_str = getEnv("CLICKHOUSE_INIT_TIMEOUT", "1000");
try
{
tries = std::stoi(timeout_str);
}
catch (const std::exception &)
{
std::cerr << "docker-init: warning: invalid CLICKHOUSE_INIT_TIMEOUT '"
<< timeout_str << "', using default 1000\n";
}
}
bool server_ready = false;
while (tries > 0 && !server_ready && !g_shutdown_requested)
{
pid_t check_pid = fork();
if (check_pid < 0)
{
/// fork failed — skip this iteration and retry.
--tries;
sleep(1); // NOLINT(concurrency-mt-unsafe)
continue;
}
if (check_pid == 0)
{
int devnull = open("/dev/null", O_WRONLY);
if (devnull >= 0)
{
dup2(devnull, STDOUT_FILENO);
dup2(devnull, STDERR_FILENO);
close(devnull);
}
/// Keep args in a named variable so the strings outlive argv.
std::vector<std::string> check_args = {
g_clickhouse_binary, "client",
"--host", "127.0.0.1",
"--port", native_port,
"-u", clickhouse_user,
"--password", clickhouse_password,
"--query", "SELECT 1",
};
auto argv = buildArgv(check_args);
execvp(argv[0], argv.data());
_exit(127);
}
int check_status = 0;
while (waitpid(check_pid, &check_status, 0) < 0 && errno == EINTR) {}
if (WIFEXITED(check_status) && WEXITSTATUS(check_status) == 0)
{
server_ready = true;
}
else
{
--tries;
sleep(1); // NOLINT(concurrency-mt-unsafe) -- Wait between health-check retries — not a race condition fix.
}
}
if (!server_ready)
{
if (g_shutdown_requested)
std::cerr << "docker-init: shutdown requested, stopping init server\n";
else
std::cerr << "docker-init: ClickHouse init process timed out\n";
kill(server_pid, SIGTERM);
while (waitpid(server_pid, nullptr, 0) < 0 && errno == EINTR) {}
g_init_server_pid = 0;
return false;
}
std::vector<std::string> client_base = {
g_clickhouse_binary, "client",
"--multiquery",
"--host", "127.0.0.1",
"--port", native_port,
"-u", clickhouse_user,
"--password", clickhouse_password,
};
const bool ok = createClickHouseDatabase(client_base, clickhouse_db)
&& runInitScripts(client_base);
/// Always stop the temporary server regardless of init result.
kill(server_pid, SIGTERM);
int server_status = 0;
while (waitpid(server_pid, &server_status, 0) < 0 && errno == EINTR) {}
g_init_server_pid = 0;
if (!WIFEXITED(server_status) || WEXITSTATUS(server_status) != 0)
std::cerr << "docker-init: warning: init server did not exit cleanly\n";
return ok;
}
} // anonymous namespace
int mainEntryClickHouseDockerInit(int argc, char ** argv);
int mainEntryClickHouseDockerInit(int argc, char ** argv)
{
g_clickhouse_binary = (argc > 0 && argv[0][0] != '\0') ? argv[0] : "clickhouse";
bool keeper_mode = false;
bool show_help = false;
bool separator_seen = false;
std::vector<std::string> extra_args;
for (int i = 1; i < argc; ++i)
{
std::string_view arg = argv[i];
if (!separator_seen && (arg == "--help" || arg == "-h"))
{
show_help = true;
break;
}
else if (!separator_seen && arg == "--keeper")
keeper_mode = true;
else if (!separator_seen && arg == "--")
separator_seen = true;
else
extra_args.emplace_back(arg);
}
if (show_help)
{
std::cout
<< "Usage: clickhouse docker-init [--keeper] [-- <extra-args>...]\n"
"Docker entrypoint for distroless ClickHouse images.\n"
"\nOptions:\n"
" --keeper Start ClickHouse Keeper instead of server\n"
" --help Show this help message\n"
"\nEnvironment variables (server mode):\n"
" CLICKHOUSE_CONFIG Path to config file "
"(default: /etc/clickhouse-server/config.xml)\n"
" CLICKHOUSE_RUN_AS_ROOT Run as root (0/1)\n"
" CLICKHOUSE_DO_NOT_CHOWN Skip chown operations (0/1)\n"
" CLICKHOUSE_UID Override UID to run as\n"
" CLICKHOUSE_GID Override GID to run as\n"
" CLICKHOUSE_USER Default user name (default: default)\n"
" CLICKHOUSE_PASSWORD Default user password\n"
" CLICKHOUSE_PASSWORD_FILE File containing password\n"
" CLICKHOUSE_DB Database to create on init\n"
" CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT Enable access management (0/1)\n"
" CLICKHOUSE_SKIP_USER_SETUP Skip user setup (0/1)\n"
" CLICKHOUSE_ALWAYS_RUN_INITDB_SCRIPTS Always run init scripts\n"
" CLICKHOUSE_INIT_TIMEOUT Max retries for server readiness (default: 1000)\n"
" CLICKHOUSE_WATCHDOG_ENABLE Enable watchdog (default: 0)\n"
"\nEnvironment variables (keeper mode):\n"
" KEEPER_CONFIG Path to keeper config file\n"
" CLICKHOUSE_DATA_DIR Data directory (default: /var/lib/clickhouse)\n"
" LOG_DIR Log directory (default: /var/log/clickhouse-keeper)\n";
return 0;
}
/// --- Passthrough mode ---
/// If the first extra argument does not start with '--', treat it as a command to exec
/// directly without server startup. This mirrors entrypoint.sh:
/// if [[ "$1" == "--"* ]]; then start server; fi; exec "$@"
///
/// For recognized ClickHouse subcommand names (client, local, etc.) resolve the path
/// via bin_dir so that multi-tool dispatch (by argv[0] basename) works correctly.
/// For everything else (echo, date, bash, ...) let PATH resolution handle it.
if (!extra_args.empty() && !extra_args[0].starts_with("--"))
{
static constexpr std::array clickhouse_tools = {
"clickhouse-client",
"clickhouse-local",
"clickhouse-keeper-client",
"clickhouse-benchmark",
"clickhouse-format",
"clickhouse-compressor",
"clickhouse-obfuscator",
"clickhouse-extract-from-config",
"clickhouse-disks",
"client",
"local",
"keeper-client",
"benchmark",
"format",
"compressor",
"obfuscator",
"extract-from-config",
"disks",
};
std::string cmd = extra_args[0];
if (std::find(clickhouse_tools.begin(), clickhouse_tools.end(), extra_args[0]) != clickhouse_tools.end())
{
/// Build the full path to the symlink (e.g. /usr/bin/clickhouse-client).
/// The symlink points to the clickhouse binary; dispatching is done by argv[0].
/// Short names like "client" must be resolved to "clickhouse-client" since the
/// distroless image only has "clickhouse-*" symlinks (not bare "client", "local", etc.).
fs::path bin_dir = fs::path(g_clickhouse_binary).parent_path();
std::string link_name = extra_args[0];
if (!link_name.starts_with("clickhouse-"))
link_name = "clickhouse-" + link_name;
cmd = (bin_dir / link_name).string();
}
std::vector<std::string> exec_cmd = {cmd};
for (std::size_t i = 1; i < extra_args.size(); ++i)
exec_cmd.push_back(extra_args[i]);
auto exec_argv = buildArgv(exec_cmd);
execvp(exec_argv[0], exec_argv.data());
std::cerr << "docker-init: failed to exec '" << extra_args[0] << "': " << strerror(errno) << "\n"; // NOLINT(concurrency-mt-unsafe)
return 1;
}
/// --- Resolve identity ---
uid_t current_uid = getuid();
uid_t run_uid = 0;
gid_t run_gid = 0;
bool do_chown = true;
if (getEnv("CLICKHOUSE_RUN_AS_ROOT") == "1" || getEnv("CLICKHOUSE_DO_NOT_CHOWN") == "1")
do_chown = false;
if (current_uid == 0)
{
if (getEnv("CLICKHOUSE_RUN_AS_ROOT") == "1")
{
run_uid = 0;
run_gid = 0;
}
else
{
/// Default to the `clickhouse` system user if it exists, otherwise fall back to UID 101.
uid_t default_uid = 101;
gid_t default_gid = 101;
const passwd * pw = getpwnam("clickhouse"); // NOLINT(concurrency-mt-unsafe)
if (pw)
{
default_uid = pw->pw_uid;
default_gid = pw->pw_gid;
}
std::string uid_str = getEnv("CLICKHOUSE_UID");
std::string gid_str = getEnv("CLICKHOUSE_GID");
run_uid = default_uid;
run_gid = default_gid;
try
{
if (!uid_str.empty())
run_uid = static_cast<uid_t>(std::stoul(uid_str));
if (!gid_str.empty())
run_gid = static_cast<gid_t>(std::stoul(gid_str));
}
catch (const std::exception &)
{
std::cerr << "docker-init: warning: invalid CLICKHOUSE_UID/GID values, "
"using defaults\n";
}
}
}
else
{
/// Non-root: cannot chown, run as current user.
run_uid = current_uid;
run_gid = getgid();
do_chown = false;
}
std::string run_as = std::to_string(run_uid) + ":" + std::to_string(run_gid);
/// --- Keeper mode ---
if (keeper_mode)
{
std::string keeper_config = getEnv("KEEPER_CONFIG", "/etc/clickhouse-keeper/keeper_config.xml");
std::string data_dir = getEnv("CLICKHOUSE_DATA_DIR", "/var/lib/clickhouse");
std::string log_dir = getEnv("LOG_DIR", "/var/log/clickhouse-keeper");
for (const auto & dir : {data_dir, log_dir,
data_dir + "/coordination",
data_dir + "/coordination/log",
data_dir + "/coordination/snapshots"})
{
if (!createDirectoryAndChown(dir, run_uid, run_gid, do_chown))
return 1;
}
/// Default to disabled so Ctrl+C works in Docker. Don't override if already set.
setenv("CLICKHOUSE_WATCHDOG_ENABLE", "0", 0); // NOLINT(concurrency-mt-unsafe)
chdir(data_dir.c_str()); // NOLINT(bugprone-unused-return-value)
std::vector<std::string> exec_args = {
g_clickhouse_binary, "su", run_as, "clickhouse-keeper",
};
std::error_code ec;
if (!keeper_config.empty() && fs::exists(keeper_config, ec))
exec_args.push_back("--config-file=" + keeper_config);
for (const auto & arg : extra_args)
exec_args.push_back(arg);
auto exec_argv = buildArgv(exec_args);
execvp(exec_argv[0], exec_argv.data());
std::cerr << "docker-init: failed to exec clickhouse keeper: " << strerror(errno) << "\n"; // NOLINT(concurrency-mt-unsafe)
return 1;
}
/// --- Server mode ---
std::string config_file = getEnv("CLICKHOUSE_CONFIG", "/etc/clickhouse-server/config.xml");
/// Extract all relevant paths from the config.
std::string data_dir = extractConfigValue(config_file, "path");
std::string tmp_dir = extractConfigValue(config_file, "tmp_path");
std::string user_files_path = extractConfigValue(config_file, "user_files_path");
std::string format_schema_path = extractConfigValue(config_file, "format_schema_path");
std::string log_dir;
std::string log_path = extractConfigValue(config_file, "logger.log");
if (!log_path.empty())
log_dir = fs::path(log_path).parent_path().string();
std::string error_log_dir;
std::string error_log_path = extractConfigValue(config_file, "logger.errorlog");
if (!error_log_path.empty())
error_log_dir = fs::path(error_log_path).parent_path().string();
auto disk_paths = extractConfigValues(config_file, "storage_configuration.disks.*.path");
auto disk_metadata_paths = extractConfigValues(config_file, "storage_configuration.disks.*.metadata_path");
/// Create and chown data directory first, then cd into it.
if (!data_dir.empty() && !createDirectoryAndChown(data_dir, run_uid, run_gid, do_chown))
return 1;
chdir(data_dir.empty() ? "/" : data_dir.c_str()); // NOLINT(bugprone-unused-return-value)