Home >Backend Development >PHP Tutorial >Why can't I call Docker commands using PHP successfully?
<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
<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