how to connect to remote host running in docker container from jenkins running in docker container using ssh

604 Views Asked by At

I have tried these steps a number of time but failing. Did lot of RnD but could not fix the issue.

I am using CentOS running on Oracle VM.

I am trying to connect from CentOS host -> Jenkins -> Remote Host using SSH

My present working directory is : /root/connect2nareshc/jenkins_0_to_hero/jenkins_image I did ssh-keygen -f remote-key to generate public and private keys.

In the directory /root/connect2nareshc/jenkins_0_to_hero/jenkins_image I have Dockerfile as follows:

FROM centos:7

RUN yum -y install openssh-server

RUN useradd remote_user  && \
    echo "1234" | passwd remote_user --stdin && \
    mkdir /home/remote_user/.ssh && \
    chmod 700 /home/remote_user/.ssh

COPY remote-key.pub /home/remote_user/.ssh/authorized_keys

RUN chown remote_user:remote_user -R /home/remote_user/.ssh && \
    chown 600 /home/remote_user/.ssh/authorized_keys

RUN ssh-keygen -A

CMD /usr/sbin/sshd -D

In one directory above in /root/connect2nareshc/jenkins_0_to_hero, I have docker-compose.yml as follows:

version: '3'
services:
  jenkins:
    container_name: jenkins_yml
    image: "jenkins/jenkins:lts"
    ports:
      - 8080:8080
    networks:
      - net
  remote_host:
    container_name: remote-host
    image: remote_host
    build:
      context: /root/connect2nareshc/jenkins_0_to_hero/jenkins_image
    networks:
      - net
networks:
  net:

I execute the following command and it works fine. I.e. From host I connect to Jenkins and from Jenkins I connect to remote_host using password.

docker-compose build
docker-compose up -d
docker exec -it jenkins_yml bash
ssh remote_user@remote_host
#Enter password 1234 when prompted.

When I try to connect using keys, I am not able to:

docker cp remote-key jenkins_yml:/tmp/remote-key
docker exec -it jenkins_yml bash
cd /tmp
ssh -i remote-key remote_user@remote_host

Instead it prompts me to connect with password.

While on remote_host I did ls -altr on /var/log and got following output. I cannot find auth.log

drwxr-xr-x. 1 root root   4096 May  4 15:36 ..
-rw-r--r--. 1 root root    193 May  4 15:36 grubby_prune_debug
-rw-------. 1 root utmp      0 May  4 15:36 btmp
drwxr-xr-x. 1 root root   4096 May  4 15:37 .
-rw-------. 1 root root   1751 Jul 28 16:35 yum.log
-rw-------. 1 root root  64064 Jul 28 16:36 tallylog
-rw-rw-r--. 1 root utmp   1152 Jul 29 03:17 wtmp
-rw-r--r--. 1 root root 292292 Jul 29 03:17 lastlog
0

There are 0 best solutions below