Docker Community Forums

Share and learn in the Docker community.

How to trigger a script inside the running container from the host?


(Sajithgem) #1

Looking for a way in which i will be able to trigger a script on demand from the host inside a running docker container., ?

similar what we have for mysql

mysql --user=root --password=xxxxxx -e "source dbscript.sql"
which can run mysql queries from the host machine.


(Andy Rothfusz) #2

This is an incredibly general question – it will be hard to give you a quick answer.

In your mysql example, I assume mysql is running in the container. So you’re connecting to the mysql service in the container via the mysql client. The client is specially built to know how to connect over a port and then pass the script to the mysql daemon/service, which then runs it.

To get similar functionality, you’ll need to start a daemon or service in your “running docker container” and communicate with it in some way. Do you need to pass parameters to the script in the container? How do you want to communicate with what’s happening in the container? What do you expect as output? Will you need a full terminal? Here are some possibilities:

  • use unix signals. You can send a signal to process 1 inside the container by using docker kill. Your daemon could listen for different signals to run different scripts. The output could go to the docker log.
  • use a tcp port. You could run a web server like nginx or some of the simple node, ruby or python servers, and communicate through a web interface or by sending HTTP via curl. The output would go back through the HTTP connection.
  • run an additional process. You can use docker exec to start up a new process in the container to run your script. Output could go to the command line where you run it.
  • use the passage of time. With a cron job, your script runs automatically on a schedule you’ve defined, so you don’t need to trigger it from the outside. Output would go to docker logs.

(Sajithgem) #3

Thanks @rufus
Docker exec approach will do good in my case…, To Run
Docker exec container-name bash -l c script full path > Redirect output to a file> and the output i get to the Volume Mounted location.