Home >Backend Development >PHP Tutorial >Why can't I call Docker commands using PHP successfully?

Why can't I call Docker commands using PHP successfully?

WBOY
WBOYOriginal
2016-07-06 13:52:011516browse

<code><?php

echo system("sudo docker run -t -i loen/hj:v0.0.1 php /root/index.php ruby PD9waHAKZWNobyAiaGVsbG8gcGhwIjsKPz4="); // 这句不能执行 使用docker ps -a 没看到执行成功!

echo system("ls -al");// 这句能执行

?></code>

Results

<code>total 24
drwxr-xr-x 2 root root 4096 Jun 23 16:24 .
drwxr-xr-x 3 root root 4096 Jun 23 15:30 ..
-rw-r--r-- 1 root root    1 Jun 23 15:42 index.html
-rw-r--r-- 1 root root  215 Jun 23 16:24 index.php</code>

It can be executed using php ./index.php
system("sudo docker...")
It cannot be executed using browser access

Reply content:

<code><?php

echo system("sudo docker run -t -i loen/hj:v0.0.1 php /root/index.php ruby PD9waHAKZWNobyAiaGVsbG8gcGhwIjsKPz4="); // 这句不能执行 使用docker ps -a 没看到执行成功!

echo system("ls -al");// 这句能执行

?></code>

Results

<code>total 24
drwxr-xr-x 2 root root 4096 Jun 23 16:24 .
drwxr-xr-x 3 root root 4096 Jun 23 15:30 ..
-rw-r--r-- 1 root root    1 Jun 23 15:42 index.html
-rw-r--r-- 1 root root  215 Jun 23 16:24 index.php</code>

It can be executed using php ./index.php
system("sudo docker...")
It cannot be executed using browser access

It should be caused by insufficient PHP permissions. You can type out the status code returned after system execution and take a look. If you want to see the specific return results, you can use the exec command.

What you execute in the browser is the www user. If you use the PHP command line (PHP CLI) to execute, it is your current user. If the current user has permissions, it can run successfully.

The reason should be insufficient permissions, which has been clearly stated above.

Let me talk about other solutions, that is, PHP can directly use the Docker Remote API to operate docker
You can refer to http://www.open-open.com/lib/view/open1419921028828.html

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn