How to i execute the command inside docker container using shell script?

how to i execute the command inside docker container using shell script?
where we need to put that shell script?

Hi madhuchilipi,
You need to copy your script over to the container in your Dockerfile.
use COPY or ADD which ever is best for you to copy your local script to a place in the container.
Your script will contains the command you want to run.
Then with CMD or ENTRYPOINT you will be running the script .
If I can add a suggestion, if you command is simple you might not need a script for that. Just provide it in CMD or ENTRYPOINT directly.
Caution: Make sure what you are hoping to run exists in the container by default. You might need to install the executables of your command. Or you will have an error such as: command not found.

1 Like

If you don’t need to have it permanently inside the container, you can user docker cp to get any file to/from container. May have to adjust permissions to make it executable.

To exec in as the user the container is running as:
docker exec -it containerid-here bash

To exec in as root user:
docker exec -it --user=root containerid-here bash

Once inside, you can do whatever you need.
Of course, if your image has a different shell, you can specify it instead of bash.

thank you for your Reply.

Here is my script:

containerID= sudo docker ps | awk ‘$2 ~ /stackstorm/’ | awk ‘NR==1{print $1}’
echo $containerID
#cd /u02/Deployment_DATE
sudo docker cp /home/servicesoasit/st2clreload.sh $containerID:/opt/stackstorm/
sudo docker exec -it $containerID /bin/bash /opt/stackstrom/st2clreload.sh

when i run this shell script the following output i am getting.

[servicesoasit@ausulsoaapp56 ~]$ sh container_refresh.sh
38476806e453

must specify at least one container source
Error: No such container: /bin/bash

its displaying the container id but not picking when i use $contaierID

regards,
Madhu

Try this:
containerID= sudo docker ps | awk ‘$2 ~ /stackstorm/’ | awk ‘NR==1{print $1}’
echo ${containerID}
#cd /u02/Deployment_DATE
sudo docker cp /home/servicesoasit/st2clreload.sh ${containerID}:/opt/stackstorm/
sudo docker exec -it ${containerID} /opt/stackstrom/st2clreload.sh

Note I added curly brackets around the variable and dropped the /bin/bash since you are just running the script.

1 Like

Thank you gary, when i tried to print that Container iD its passing null,

Here is the Result:

38476806e453

sudo docker cp /home/servicesoasit/st2clreload.sh :/opt/stackstorm/
“docker exec” requires at least 2 arguments.
See ‘docker exec --help’.

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG…] [flags]

its normally publishing the containerID, but not able to invoke that value to docker command.

Regards,
Madhu

Try wrapping your sudo lines with single ticks.

Sorry, I meant back ticks not regular ticks.

You can try and give your container a static-name ( with the --name option in your docker run command )
then you can use docker exec -ti THE-NAME-YOU-PICKED /bin/bash …

2 Likes

where do i run in, like i am downloading with convenience script installation it says to run this command but where i tried to run it on my terminal but it did not work. im new