Bool function in PHP to check if you run the script inside docker

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I had to do a CLI script that does something, I also had to add a validation that checks if it runs inside the Docker. After a research I found a solution that people check some processes via cat /proc/self/cgroup and then grep to search for a docker phrase within.



So I ended with thin method. It works pretty well. Inside docker it returns true, on my local mac it returns false.



It's not a rocket science however I would like people to take a look and tell me if this is a sufficient method or a completely different approach is better.



private function isDocker(): bool

$processStack = explode(PHP_EOL, shell_exec('cat /proc/self/cgroup'));
$processStack = array_filter($processStack); // remove empty item made by EOL

foreach ($processStack as $process)
if (strpos($process, 'docker') === false)
return false;



return true;



Later call to this script looks like:



if ($this->isDocker() === false) 
throw new Exception('This helper script can be called only inside Docker container.' . "n");







share|improve this question

























    up vote
    1
    down vote

    favorite












    I had to do a CLI script that does something, I also had to add a validation that checks if it runs inside the Docker. After a research I found a solution that people check some processes via cat /proc/self/cgroup and then grep to search for a docker phrase within.



    So I ended with thin method. It works pretty well. Inside docker it returns true, on my local mac it returns false.



    It's not a rocket science however I would like people to take a look and tell me if this is a sufficient method or a completely different approach is better.



    private function isDocker(): bool

    $processStack = explode(PHP_EOL, shell_exec('cat /proc/self/cgroup'));
    $processStack = array_filter($processStack); // remove empty item made by EOL

    foreach ($processStack as $process)
    if (strpos($process, 'docker') === false)
    return false;



    return true;



    Later call to this script looks like:



    if ($this->isDocker() === false) 
    throw new Exception('This helper script can be called only inside Docker container.' . "n");







    share|improve this question





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I had to do a CLI script that does something, I also had to add a validation that checks if it runs inside the Docker. After a research I found a solution that people check some processes via cat /proc/self/cgroup and then grep to search for a docker phrase within.



      So I ended with thin method. It works pretty well. Inside docker it returns true, on my local mac it returns false.



      It's not a rocket science however I would like people to take a look and tell me if this is a sufficient method or a completely different approach is better.



      private function isDocker(): bool

      $processStack = explode(PHP_EOL, shell_exec('cat /proc/self/cgroup'));
      $processStack = array_filter($processStack); // remove empty item made by EOL

      foreach ($processStack as $process)
      if (strpos($process, 'docker') === false)
      return false;



      return true;



      Later call to this script looks like:



      if ($this->isDocker() === false) 
      throw new Exception('This helper script can be called only inside Docker container.' . "n");







      share|improve this question











      I had to do a CLI script that does something, I also had to add a validation that checks if it runs inside the Docker. After a research I found a solution that people check some processes via cat /proc/self/cgroup and then grep to search for a docker phrase within.



      So I ended with thin method. It works pretty well. Inside docker it returns true, on my local mac it returns false.



      It's not a rocket science however I would like people to take a look and tell me if this is a sufficient method or a completely different approach is better.



      private function isDocker(): bool

      $processStack = explode(PHP_EOL, shell_exec('cat /proc/self/cgroup'));
      $processStack = array_filter($processStack); // remove empty item made by EOL

      foreach ($processStack as $process)
      if (strpos($process, 'docker') === false)
      return false;



      return true;



      Later call to this script looks like:



      if ($this->isDocker() === false) 
      throw new Exception('This helper script can be called only inside Docker container.' . "n");









      share|improve this question










      share|improve this question




      share|improve this question









      asked Aug 1 at 22:31









      slick

      1063




      1063

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200779%2fbool-function-in-php-to-check-if-you-run-the-script-inside-docker%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f200779%2fbool-function-in-php-to-check-if-you-run-the-script-inside-docker%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation