Establish SSH connection from Jenkins container to SSH server container, I can establish with password login but can't establish with private key
-rw------- 1 245867976 1349604816 1679 Sep 18 20:53 remote-key
jenkins@7551f2fa441d:/$ chown jenkins:jenkins /tmp/remote-keychown: changing ownership of '/tmp/remote-key': Operation not permitted
However the chown command tells me operation not permitted. :( What should I do now?
Looks like permission on your SSH key are indeed not set properly. You cannot change the file permission as it's owned by 245867976
but your bash session is ran by jenkins
user.
You should change the file ownership key using root
:
docker exec -it -u root jenkins bash$ chown jenkins:jenkins /tmp/remote-key $ exit
-u
flag will run bash
in jenkins
container as root
user, with which you should be able to change your file permission. Then try again:
docker exec -it jenkins bash$ ssh -i /tmp/remote-key remote_user@remote_host
Note: you can also directly run ssh
with root
without needing to change permissions
docker exec -it -u root jenkins bash$ ssh -i /tmp/remote-key remote_user@remote_host
As far as I understand, you basically want to run commands in other docker container.
That's totally possible. Actually, I've already written an answer on how to establish a ssh-connection between two independent docker containers within the same docker network.
This solution I provided involves
- Static IPs in a docker network (you can also use the hostnames, but I personally prefer static IPs)
- A volume for sharing the the public ssh-key
A note on storing sensitive files in docker: It is advisable not to place keys, certificates whatsoever into the container's file system at all. This can be easily achieved by using docker volumes; and you would simply mount a volume holding keys/containers into the Docker container when launching it.