Docker – How to SSH to Running Container

This article represents instructions on how you could get a docker container connect with other docker container using SSH. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.

Following are the key points described later in this article:

  • Instructions to Install SSH
  • Techniques to Enable SSH on the Existing Container
  • Techniques to SSH to Running Container
Instructions to Install SSH

If you already have a running container, and you would like to put SSH on it and allow other docker container to connect via SSH, following is a set of instructions to install SSH:

## 
## Install the openssh-server and epel-release
##

yum -y install openssh-server epel-release
yum -y install pwgen
rm -f /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_rsa_key
ssh-keygen -q -N "" -t dsa -f /etc/ssh/ssh_host_ecdsa_key
ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key 
sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
sed -i "s/UsePAM.*/UsePAM yes/g" /etc/ssh/sshd_config
ssh-keygen -A

##
## Create Set Root Password Script. Name it as set_root_pw.sh. Save it in a folder
##

#!/bin/bash
if [ -f /.root_pw_set ]; then
	echo "Root password already set!"
	exit 0
fi

PASS=${ROOT_PASS:-$(pwgen -s 12 1)}
_word=$( [ ${ROOT_PASS} ] && echo "preset" || echo "random" )
echo "=> Setting a ${_word} password to the root user"
echo "root:$PASS" | chpasswd

echo "=> Done!"
touch /.root_pw_set

echo "========================================================================"
echo "You can now connect to this CentOS container via SSH using:"
echo ""
echo "    ssh -p  root@"
echo "and enter the root password '$PASS' when prompted"
echo ""
echo "Please remember to change the above password as soon as possible!"
echo "========================================================================"

##
## Create run.sh file with following content and save it in same folder as the above
## set_root_pw.sh
##

#!/bin/bash

if [ "${AUTHORIZED_KEYS}" != "**None**" ]; then
    echo "=> Found authorized keys"
    mkdir -p /root/.ssh
    chmod 700 /root/.ssh
    touch /root/.ssh/authorized_keys
    chmod 600 /root/.ssh/authorized_keys
    IFS=$'\n'
    arr=$(echo ${AUTHORIZED_KEYS} | tr "," "\n")
    for x in $arr
    do
        x=$(echo $x |sed -e 's/^ *//' -e 's/ *$//')
        cat /root/.ssh/authorized_keys | grep "$x" >/dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo "=> Adding public key to /root/.ssh/authorized_keys: $x"
            echo "$x" >> /root/.ssh/authorized_keys
        fi
    done
fi

if [ ! -f /.root_pw_set ]; then
	/set_root_pw.sh
fi
exec /usr/sbin/sshd -D

If you do not have yum installed on your docker, download it using wget. Alternatively, the above works fine on Centos containers.

Techniques to Enable SSH on the Existing Container

Once done with above, it is time to run the SSH Daemon.

  • Go to the folder consisting of above created files such as set_root_pw.sh and run.sh
  • Change the mode using following command: chmod +x ./*.sh
  • Execute the run.sh script by executing it on the shell prompt: ./run.sh
  • It is advisable to run it using nohup such that sshd runs in the background.

Once done with above, it is time to expose port 22 from the container. Following is how you would expose the 22 port:

  • Exit from the container
  • Commit the docker container image using command: docker commit <container_name> <some_image_name>
  • Run a new container using the committed image using following command: docker run -ti -d -P -p 22:22 –name <new_container_name> -v /c/Users:/mnt/Users <new_image_name_saved> /bin/bash
  • That is it.

 

Techniques to SSH to Running Container

Once installed SSH on an existing container and exposed 22 using the steps mentioned, do the following in order to test SSH from another container:

  • Follow above steps to install SSH, configure and expose 22
  • If you want to connect without having need to enter password, execute the following command:
    • ssh-keygen -t rsa
    • cat ~/.ssh/id_rsa.pub | ssh <usernameof other docker container>@<ip of other docker container> ‘cat >> .ssh/authorized_keys && echo “Key copied”‘
    • Executing above should print “Key Copied”

Once done with above, go ahead and test SSH connection using SCP:

  • scp /tmp/somefile.txt <usernameof other docker container>@<ip of other docker container>:/tmp
  • Executing above would send the file to /tmp folder of other docker container

Hope you found this article useful and helpful in havingĀ one docker container connect to other docker container using SSH. Please feel free to share your comments.

Ajitesh Kumar

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.
Posted in Virtualization. Tagged with , .