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

5
  • 1
    please mount another volume during docker run and use that path in your script Commented Oct 22, 2018 at 9: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. Commented Oct 23, 2018 at 6:46
  • @guguli do you tried my answer> Commented Oct 23, 2018 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. Commented Oct 23, 2018 at 11:13
  • @guguli going to add this as the answer, this was the fastest way to achieve that. I nearly missed it out. Commented Dec 12, 2019 at 8:52

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.