创建带sshd服务的docker image

系统 1788 0

参考: https://docs.docker.com/examples/running_ssh_service/


1. 创建一个空目录用于存放 Dockerfile

mkdir -p /home/thm/docker/test/new_image

vim Dockerfile

Dockerfile 的内容为:

 

        FROM tanghuimin0713/ubuntu_amd64:
      
        14.04
      
      
        
  RUN apt
      
      -
      
        get
      
      
         update
  RUN apt
      
      -
      
        get
      
       install -y openssh-
      
        server
  #RUN mkdir 
      
      /
      
        var
      
      /run/
      
        sshd
  RUN sed 
      
      -i 
      
        '
      
      
        s/^PermitRootLogin/#PermitRootLogin/
      
      
        '
      
       /etc/ssh/
      
        sshd_config
  RUN echo 
      
      
        "
      
      
        PermitRootLogin yes
      
      
        "
      
       >> /etc/ssh/
      
        sshd_config
  RUN echo 
      
      
        "
      
      
        root:root
      
      
        "
      
       |
      
         chpasswd
  EXPOSE 
      
      
        22
      
    

 

2. build a image

docker build -t tanghuimin0713/ubuntu_amd64_ssh .

3. run a container

docker run -d -p 2222:22 tanghuimin0713/ubuntu_amd64_ssh /usr/sbin/sshd -D

4. 主机下 ssh 登陆 docker 容器

ssh root@localhost -p 2222

密码为: root

      root@ubuntu:/home/thm/docker/test/new_image# ssh root@localhost -p 
      
        2222
      
      
        
The authenticity of host 
      
      
        '
      
      
        [localhost]:2222 ([127.0.0.1]:2222)
      
      
        '
      
       can
      
        '
      
      
        t be established.
      
      
ECDSA key fingerprint 
      
        is
      
       bd:
      
        41
      
      :3f:e4:e3:bd:b4:
      
        46
      
      :
      
        54
      
      :fe:1b:
      
        46
      
      :eb:
      
        60
      
      :f3:
      
        73
      
      
        .
Are you sure you want to 
      
      
        continue
      
       connecting (yes/no)?
      
         yes
Warning: Permanently added 
      
      
        '
      
      
        [localhost]:2222
      
      
        '
      
      
         (ECDSA) to the list of known hosts.
root@localhost
      
      
        '
      
      
        s password: 
      
      
Welcome to Ubuntu 
      
        14.04
      
       LTS (GNU/Linux 
      
        3.13
      
      .
      
        0
      
      -
      
        32
      
      -
      
        generic x86_64)

 
      
      * Documentation:  https:
      
        //
      
      
        help.ubuntu.com/
      
      
        
The programs included with the Ubuntu system are free software;
the exact distribution terms 
      
      
        for
      
       each program are described 
      
        in
      
      
         the
individual files 
      
      
        in
      
       /usr/share/doc
      
        /*
      
      
        /copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@db36812760ac:~# 
      
    

成功。

若在 ssh 登陆时提示:

 

      
        @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED
      
      !
      
             @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY
      
      !
      
        
Someone could be eavesdropping on you right now (man
      
      -
      
        in
      
      -the-middle attack)!
      
        
It 
      
      
        is
      
      
         also possible that a host key has just been changed.
The fingerprint 
      
      
        for
      
       the ECDSA key sent by the remote host 
      
        is
      
      
        
bd:
      
      
        41
      
      :3f:e4:e3:bd:b4:
      
        46
      
      :
      
        54
      
      :fe:1b:
      
        46
      
      :eb:
      
        60
      
      :f3:
      
        73
      
      
        .
Please contact your system administrator.
Add correct host key 
      
      
        in
      
       /root/.ssh/known_hosts to 
      
        get
      
       rid of 
      
        this
      
      
         message.
Offending ECDSA key 
      
      
        in
      
       /root/.ssh/known_hosts:
      
        5
      
      
        
  remove with: ssh
      
      -keygen -f 
      
        "
      
      
        /root/.ssh/known_hosts
      
      
        "
      
       -R 
      
        172.17
      
      .
      
        0.2
      
      
        
ECDSA host key 
      
      
        for
      
      
        172.17
      
      .
      
        0.2
      
      
         has changed and you have requested strict checking.
Host key verification failed.
      
    

 

rm $HOME/.ssh/known_hosts 即可。

 

 

创建带sshd服务的docker image


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论