Docker命令通过php shell_exec

我试图从我的index.php运行命令:

$output = shell_exec('docker images'); 

然后输出结果,

或以相同的方式运行新的容器:

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

看来,我不能通过PHP运行任何dockerCMD。

如何正确?

你可以这样做:

 vi rd.php 

把这个内容放在rd.php文件中

 <?php $output = shell_exec('RET=`docker run hello-world`;echo $RET'); echo $output; 

现在你可以跑了

 php rd.php 

您可以查看结果:

 Hello from Docker. This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (Assuming it was not already locally available.) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash For more examples and ideas, visit: http://docs.docker.com/userguide/ 

就这样 !

我希望这可以帮助你

我做了以下工作:

  1. / var / www / html /下创build一个名为index.php的php文件,内容如下:

     <?php echo '<pre>'; $content = system('sudo docker images', $ret); echo '</pre>'; ?> 
  2. 使用visudo编辑sudoers文件,在末尾添加以下行:

     www-data ALL=NOPASSWD: /usr/bin/docker 
  3. 检查http://localhost/index.php ,它的工作!

在这里输入图像说明

你甚至可以用这个来创build和运行容器,希望它适合你。