1

Well, I need to run a Docker using a PHP function. I have a web page where pushing a link I execute a shell order using shell_exec or exec. This works for me if the execution is like an ls or something that expects a result. The problem is that if the command is to run the Docker (or for example a ping) it doesn't work.

What I want is when the user clicks the link, the shell will execute a command to run Docker in the browser, and the page will be redirected there.

For exemple, if I use shell_exec('firefox'); this should open a new firefox browser, but it doesn't work. It seems that the browser is opened but few seconds later is closed.

This is the Docker execution that doesn't work.

public function executeDocker() {
      $result = shell_exec('docker run --rm -p 3838:3838 shiny/gsva_interactive /usr/bin/shiny-server.sh');

      echo "<br><br>Execution: ".$result;
    }
2
  • why not add a few things at the end, like `'docker run --rm -p 3838:3838 shiny/gsva_interactive /usr/bin/shiny-server.sh ; tail /var/log/xxx'? Commented Jun 13, 2017 at 9:18
  • The command works propertly, is the PHP function that does not work. Commented Jun 13, 2017 at 9:21

3 Answers 3

3

i was having the same issue running docker exec via shell_exec. shell_exec('docker exec -it containerid /usr/bin/cmd);

Getting rid of the -i option worked for me.

Sign up to request clarification or add additional context in comments.

Comments

1

shell_exec will only return the output of a, in this case Docker, command only when the command has exited completely. In the case of ping (it will just keep pinging) and probably in the case of your Docker image, the process will never exit by itself, so it will never give a response.

Using passthru instead of shell_exec should give you the commandline output of your Docker script right back as a response.

If the Docker container is not meant to exit you should probably start it in detached mode with $containerId = shell_exec('docker run -d --rm -p 3838:3838 shiny/gsva_interactive /usr/bin/shiny-server.sh'), so the docker run command will exit. This will return the container id, which you can use with $result = shell_exec("docker ps -f \"id=$containerId\"") to check if the container is running correctly and redirect the user if it is.

4 Comments

I don't want the output, I just need to know that the Docker is executing correctly and find a way to control the execution.
Can you confirm that the Docker container exits? Because the point of my answer is that shell_exec will never give you any output if the Docker container never exits. Ping will also never exit with the default settings, so that is why ls works and ping does not.
I updated my question based on the assumption that you do not want the Docker container to exit.
I just tried your answer but it does not work. I don't know why the Docker is not executed and the container is not listed when I use docker ps -a.
0

Finally I solved it. The problem was in the user group and permissions. In the system that I was using, CentOS, apache server uses a user called apache. This user needs to be in the docker group and reboot the services.

Now it works. Thanks to everyone who helped me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.