Docker Community Forums

Share and learn in the Docker community.

How to execute "docker run" from a web site?

(Rferroni) #1


I would like to know how to create a docker instance from a web site.
Using a button in a html form, I`m thinking to use a php code like this:

<?php $output = shell_exec('RET=`docker images`;echo $RET'); echo $output; ?>

Of course I will replace the command for “docker run …”

There is a better way to do this ?
I found some issues with permissions between docker and www-data who actually run the command.


(Michael Davis) #2

If you’re on Linux or OSX, then you can allow the www-data user to run docker commands. See here for details:

Then your code should work.

(Rferroni) #3

Hi Michel!
I added the docker group to the www-data user and still not working.
As you said I`m using ubuntu, and if I run #php some.php it works fine in command line. And if a enter on the web site https://someurl/info.php I got the phpinfo(). So php with nginx is also working fine.
But if I try https://someurl/some.php nothing happend :slight_frown:

Thanks for your help!

(Michael Davis) #4

It will be hard for me to help without sitting at your computer - there could be any number of reasons why it doesn’t work, but here’s something you could try.

I don’t know php very well but I do remember that there’s a way to turn on debugging so that you get a detailed error page when something goes wrong.

Maybe php has a limited path and can’t see the docker binary. Do:

which docker

and if it’s in, say /usr/local/bin/docker, then use that full path:

php_exec(’/usr/local/bin/docker run … ');

(Rferroni) #5

I tried with the full path, in my case is /usr/bin/docker but still not working.
I will find how to debug php to see what is happening!

Thanks again for your help!

If I find something I will post it!

(Rferroni) #6

Hi! It works!!!

I tried some php exec before I get what was the problem:

<?php echo exec('whoami'); ?>

The result was “www-data” that confirm the user.
And then:

<?php echo exec('groups'); ?>

That shows only “www-data” because when I add the group “docker” to the user “www-data” it still not was confirm. I reboot the machine and now it shows “www-data docker”!!!

So now is working properly :slight_smile:

Thanks for your help!

(Ryan Kennedy) #7