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;
$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");
php strings docker
$endgroup$
add a comment |
$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");
php strings docker
$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
add a comment |
$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");
php strings docker
$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
php strings docker
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
add a comment |
$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
add a comment |
1 Answer
1
active
oldest
votes
$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;
$endgroup$
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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;
$endgroup$
add a comment |
$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;
$endgroup$
add a comment |
$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;
$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;
edited 11 mins ago
answered Aug 10 '18 at 17:46
Sᴀᴍ OnᴇᴌᴀSᴀᴍ Onᴇᴌᴀ
10.6k62168
10.6k62168
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$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