closed
logo logo
关于我们

技术分享

技术分享 docker使用ssh远程连接容器

docker使用ssh远程连接容器

2016-12-26

突然有需求,需要使用go语言写个ssh终端连接功能,这时候手上又没有服务器,虚拟机也没有,正好使用docker搞起来

docker容器开启sshd服务,模拟服务器

我们知道docker是可以用exec来直接访问容器的,但是还不够high,有时候要模拟服务器的登录总不能用docker exec吧,来吧,老司机带你飞!

以centos为例,需要几步操作

1.安装openssh-server

2.初始化root用户密码

3.开启sshd服务

废话不多说,dockerfile献上

FROM centosRUN yum install -y wget && \
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo  && \yum install -y passwd && \
yum install -y openssh-server  ssh-keygen && \
echo 'abcd1234' | passwd root --stdinRUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" -q && \
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" -q && \
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" -q
#RUN systemctl enable sshdCMD /usr/sbin/sshd && tail -f /var/log/wtmp

简单解释一下,

  • 安装openssh-server

yum install -y wget && \
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo  && \yum install -y passwd && \
yum install -y openssh-server  ssh-keygen
  • 修改root密码为88888888

echo '88888888' | passwd root --stdin
  • 创建ssh-keygen创建相关的ssh文件,-q的意思是静默模式(就是默认是需要让你回车输入的,加上这个直接跳过)

RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" -q && \
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" -q && \
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" -q
  • 开启sshd服务,并用tail来前台执行阻止docker容器退出 

CMD /usr/sbin/sshd && tail -f /var/log/wtmp

 

一、构建镜像

Dockerfile目录下执行,yeah,就是chenqionghe/centos镜像,你也可以弄成自己的,例如muscle/lightwegiht

docker build -t chenqionghe/centos .

二、运行容器

docker run --name centos_ssh -p 2222:22 -it chenqionghe/centos  

三、使用ssh连接容器

 这里使用了2222端口来映射容器里的22端口,运行起来就可以使用ssh连接了,输出设置好的88888888密码,注意,这里用的是2222映射的端口  

➜  ~ ssh root@127.0.0.1 -p 2222
root@127.0.0.1's password:
Last login: Tue Nov 20 04:10:17 2018 from 172.17.0.1
[root@a8c8e0fbd74f ~]# ls -l /
total 56
-rw-r--r--   1 root root 12030 Oct  6 19:15 anaconda-post.log
lrwxrwxrwx   1 root root     7 Oct  6 19:14 bin -> usr/bin
drwxr-xr-x   5 root root   360 Nov 20 04:09 dev
drwxr-xr-x  54 root root  4096 Nov 20 04:09 etc
drwxr-xr-x   2 root root  4096 Apr 11  2018 home
lrwxrwxrwx   1 root root     7 Oct  6 19:14 lib -> usr/lib
lrwxrwxrwx   1 root root     9 Oct  6 19:14 lib64 -> usr/lib64
drwxr-xr-x   2 root root  4096 Apr 11  2018 media
drwxr-xr-x   2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x   2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x 223 root root     0 Nov 20 04:09 proc

以上就是使用docker开启ssh模拟服务器的全过程,

附上ubuntu的Dockerfile,原理大同小异

FROM ubuntu:18.04# Ali apt-get source.listRUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo "deb

#安装openssh-serverRUN apt-get -y update && apt-get -y upgrade && apt-get install -y openssh-server

#修改默认密码RUN echo 'root:88888888' | chpasswdRUN mkdir -p /run/sshd
# 允许登录RUN sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_configRUN  apt-get install -y python vim curlCMD /usr/sbin/sshd -D && tail -f /var/log/wtmp

 

hight起来,light weight baby! 

云祺备份软件,云祺容灾备份系统,虚拟机备份,数据库备份,文件备份,实时备份,勒索软件,美国,图书馆
  • 标签:
  • 技术分享

您可能感兴趣的新闻 换一批

现在下载,可享30天免费试用

立即下载

请添加好友为您提供支持
jia7jia_7

请拨打电话
为您提供支持

400-9955-698