Bool function in PHP to check if you run the script inside docker Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)A PHP function to check for cookiesPHP ideal way to check function returns trueThe perfect PHP login scriptCheck the security of my image upload scriptWhat is the most memory efficient way to load big arrays into PHP script?PHP login script security checkPHP rating function for getting the average value of columnA PHP function that prints the url based on pathPHP function to check whether a user is logged inPHP - hacker rank - ransom check - extra space at the end of the string

How can whole tone melodies sound more interesting?

How to recreate this effect in Photoshop?

How to motivate offshore teams and trust them to deliver?

macOS-like app switching in Plasma 5

Single word antonym of "flightless"

How does cp -a work

Why there are no cargo aircraft with "flying wing" design?

How do I stop a creek from eroding my steep embankment?

Can Pao de Queijo, and similar foods, be kosher for Passover?

Did Xerox really develop the first LAN?

How can I make names more distinctive without making them longer?

Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?

When is phishing education going too far?

Check which numbers satisfy the condition [A*B*C = A! + B! + C!]

If a contract sometimes uses the wrong name, is it still valid?

What are the pros and cons of Aerospike nosecones?

WAN encapsulation

Why did the IBM 650 use bi-quinary?

Does polymorph use a PC’s CR or its level?

Is 1 ppb equal to 1 μg/kg?

3 doors, three guards, one stone

Marking the functions of a sentence: 'She may like it'

Are variable time comparisons always a security risk in cryptography code?

How do I keep my slimes from escaping their pens?



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



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)A PHP function to check for cookiesPHP ideal way to check function returns trueThe perfect PHP login scriptCheck the security of my image upload scriptWhat is the most memory efficient way to load big arrays into PHP script?PHP login script security checkPHP rating function for getting the average value of columnA PHP function that prints the url based on pathPHP function to check whether a user is logged inPHP - hacker rank - ransom check - extra space at the end of the string



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








3












$begingroup$


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 some 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-up with this method. It works pretty well. Inside docker it returns true, while on my local mac it returns false.



It's not 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











$endgroup$











  • $begingroup$
    Hey Matt- would you be willing to let me know if my answer is useful? this question is technically still unanswered
    $endgroup$
    – Sᴀᴍ Onᴇᴌᴀ
    Mar 6 at 19:28

















3












$begingroup$


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 some 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-up with this method. It works pretty well. Inside docker it returns true, while on my local mac it returns false.



It's not 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











$endgroup$











  • $begingroup$
    Hey Matt- would you be willing to let me know if my answer is useful? this question is technically still unanswered
    $endgroup$
    – Sᴀᴍ Onᴇᴌᴀ
    Mar 6 at 19:28













3












3








3





$begingroup$


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 some 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-up with this method. It works pretty well. Inside docker it returns true, while on my local mac it returns false.



It's not 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











$endgroup$




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 some 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-up with this method. It works pretty well. Inside docker it returns true, while on my local mac it returns false.



It's not 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");







php strings docker






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 12 at 7:03









Sᴀᴍ Onᴇᴌᴀ

10.6k62168




10.6k62168










asked Aug 1 '18 at 22:31









Matt KomarnickiMatt Komarnicki

1163




1163











  • $begingroup$
    Hey Matt- would you be willing to let me know if my answer is useful? this question is technically still unanswered
    $endgroup$
    – Sᴀᴍ Onᴇᴌᴀ
    Mar 6 at 19:28
















  • $begingroup$
    Hey Matt- would you be willing to let me know if my answer is useful? this question is technically still unanswered
    $endgroup$
    – Sᴀᴍ Onᴇᴌᴀ
    Mar 6 at 19:28















$begingroup$
Hey Matt- would you be willing to let me know if my answer is useful? this question is technically still unanswered
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
Mar 6 at 19:28




$begingroup$
Hey Matt- would you be willing to let me know if my answer is useful? this question is technically still unanswered
$endgroup$
– Sᴀᴍ Onᴇᴌᴀ
Mar 6 at 19:28










1 Answer
1






active

oldest

votes


















0












$begingroup$

