您的位置:首页 > 房产 > 家装 > 如何通过SSH远程登录到docker容器内部

如何通过SSH远程登录到docker容器内部

2024/10/6 8:35:01 来源:https://blog.csdn.net/fydw_715/article/details/141399736  浏览:    关键词:如何通过SSH远程登录到docker容器内部

在容器中安装和配置SSH服务以进行远程登录

在容器中运行 sshd 服务可以使你通过SSH直接登录到容器内部,这对于调试和管理容器非常有用。本文将介绍如何在容器中安装和配置 sshd 服务,并通过SSH进行登录,同时将改动提交到镜像并推送到阿里云镜像仓库。

步骤一:更新包列表

首先,确保APT包列表是最新的:

apt-get update

输出示例:

Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [128 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
...
Reading package lists... Done

步骤二:安装OpenSSH服务器

安装 openssh-server 包:

apt-get install openssh-server

输出示例:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:...
Need to get 2288 kB of archives.
After this operation, 9878 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...
Setting up openssh-server (1:8.2p1-4ubuntu0.11) ...
...

步骤三:配置SSH服务

编辑 sshd_config 文件,确保允许 root 用户登录并启用密码认证:

vim /etc/ssh/sshd_config

确保以下配置项被设置:

Port 50022
PermitRootLogin yes
PasswordAuthentication yes

步骤四:启动SSH服务

由于容器中可能没有使用 systemd,我们使用 service 命令来启动 sshd 服务:

service ssh start

输出示例:

 * Starting OpenBSD Secure Shell server sshd                                                                                                                       [ OK ] 

确认SSH服务正在运行:

service ssh status

输出示例:

 * sshd is running

步骤五:检查端口监听

安装 net-tools 包以使用 netstat 命令检查端口监听:

apt-get install net-tools

确认 sshd 服务正在监听端口 50022

netstat -tuln | grep 50022

输出示例:

tcp        0      0 0.0.0.0:50022           0.0.0.0:*               LISTEN     
tcp6       0      0 :::50022                :::*                    LISTEN     

步骤六:设置root用户密码

设置 root 用户密码:

passwd root

输入并确认新密码。

步骤七:通过SSH连接到容器

使用SSH连接到容器:

ssh -p 50022 localhost

输出示例:

The authenticity of host '[localhost]:50022 ([::1]:50022)' can't be established.
ECDSA key fingerprint is SHA256:l295L3ZoDTpATH8G+KXWcuannUAXozL5sfxW/+BS1L8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[localhost]:50022' (ECDSA) to the list of known hosts.
root@localhost's password: 
Welcome to Ubuntu 20.04.5 LTS (GNU/Linux 6.5.0-44-generic x86_64)
...

成功登录后,你可以在容器中执行命令。

步骤八:提交改动到新镜像

使用 docker commit 命令将容器的当前状态提交为一个新的镜像:

docker commit ec9206f93167 my_new_image:sshd_installed

步骤九:为新镜像打标签

将新镜像打上阿里云镜像仓库的标签:

docker tag my_new_image:sshd_installed bgil-pai-eas-registry.cn-shanghai.cr.aliyuncs.com/qwen-vl-7b/lmdeploy:sshd_installed

步骤十:登录到阿里云镜像仓库

使用 docker login 命令登录到阿里云镜像仓库:

docker login bgil-pai-eas-registry.cn-shanghai.cr.aliyuncs.com

你需要输入阿里云账号的用户名和密码或者使用阿里云提供的临时登录令牌。

步骤十一:推送新镜像到阿里云镜像仓库

使用 docker push 命令将新镜像推送到阿里云镜像仓库:

docker push bgil-pai-eas-registry.cn-shanghai.cr.aliyuncs.com/qwen-vl-7b/lmdeploy:sshd_installed

完整示例

以下是一个完整的示例,包括所有步骤:

# 确认当前正在运行的容器
docker ps# 提交当前容器的改动为一个新的镜像
docker commit ec9206f93167 my_new_image:sshd_installed# 为新镜像打标签
docker tag my_new_image:sshd_installed bgil-pai-eas-registry.cn-shanghai.cr.aliyuncs.com/qwen-vl-7b/lmdeploy:sshd_installed# 登录到阿里云镜像仓库
docker login bgil-pai-eas-registry.cn-shanghai.cr.aliyuncs.com# 推送新镜像到阿里云镜像仓库
docker push bgil-pai-eas-registry.cn-shanghai.cr.aliyuncs.com/qwen-vl-7b/lmdeploy:sshd_installed

验证推送

推送完成后,你可以登录到阿里云镜像仓库控制台,查看新镜像是否已经成功推送。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com