Over a million developers have joined DZone.

Docker – How to SSH to a Running Container

Learn how to install SSH to a Docker container and how to SSH to other Docker containers.

· Cloud Zone

Download this eBook outlining the critical components of success for SaaS companies - and the new rules you need to play by.  Brought to you in partnership with NuoDB.

This article contains instructions on how you can get a Docker container to connect with other Docker containers using SSH. Please feel free to comment/suggest if I failed to mention one or more important points.

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 SSH it and allow other Docker containers to connect via SSH, the 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

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

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


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
    arr=$(echo ${AUTHORIZED_KEYS} | tr "," "\n")
    for x in $arr
        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

if [ ! -f /.root_pw_set ]; then
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 the 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. The 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

Techniques to SSH to Running Container

Once you've installed SSH on an existing container and exposed port 22 using the steps above, do the following in order to test SSH from another container:

  • Follow above steps to install SSH, configure and expose port 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

I hope you found this article useful and helpful in having one Docker container connect to other Docker containers using SSH. Please feel free to share your comments.

Learn how moving from a traditional, on-premises delivery model to a cloud-based, software-as-a-service (SaaS) strategy is a high-stakes, bet-the-company game for independent software vendors. Brought to you in partnership with NuoDB.

docker ,containers ,ssh ,cloud

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}