For this simple method, I have a couple thoughts below. Your method seems sufficient; I don't think a completely different approach is necessary, although I question how often this method is called. If it is called more than once per page load/script run, it may be worth memoizing the value once obtained for better performance.




Good job utilizing the PHP 7 return type declaration.




Because the method utilizes no instance properties or methods, it could be declared as static, and possibly public so other code could use it, though maybe it is best to leave it as private until there is a legitimate need to make it otherwise.




While the logic would need to be reversed, substr_count() could be used instead of strpos(). The difference is likely negligible but you need to ask yourself in which case would you rather have the method return as soon as the string is or isn't found...



 foreach ($processStack as $process) 
if (substr_count($process, 'docker'))
return true;


return false;





share|improve this answer











$endgroup$













    Your Answer






    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    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















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0












    $begingroup$

    For this simple method, I have a couple thoughts below. Your method seems sufficient; I don't think a completely different approach is necessary, although I question how often this method is called. If it is called more than once per page load/script run, it may be worth memoizing the value once obtained for better performance.




    Good job utilizing the PHP 7 return type declaration.




    Because the method utilizes no instance properties or methods, it could be declared as static, and possibly public so other code could use it, though maybe it is best to leave it as private until there is a legitimate need to make it otherwise.




    While the logic would need to be reversed, substr_count() could be used instead of strpos(). The difference is likely negligible but you need to ask yourself in which case would you rather have the method return as soon as the string is or isn't found...



     foreach ($processStack as $process) 
    if (substr_count($process, 'docker'))
    return true;


    return false;





    share|improve this answer











    $endgroup$

















      0












      $begingroup$

      For this simple method, I have a couple thoughts below. Your method seems sufficient; I don't think a completely different approach is necessary, although I question how often this method is called. If it is called more than once per page load/script run, it may be worth memoizing the value once obtained for better performance.




      Good job utilizing the PHP 7 return type declaration.




      Because the method utilizes no instance properties or methods, it could be declared as static, and possibly public so other code could use it, though maybe it is best to leave it as private until there is a legitimate need to make it otherwise.




      While the logic would need to be reversed, substr_count() could be used instead of strpos(). The difference is likely negligible but you need to ask yourself in which case would you rather have the method return as soon as the string is or isn't found...



       foreach ($processStack as $process) 
      if (substr_count($process, 'docker'))
      return true;


      return false;





      share|improve this answer











      $endgroup$















        0












        0








        0





        $begingroup$

        For this simple method, I have a couple thoughts below. Your method seems sufficient; I don't think a completely different approach is necessary, although I question how often this method is called. If it is called more than once per page load/script run, it may be worth memoizing the value once obtained for better performance.




        Good job utilizing the PHP 7 return type declaration.




        Because the method utilizes no instance properties or methods, it could be declared as static, and possibly public so other code could use it, though maybe it is best to leave it as private until there is a legitimate need to make it otherwise.




        While the logic would need to be reversed, substr_count() could be used instead of strpos(). The difference is likely negligible but you need to ask yourself in which case would you rather have the method return as soon as the string is or isn't found...



         foreach ($processStack as $process) 
        if (substr_count($process, 'docker'))
        return true;


        return false;





        share|improve this answer











        $endgroup$



        For this simple method, I have a couple thoughts below. Your method seems sufficient; I don't think a completely different approach is necessary, although I question how often this method is called. If it is called more than once per page load/script run, it may be worth memoizing the value once obtained for better performance.




        Good job utilizing the PHP 7 return type declaration.




        Because the method utilizes no instance properties or methods, it could be declared as static, and possibly public so other code could use it, though maybe it is best to leave it as private until there is a legitimate need to make it otherwise.




        While the logic would need to be reversed, substr_count() could be used instead of strpos(). The difference is likely negligible but you need to ask yourself in which case would you rather have the method return as soon as the string is or isn't found...



         foreach ($processStack as $process) 
        if (substr_count($process, 'docker'))
        return true;


        return false;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 11 mins ago

























        answered Aug 10 '18 at 17:46









        Sᴀᴍ OnᴇᴌᴀSᴀᴍ Onᴇᴌᴀ

        10.6k62168




        10.6k62168



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            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















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

            格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订