Docker 通过 php 命令 shell_exec

Docker commands via php shell_exec

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

$output = shell_exec('docker images');

然后输出结果,

或运行新容器同理:

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

看来我无法通过 php.运行 任何 docker cmd。

如何正确操作?

你可以这样做:

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/[=33= 上创建了一个名为 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 并且有效!

您甚至可以用它构建 运行 容器,希望它对您有用。