Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run shell script on Host from jenkins docker container?

I know my issue is already discussed in How to run shell script on host from docker container? but i think my issue is a littel bit more complicated.

At first I try to explain my situation. I'm using jenkins 2.x from a docker container in CentOS VM (Host). In jenkins i created a Job which checks out 3 files from SVN (2 Shell scripts and 1 .jar file). these files will be downloaded in jenkins workspace in jenkins docker container and also on host in a mounted directory like that:

 volumes:
  - ${DATA_HOME}/jenkins/data:/var/jenkins_home

One of these scripts will be executed from jenkins job and that executes the other script. The second script checks out a SVN directory and does much more stuffs.

So I want a new mounted volume in that directory all results of executed second script will be placed on Host. I think to connect to the host over 'SSH' and execute the script seems to be fine but how can i do that.

I hope I could explain my issue understandable

like image 906
guguli Avatar asked Oct 16 '25 11:10

guguli


1 Answers

I will answer regarding "I think to connect to the host over 'SSH' and execute the script seems to be fine but how can i do that"

Pass Host machine Ip to your run command.

docker run --name redis --env pass=pass_my --add-host="hostmachine:192.168.1.23" -dit redis

Now,

docker exec -it redis ash

and run this command. This will do SSH from the container to host

ssh user_name@hostmachine 'ls; bash /home/user_name/Desktop/test.sh; docker run --name db  -dit db; docker ps'

If you want something without password then set ssh-key in a container or you can also try

sshpass -p $pass ssh user_name@hostmachine 'ls;/home/user_name/Desktop/test.sh; docker run --name db  -d
it db; docker ps'

or if you want to run the script that is inside container you can also do that just pass the script to ssh.

sshpass -p $pass ssh user_name@hostmachine < ./ab.sh

Note: $pass is password of host from ENV and hostmachine is host the we set during run command.

like image 114
Adiii Avatar answered Oct 19 '25 03:10

Adiii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!