docker使用ssh遠程連接容器(沒錢買服務器又不想安裝虛擬機患者必備)


突然有需求,需要使用go語言寫個ssh終端連接功能,這時候手上又沒有服務器,虛擬機也沒有,正好使用docker搞起來

docker容器開啟sshd服務,模擬服務器

我們知道docker是可以用exec來直接訪問容器的,但是還不夠high,有時候要模擬服務器的登錄總不能用docker exec吧,來吧,老司機帶你飛!

以centos為例,需要幾步操作

1.安裝openssh-server

2.初始化root用戶密碼

3.開啟sshd服務

廢話不多說,dockerfile獻上

FROM centos
RUN 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 --stdin
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
#RUN systemctl enable sshd
CMD /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.list
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list

#安裝openssh-server
RUN apt-get -y update && apt-get -y upgrade && apt-get install -y openssh-server

#修改默認密碼
RUN echo 'root:88888888' | chpasswd
RUN mkdir -p /run/sshd
# 允許登錄
RUN sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config

RUN  apt-get install -y python vim curl

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

 

hight起來,light weight baby! 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM