I am new to Docker technology,I am trying to create a shell script for setting up a docker container, my script file looks like following
#!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, please feel free to ask if the scenario is not clear.
I was searching an answer for this same question and found ENTRYPOINT in Dockerfile solution for me.
Dockerfile
Now the scripts are executed when I start the container and I get the bash prompt after the scripts has been executed.
You could also mount a local directory into your docker image and source the script in your
.bashrc
. Don't forget the script has to consist of functions unless you want it to execute on every new shell. (This is outdated see the update notice.)I'm using this solution to be able to update the script outside of the docker instance. This way I don't have to rerun the image if changes occur, I just open a new shell. (Got rid of reopening a shell - see the update notice)
Here is how you bind your current directory:
Now your current directory is bound to
/scripts
of your docker instance.(Outdated) To save your
.bashrc
changes commit your working image with this command:Update
To solve the issue to open up a new shell for every change I now do the following:
In the dockerfile itself I add
RUN echo "/scripts/bashrc" > /root/.bashrc"
. Insidezshrc
I export the scripts directory to the path. The scripts directory now contains multiple files instead of one. Now I can directly call all scripts without having open a sub shell on every change.BTW you can define the history file outside of your container too. This way it's not necessary to commit on a bash change anymore.
You can run a command in a running container using
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
:And to run from a bash session:
And from there you can run your script or whatever.
Have a look at entry points too. You will be able to use multiple CMD https://docs.docker.com/engine/reference/builder/#/entrypoint
If you want to run the same command on multiple instances you can do this :
Assuming that your docker container is up and running, you can run commands as: