Skip to content

Commit

Permalink
Refine docker detect mechenism. v5.0.190 v6.0.90 (ossrs#3758)
Browse files Browse the repository at this point in the history
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 <hondaxiao@tencent.com>
  • Loading branch information
winlinvip and xiaozhihong authored Oct 17, 2023
1 parent c91e3a3 commit a458c9c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

2 changes: 2 additions & 0 deletions trunk/doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The changelog for SRS.
<a name="v6-changes"></a>

## 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)
Expand Down Expand Up @@ -101,6 +102,7 @@ The changelog for SRS.
<a name="v5-changes"></a>

## 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)
Expand Down
49 changes: 46 additions & 3 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_version5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

#define VERSION_MAJOR 5
#define VERSION_MINOR 0
#define VERSION_REVISION 189
#define VERSION_REVISION 190

#endif
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_version6.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

#define VERSION_MAJOR 6
#define VERSION_MINOR 0
#define VERSION_REVISION 89
#define VERSION_REVISION 90

#endif
48 changes: 3 additions & 45 deletions trunk/src/main/srs_main_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit a458c9c

Please sign in to comment.