Running a script inside a docker container using shell script

zappy picture zappy · Jul 23, 2015 · Viewed 234.8k times · Source

I am trying to create a shell script for setting up a docker container. My script file looks like:

#!bin/bash

docker run -t -i -p 5902:5902 --name "mycontainer" --privileged myImage:new /bin/bash

Running this script file will run the container in a newly invoked bash.

Now I need to run a script file (test.sh)which is already inside container from the above given shell script.(eg: cd /path/to/test.sh && ./test.sh) How to do that?

Answer

Javier Cortejoso picture Javier Cortejoso · Jul 23, 2015

You can run a command in a running container using docker exec [OPTIONS] CONTAINER COMMAND [ARG...]:

docker exec mycontainer /path/to/test.sh

And to run from a bash session:

docker exec -it mycontainer /bin/bash

From there you can run your script.