DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Docker Model Runner: Streamlining AI Deployment for Developers
  • A Guide to Container Runtimes
  • Gemma 3: Unlocking GenAI Potential Using Docker Model Runner
  • Docker vs Kubernetes: Which to Use and When?

Trending

  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  • AI-Based Threat Detection in Cloud Security
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Docker – How to SSH to a Running Container

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.

By 
Ajitesh Kumar user avatar
Ajitesh Kumar
·
Aug. 03, 15 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
61.2K Views

Join the DZone community and get the full member experience.

Join For Free

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
##

#!/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 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   
  • 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 @ ‘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.

Docker (software)

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

Opinions expressed by DZone contributors are their own.

Related

  • Docker Model Runner: Streamlining AI Deployment for Developers
  • A Guide to Container Runtimes
  • Gemma 3: Unlocking GenAI Potential Using Docker Model Runner
  • Docker vs Kubernetes: Which to Use and When?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!