4

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

guguli
  • 249
  • 3
  • 7
  • 16
  • 1
    please mount another volume during docker run and use that path in your script – Ijaz Ahmad Oct 22 '18 at 09:13
  • What do you exactly mean? I use that volume `- /opt/vfoms-central-test-environments:/opt/vfoms` and i want to execute the shell script in `/opt/vfoms-central-test-environments` from docker container. – guguli Oct 23 '18 at 06:46
  • @guguli do you tried my answer> – Adiii Oct 23 '18 at 10:09
  • 1
    @Adiii no i didn't. I have installed ssh plug in in jenkins, with that can do it. It is easier. – guguli Oct 23 '18 at 11:13
  • @guguli going to add this as the answer, this was the fastest way to achieve that. I nearly missed it out. – peeyush singh Dec 12 '19 at 08:52

2 Answers2

2

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.

Adiii
  • 54,482
  • 7
  • 145
  • 148
0

Based on comments in ans:

We can simply install any SSH plugin (SSH) or (Publish over SSH) and it will work after providing username/password.

Only thing to watch out is that host name resolution does not work and we will need to provide an IP address.

As pointed out this is not the best approach, but sometimes in migration from older systems, we need to move one step at a time and this is the easiest step to take.

peeyush singh
  • 1,337
  • 1
  • 12
  • 23