From a458c9c68d4b33c3c842e0b5a3885346e10dda30 Mon Sep 17 00:00:00 2001 From: Winlin Date: Tue, 17 Oct 2023 21:24:12 +0800 Subject: [PATCH] Refine docker detect mechenism. v5.0.190 v6.0.90 (#3758) When using Docker, logs are usually printed to console (stdout and stderr). However, since Docker detection occurs late, after log initialization, the default log output may be incorrect. In Docker, logs may still be written to a file instead of the console as expected. Additionally, the Dockerfile has been improved with a new environment variable `SRS_IN_DOCKER=on` to clearly indicate a Docker environment. If automatic Docker detection fails, the configuration will be read, and this variable will correctly inform SRS that it's in a Docker environment. Lastly, the default configuration values have been improved for Docker environments. By default, `SRS_LOG_TANK=console` and daemon mode is disabled. --------- Co-authored-by: john --- Dockerfile | 2 +- trunk/doc/CHANGELOG.md | 2 ++ trunk/src/app/srs_app_config.cpp | 49 ++++++++++++++++++++++++++-- trunk/src/core/srs_core_version5.hpp | 2 +- trunk/src/core/srs_core_version6.hpp | 2 +- trunk/src/main/srs_main_server.cpp | 48 ++------------------------- 6 files changed, 54 insertions(+), 51 deletions(-) diff --git a/Dockerfile b/Dockerfile index 63d3783f5..07cf8b5e0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -56,6 +56,6 @@ RUN ldd /usr/local/srs/objs/ffmpeg/bin/ffmpeg && \ # Default workdir and command. WORKDIR /usr/local/srs -ENV SRS_DAEMON=off +ENV SRS_DAEMON=off SRS_IN_DOCKER=on CMD ["./objs/srs", "-c", "conf/docker.conf"] diff --git a/trunk/doc/CHANGELOG.md b/trunk/doc/CHANGELOG.md index e6db4a8e1..94d188e02 100644 --- a/trunk/doc/CHANGELOG.md +++ b/trunk/doc/CHANGELOG.md @@ -7,6 +7,7 @@ The changelog for SRS. ## SRS 6.0 Changelog +* v6.0, 2023-10-17, Merge [#3758](https://github.com/ossrs/srs/pull/3758): Refine docker detect mechenism. v6.0.90 (#3758) * v6.0, 2023-10-11, Merge [#3827](https://github.com/ossrs/srs/pull/3827): Fix bug for upgrading to OpenSSL 3.0. v6.0.89 (#3827) * v6.0, 2023-10-10, Merge [#3825](https://github.com/ossrs/srs/pull/3825): SRT: Fix the missing config mss. v6.0.88 (#3825) * v6.0, 2023-10-08, Merge [#3824](https://github.com/ossrs/srs/pull/3824): Solve the problem of inaccurate HLS TS duration. v6.0.87 (#3824) @@ -101,6 +102,7 @@ The changelog for SRS. ## SRS 5.0 Changelog +* v5.0, 2023-10-17, Merge [#3758](https://github.com/ossrs/srs/pull/3758): Refine docker detect mechenism. v5.0.190 (#3758) * v5.0, 2023-10-11, Merge [#3827](https://github.com/ossrs/srs/pull/3827): Fix bug for upgrading to OpenSSL 3.0. v5.0.189 (#3827) * v5.0, 2023-10-10, Merge [#3825](https://github.com/ossrs/srs/pull/3825): SRT: Fix the missing config mss. v5.0.188 (#3825) * v5.0, 2023-10-08, Merge [#3824](https://github.com/ossrs/srs/pull/3824): Solve the problem of inaccurate HLS TS duration. v5.0.187 (#3824) diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 3e03c82a7..172b2eaa3 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -94,6 +94,38 @@ bool is_common_space(char ch) return (ch == ' ' || ch == '\t' || ch == SRS_CR || ch == SRS_LF); } +extern bool _srs_in_docker; + +// Detect docker by https://stackoverflow.com/a/41559867 +srs_error_t srs_detect_docker() +{ + srs_error_t err = srs_success; + + _srs_in_docker = false; + + SrsFileReader fr; + if ((err = fr.open("/proc/1/cgroup")) != srs_success) { + return err; + } + + ssize_t nn; + char buf[1024]; + if ((err = fr.read(buf, sizeof(buf), &nn)) != srs_success) { + return err; + } + + if (nn <= 0) { + return err; + } + + string s(buf, nn); + if (srs_string_contains(s, "/docker")) { + _srs_in_docker = true; + } + + return err; +} + namespace srs_internal { SrsConfigBuffer::SrsConfigBuffer() @@ -1934,6 +1966,19 @@ srs_error_t SrsConfig::parse_options(int argc, char** argv) if (root->directives.empty()) root->get_or_create("vhost", "__defaultVhost__"); } + // Ignore any error while detecting docker. + if ((err = srs_detect_docker()) != srs_success) { + srs_error_reset(err); + } + + // Try to load the config if docker detect failed. + if (!_srs_in_docker) { + _srs_in_docker = _srs_config->get_in_docker(); + if (_srs_in_docker) { + srs_trace("enable in_docker by config"); + } + } + //////////////////////////////////////////////////////////////////////// // check log name and level //////////////////////////////////////////////////////////////////////// @@ -2856,7 +2901,7 @@ string SrsConfig::argv() bool SrsConfig::get_daemon() { - SRS_OVERWRITE_BY_ENV_BOOL2("srs.daemon"); + SRS_OVERWRITE_BY_ENV_BOOL2("srs.daemon"); // SRS_DAEMON SrsConfDirective* conf = root->get("daemon"); if (!conf || conf->arg0().empty()) { @@ -6520,8 +6565,6 @@ string SrsConfig::get_ingest_input_url(SrsConfDirective* conf) return conf->arg0(); } -extern bool _srs_in_docker; - bool SrsConfig::get_log_tank_file() { if (!srs_getenv("srs.srs_log_tank").empty()) { // SRS_SRS_LOG_TANK diff --git a/trunk/src/core/srs_core_version5.hpp b/trunk/src/core/srs_core_version5.hpp index c4b5686a0..d6d5b7803 100644 --- a/trunk/src/core/srs_core_version5.hpp +++ b/trunk/src/core/srs_core_version5.hpp @@ -9,6 +9,6 @@ #define VERSION_MAJOR 5 #define VERSION_MINOR 0 -#define VERSION_REVISION 189 +#define VERSION_REVISION 190 #endif diff --git a/trunk/src/core/srs_core_version6.hpp b/trunk/src/core/srs_core_version6.hpp index a614245bc..033e1007b 100644 --- a/trunk/src/core/srs_core_version6.hpp +++ b/trunk/src/core/srs_core_version6.hpp @@ -9,6 +9,6 @@ #define VERSION_MAJOR 6 #define VERSION_MINOR 0 -#define VERSION_REVISION 89 +#define VERSION_REVISION 90 #endif diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index 519b7ef38..6bf06d1d3 100644 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -57,7 +57,6 @@ using namespace std; // pre-declare srs_error_t run_directly_or_daemon(); srs_error_t run_in_thread_pool(); -srs_error_t srs_detect_docker(); void show_macro_features(); // @global log and context. @@ -79,6 +78,9 @@ bool _srs_config_by_env = false; // The binary name of SRS. const char* _srs_binary = NULL; +// @global Other variables. +bool _srs_in_docker = false; + // Free global data, for address sanitizer. extern void srs_free_global_system_ips(); @@ -131,11 +133,6 @@ srs_error_t do_main(int argc, char** argv, char** envp) #ifdef SRS_GPERF_MP #warning "gmp is not used for memory leak, please use gmc instead." #endif - - // Ignore any error while detecting docker. - if ((err = srs_detect_docker()) != srs_success) { - srs_error_reset(err); - } // never use srs log(srs_trace, srs_error, etc) before config parse the option, // which will load the log config and apply it. @@ -400,49 +397,10 @@ void show_macro_features() #endif } -// Detect docker by https://stackoverflow.com/a/41559867 -bool _srs_in_docker = false; -srs_error_t srs_detect_docker() -{ - srs_error_t err = srs_success; - - _srs_in_docker = false; - - SrsFileReader fr; - if ((err = fr.open("/proc/1/cgroup")) != srs_success) { - return err; - } - - ssize_t nn; - char buf[1024]; - if ((err = fr.read(buf, sizeof(buf), &nn)) != srs_success) { - return err; - } - - if (nn <= 0) { - return err; - } - - string s(buf, nn); - if (srs_string_contains(s, "/docker")) { - _srs_in_docker = true; - } - - return err; -} - srs_error_t run_directly_or_daemon() { srs_error_t err = srs_success; - // Try to load the config if docker detect failed. - if (!_srs_in_docker) { - _srs_in_docker = _srs_config->get_in_docker(); - if (_srs_in_docker) { - srs_trace("enable in_docker by config"); - } - } - // Load daemon from config, disable it for docker. // @see https://github.com/ossrs/srs/issues/1594 bool run_as_daemon = _srs_config->get_daemon();