1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Refine docker detect mechenism. v5.0.190 (#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 <hondaxiao@tencent.com>
This commit is contained in:
john 2023-10-17 21:27:07 +08:00
parent ef7fe1b186
commit 3da81e4b75
5 changed files with 52 additions and 50 deletions

View file

@ -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