Docker——如何通过 SSH 连接到正在运行的容器

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡/ 赠书活动

目前,正在 星球 内带小伙伴们做第一个项目:全栈前后端分离博客项目,采用技术栈 Spring Boot + Mybatis Plus + Vue 3.x + Vite 4手把手,前端 + 后端全栈开发,从 0 到 1 讲解每个功能点开发步骤,1v1 答疑,陪伴式直到项目上线,目前已更新了 204 小节,累计 32w+ 字,讲解图:1416 张,还在持续爆肝中,后续还会上新更多项目,目标是将 Java 领域典型的项目都整上,如秒杀系统、在线商城、IM 即时通讯、权限管理等等,已有 870+ 小伙伴加入,欢迎点击围观

本文包含有关如何让 Docker 容器使用 SSH 与其他 Docker 容器连接的说明。如果我没有提到一个或多个要点,请随时发表评论/建议。

以下是本文后面描述的要点:

  • 安装 SSH 的说明
  • 在现有容器上启用 SSH 的技术
  • SSH 到正在运行的容器的技术

安装 SSH 的说明

如果您已经有一个正在运行的容器,并且您希望通过 SSH 连接它并允许其他 Docker 容器通过 SSH 连接,以下是一组安装 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


如果您的 docker 上没有安装 yum,请使用 wget 下载它。或者,以上在 CentOS 容器上运行良好。


在现有容器上启用 SSH 的技术

完成上述操作后,就该运行 SSH 守护进程了。

  • 转到由上面创建的文件组成的文件夹,例如 set_root_pw.sh 和 run.sh
  • 使用以下命令更改模式: chmod +x ./*.sh
  • 在 shell 提示符下执行 run.sh 脚本: ./run.sh
  • 建议使用 nohup 运行它,以便 sshd 在后台运行。

完成上述操作后,就可以从容器中公开端口 22 了。以下是公开 22 端口的方式:

  • 从容器中退出
  • 使用命令提交 Docker 容器镜像: docker commit
  • 使用以下命令使用提交的图像运行新容器:

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


SSH 到正在运行的容器的技术

在现有容器上安装 SSH 并使用上述步骤公开端口 22 后,请执行以下操作以从另一个容器测试 SSH:

  • 按照上述步骤安装 SSH,配置并公开端口 22
  • 如果不想输入密码就可以连接,执行以下命令:
    • ssh-keygen -t rsa
    • cat ~/.ssh/id_rsa.pub | ssh @ 'cat >> .ssh/authorized_keys && echo “Key copied”' cat ~/.ssh/id_rsa.pub | ssh @ 'cat >> .ssh/authorized_keys && echo “Key copied”' cat ~/.ssh/id_rsa.pub | ssh @ 'cat >> .ssh/authorized_keys && echo “Key copied”'
    • 执行上面应该打印“Key Copied”

完成上述操作后,继续使用 SCP 测试 SSH 连接:

  • scp /tmp/somefile.txt <其他docker容器的用户名>@<其他docker容器的ip>:/tmp
  • 上面的执行会将文件发送到其他 docker 容器的 /tmp 文件夹

我希望您发现这篇文章对使用 SSH 将一个 Docker 容器连接到其他 Docker 容器很有帮助 请随时分享您的意见。