docker: use /host/etc/hostname if mounted (#16401)
Ilya Mashchenko committed
Nov 16, 2023 at 17:09 UTC
5344c11728dfcd427ca6f5561f701b8680dd9598
2 files changed
+20
-8
daemon/main.c
+18
-6
@@ -1054,16 +1054,32 @@ static void backwards_compatible_config() {
1054
1055
}
1056
1057
+static int get_hostname(char *buf, size_t buf_size) {
1058
+ if (netdata_configured_host_prefix && *netdata_configured_host_prefix) {
1059
+ char filename[FILENAME_MAX + 1];
1060
+ snprintfz(filename, FILENAME_MAX, "%s/etc/hostname", netdata_configured_host_prefix);
1061
+
1062
+ if (!read_file(filename, buf, buf_size)) {
1063
+ trim(buf);
1064
+ return 0;
1065
+ }
1066
+ }
1067
+
1068
+ return gethostname(buf, buf_size);
1069
+}
1070
+
1071
static void get_netdata_configured_variables() {
1072
backwards_compatible_config();
1073
1074
// ------------------------------------------------------------------------
1075
// get the hostname
1076
1077
+ netdata_configured_host_prefix = config_get(CONFIG_SECTION_GLOBAL, "host access prefix", "");
1078
+ verify_netdata_host_prefix();
1079
+
1080
char buf[HOSTNAME_MAX + 1];
1064
- if(gethostname(buf, HOSTNAME_MAX) == -1){
1081
+ if (get_hostname(buf, HOSTNAME_MAX))
1082
netdata_log_error("Cannot get machine hostname.");
1066
- }
1083
1084
netdata_configured_hostname = config_get(CONFIG_SECTION_GLOBAL, "hostname", buf);
1085
netdata_log_debug(D_OPTIONS, "hostname set to '%s'", netdata_configured_hostname);
@@ -1163,10 +1179,6 @@ static void get_netdata_configured_variables() {
1179
default_rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
1180
}
1181
#endif
1166
- // ------------------------------------------------------------------------
1167
-
1168
- netdata_configured_host_prefix = config_get(CONFIG_SECTION_GLOBAL, "host access prefix", "");
1169
- verify_netdata_host_prefix();
1182
1183
// --------------------------------------------------------------------
1184
// get KSM settings
packaging/docker/README.md
+2
-2
@@ -453,9 +453,9 @@ above section on [configuring Agent containers](#configure-agent-containers) to
453
how you created the container.
454
455
Alternatively, you can directly use the hostname from the node running the container by mounting `/etc/hostname` from
456
-the host in the container. With `docker run`, this can be done by adding `--volume /etc/hostname:/etc/hostname:ro` to
456
+the host in the container. With `docker run`, this can be done by adding `--volume /etc/hostname:/host/etc/hostname:ro` to
457
the options. If you are using Docker Compose, you can add an entry to the container's `volumes` section
458
-reading `- /etc/hostname:/etc/hostname:ro`.
458
+reading `- /etc/hostname:/host/etc/hostname:ro`.
459
460
## Adding extra packages at runtime
461