Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker commands via php shell_exec

I'm trying run command from my index.php:

$output = shell_exec('docker images');

and then output results,

or run new container the same way:

$output = shell_exec('docker run hello-world');

It seems that I could not run ANY docker cmd via php.

How do it properly?

like image 571
YasiuMaster Avatar asked Oct 12 '25 08:10

YasiuMaster


1 Answers

I did the following to get this working:

  1. Created a php file called index.php on /var/www/html/ with this content:

    <?php
        echo '<pre>';
        $content = system('sudo docker images', $ret);
        echo '</pre>';
    ?>
    
  2. Edited sudoers file with visudo, adding the following line at the end:

    www-data ALL=NOPASSWD: /usr/bin/docker
    
  3. Checked http://localhost/index.php and it worked!

enter image description here

You can even build and run containers with this, hope it works for you.

like image 91
galoget Avatar answered Oct 14 '25 23:10

galoget