版权声明:本文由姚俊刚原创文章,转载请注明出处: 
文章原文链接:https://www.qcloud.com/community/article/84

来源:腾云阁 https://www.qcloud.com/community

Docker 官方网站上给出的示例里面有个 用 Dockerfile 构建 SSH Server 的例子, 我在腾讯云的主机上实验了一下, 中间添加了一些优化, 把实验过程记录如下, 希望对大家有帮助.

一. 相关的文件

1. 新建一个目录和一个 Dockerfile

mkdir y109-sshd
vim Dockerfile

2. Dockerfile 的内容如下

# docker sshd
FROM ubuntu:14.04
MAINTAINER y109<y109@qq.com> # 使用 163.com 的源
COPY sources.list.163.txt /etc/apt/sources.list
RUN apt-get -y update # 设置 root 密码
RUN echo 'root:bMg5kesfdsfesx9gD' | chpasswd # 安装 openssh-server
RUN apt-get -y install openssh-server
RUN mkdir /var/run/sshd # SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
#
# ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile # 添加公钥(如果没有公钥可以省略)
RUN mkdir /root/.ssh
RUN echo 'ssh-rsa YOU_PUB_KEY' > /root/authorized_keys # 容器启动后运行的程序
CMD ["/usr/sbin/sshd", "-D"] # 打开 22 端口
EXPOSE 22

sources.list.163.txt 的内容如下

deb http://mirrors.163.com/ubuntu/ precise main restricted
deb-src http://mirrors.163.com/ubuntu/ precise main restricted deb http://mirrors.163.com/ubuntu/ precise-updates main restricted
deb-src http://mirrors.163.com/ubuntu/ precise-updates main restricted deb http://mirrors.163.com/ubuntu/ precise universe
deb-src http://mirrors.163.com/ubuntu/ precise universe
deb http://mirrors.163.com/ubuntu/ precise-updates universe
deb-src http://mirrors.163.com/ubuntu/ precise-updates universe deb http://mirrors.163.com/ubuntu/ precise-security main restricted
deb-src http://mirrors.163.com/ubuntu/ precise-security main restricted
deb http://mirrors.163.com/ubuntu/ precise-security universe
deb-src http://mirrors.163.com/ubuntu/ precise-security universe

二.构建 Image

使用 docker build 来生成镜像
-t 参数是给这个镜像的 TAG

sudo docker build -t 'y109/sshd' ./
Sending build context to Docker daemon 4.608 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:14.04
---> 9cbaf023786c
Step 1 : MAINTAINER y109<y109@qq.com>
---> Using cache
---> 2256ab1cc931
Step 2 : COPY sources.list.163.txt /etc/apt/sources.list
---> Using cache
---> 65536ca26964
Step 3 : RUN apt-get -y update
---> Using cache
---> 60639e42f098
Step 4 : RUN echo 'root:pass123456' | chpasswd
---> Using cache
---> 8644dd20854f
Step 5 : RUN apt-get -y install openssh-server
---> Using cache
---> 98039327bca7
Step 6 : RUN mkdir /var/run/sshd
---> Using cache
---> 9bd3b3fc7828
Step 7 : RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
---> Using cache
---> d748cb9428a0
Step 8 : RUN echo "export VISIBLE=now" >> /etc/profile
---> Using cache
---> e975cd819243
Step 9 : RUN mkdir /root/.ssh
---> Using cache
---> e561acc07675
Step 10 : RUN echo 'ssh-rsa YOU_PUBLIC_KEY'
---> Using cache
---> 8f6882a72037
Step 11 : CMD ["/usr/sbin/sshd", "-D"]
---> Using cache
---> 48cbd2c4aa70
Step 12 : EXPOSE 22
---> Using cache
---> 3101a36f0084
Successfully built 3101a36f0084

使用 docker images 命令查看镜像, 确认镜像构建成功了

sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
y109/sshd latest 3101a36f0084 22 minutes ago 226.1 MB
<none> <none> 23f604e547b8 28 minutes ago 226.1 MB
<none> <none> 50647a1fb746 36 minutes ago 226.1 MB
y
...

y109/sshd就是我们刚才构建的镜像

三.创建 Container

使用 docker run 来用镜像创建一个 Container

-d : Detached mode, 使 Container 在 background 模式运行
-p : 把 22 端口映射到主机的网卡上, 格式: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort
–name : 给 Container 指定一个名字, 一旦指定了名称这个名称就和这个 Container 绑定了, 可以用 docker ps -a 列出来

sudo docker run -d -p 10922:22 --name y109-sshd y109/sshd
我用的外网端口是 10922, 可以根据需要修改, 下一步需要确认 Container 是否正常执行了

sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc37b83d343e y109/sshd:latest "/usr/sbin/sshd -D" 9 seconds ago Up 9 seconds 0.0.0.0:10922->22/tcp y109-sshd

看来执行成功了, 连接试试看看

ssh root@localhost -p10922
The authenticity of host '[localhost]:10922 ([127.0.0.1]:10922)' can't be established.
ECDSA key fingerprint is 4d:48:5c:61:54:d6:8f:62:70:a2:0e:ab:b7:1a:cb:f7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:10922' (ECDSA) to the list of known hosts. 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@80f07ad418fe:~#

