Docker命令通过php shell_exec

时间:2015-07-17 19:25:45

标签: php shell docker ubuntu-14.04

我正在尝试从我的index.php运行命令:

$output = shell_exec('docker images');

然后输出结果,

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

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

似乎我无法通过php运行任何docker cmd。

怎么做得好?

2 个答案:

答案 0 :(得分:3)

我做了以下工作来实现这个目标:

  1. 使用以下内容在 / var / www / html / 上创建一个名为 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并且它有效!

  4. enter image description here

    您甚至可以使用此构建和运行容器,希望它适合您。

答案 1 :(得分:2)

你可以这样做:

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/

这就是全部!

我希望这可以帮到你