已经成功连接进入 Container 了

四.关闭 Container

sudo docker stop fc3 fc3 是 Container Id fc37b83d343e 的缩写, 只要能够唯一标识这个 Container 就可以了。或者sudo docker stop y109-sshd

五.运行 Container

sudo docker start y109-sshd

 

Dockerfile分享之SSH Server的更多相关文章

  1. windows 7 ssh server for scp

    Software: BvSshServe. (个人用免费,商业收费) scp localfile.txt user_tst@11.111.12.170:'E:\downloads\SSH\auto.p ...

  2. The remote SSH server rejected X11 forwarding request

    两台相同的虚拟机,一台没有错误,一个经常出现警告,内容如下所示: The remote SSH server rejected X11 forwarding request 找了很多方法,最后发现是安 ...

  3. Ubuntu配置ssh server

    SSH-Server配置指南 一.SSH简介 SSH (Secure Shell)是一个应用程序中提供安全通信的协议,通过SSH协议可以安全地访问服务器,因为SSH 具有成熟的公钥加密体系,在数据进行 ...

  4. MobaSSH SSH server for Windows - Download Home Edition

    MobaSSH SSH server for Windows - Download Home Edition undefined

  5. 在fedora 20下使用ssh server

    在红帽和centos下,一般安装完后会自带ssh,然后可以通过/etc/init.d/sshd start的方式运行,但是在Fedora 20下,系统改用了另外一套服务开启机制. 首先安装ssh se ...

  6. Centos更换yum源,安装ssh server

    先连上网,然后更换yum源 1. 新建的用户没有sudo权限,所以首先切换到root用户su -输入密码 2. 备份之前的yum源mv /etc/yum.repos.d/CentOS-Base.rep ...

  7. 转-How to install an SSH Server in Windows Server 2008

    window也可以通过ssh客户端连接,具体方式参考下面 1 How to install an SSH Server in Windows Server 2008 2 freeSSHd and fr ...

  8. Xshell报错“The remote SSH server rejected X11 forwarding request.”

    Xshell报错“The remote SSH server rejected X11 forwarding request.” 2012年12月17日 ⁄ Linux⁄ 共 218字 ⁄ 字号 小  ...

  9. Xshell 连接虚拟机出现 "The remote SSH server rejected X11 forwarding request"

    1. 描述 虚拟机:VirtualBox Linux: centOS7 解决了 centOS7在VirtualBox中装好后的网络连接问题 后,用 Xshell 连接服务器时出现下面情况: 2. ss ...

随机推荐

  1. C/C++捕获段错误,打印出错的具体位置(精确到哪一行)_转

    转自:C/C++捕获段错误,打印出错的具体位置(精确到哪一行) 修订:2013-02-16 其实还可以使用 glibc 的 backtrace_symbols 函数,把栈帧各返回地址里面的数字地址翻译 ...

  2. 官网下载到离线的Adobe Acrobat Reader DC

    Adobe 官方 FTP :ftp://ftp.adobe.com/ Adobe Acrobat Reader DC 下载目录:ftp://ftp.adobe.com/pub/adobe/reader ...

  3. 【C】——回调函数实现泛型算法

    回调函数的一个典型应用就是实现类似C++的泛型算法(Generics Algorithm).下面实现的max函数可以在任意一组对象中找出最大值,可以是一组int.一组char或者一组结构体,但是实现者 ...

  4. js学习笔记28----事件默认行为

    事件默认行为 : 当一个事件发生的时候浏览器自己会默认做的事情.   怎么阻止? 当前这个行为是什么事件触发的,然后在这个事件的处理函数中使用return false;   右键菜单事件 oncont ...

  5. laravel bald视图控制流与子视图

    1:laravel 视图控制流的写法 假设控制器代码如下 $data = [ 0 => '张三', 1 => '李四', 2 => '王五' ]; return view('test ...

  6. 《FPGA全程进阶---实战演练》第三章之PCB设计之去耦电容

    1.关于去耦电容为何需要就近摆放? 大多数资料有提到过,去耦电容就近放置,是从减小回路电感的角度去谈及摆放问题,其实还有一个原则就是去耦半径的问题,如果电容离着芯片位置较远,超过去耦半径,会起不到去耦 ...

  7. 关于Unity中的删除、显示和隐藏节点

    一.删除节点和组件 1.删除一个节点,以及节点上面所有的组件全部删除了//删除一个节点的时候,节点上面所有的组件也会被删除:MonoBehaviour.Destroy(this.gameObject) ...

  8. HTTP/1.1 学习

    发现对于HTTP协议不能脱口而出,故而怒翻资料,RFC2616 . 在其abstract中是这么说HTTP的,应用层协议,generic.无状态.其特点之一是 the typing and negot ...

  9. AOP(Aspect Oriented Programming),即面向切面编程

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  10. ubuntu 12.04 LTS server 中文乱码【转】

    ubuntu 12.04 LTS server 中文乱码 最近装了一台ubuntu 12.04 server装完后是没有桌面的,后来又手动安装了桌面,但进行后发现桌面是乱码,应该是缺少字体在googl ...