【Docker江湖】之创建带有SSH服务的镜像
来源:互联网 发布:树莓派网络配置 编辑:程序博客网 时间:2024/04/29 15:33
转载请注明出处:http://blog.csdn.net/gamer_gyt
博主微博:http://weibo.com/234654758
Github:https://github.com/thinkgamer
Docker江湖
【Docker江湖】之Docker部署与理解
【Docker江湖】之hub上镜像的使用,Dockerfile语法解读和数据管理
【Docker江湖】之创建带有SSH服务的镜像
写在前边的话
一般情况下,linux操作系统的管理员通过SSH服务来管理操作系统,但是Docker的很多镜像都是不带SSH服务的,接下来我们就来看一下如何创建一个带有SSH服务的镜像
基于Commit命令创建
1:准备一个ubuntu的镜像
sudo docker pull ubuntu
默认安装最新版,查看镜像
sudo docker images
这个时候便可以看到我们pull的ubuntu镜像了
2:启动镜像,进入容器
[redhat@localhost ~]$ sudo docker run -it -d ubuntu:latest /bin/bash[sudo] password for redhat:e968f75ffc88881377ac0b5b74bd273c3c516544ff7d6270a1683aa676da3d6c[redhat@localhost ~]$ sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe968f75ffc88 ubuntu:latest "/bin/bash" 18 seconds ago Up 16 seconds cranky_stallman[redhat@localhost ~]$ sudo docker exec -it e9 /bin/bash
尝试使用sshd命令,会发现容器中并没有安装此命令
root@e968f75ffc88:/# sshdbash: sshd: command not found
尝试安装openssh-server
root@e968f75ffc88:/# apt-get install openssh-serverReading package lists... DoneBuilding dependency treeReading state information... DoneE: Unable to locate package openssh-server
更新软件源
apt-get update
3:安装和配置ssh服务
apt-get install openssh-server
要正常启动ssh服务,需要目录/var/run/sshd存在,手动创建他,并启动服务
mkdir -p /var/run/sshd
/usr/sbin/sshd -D &
此时查看容器的22端口(SSH服务默认监听的端口),已经处于监听状态
apt-get install net-tools
netstat -tunlp
root@e968f75ffc88:/# netstat -tunlpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program nametcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3551/sshdtcp6 0 0 :::22 :::* LISTEN 3551/sshd
修改SSH服务的安全登录配置,取消pam登陆限制
sed -ri ‘s/session required pam_loginuid.so/#session required pam_loginuid.so/g’ /etc/pam.d/sshd
在root用户目录下创建.ssh目录,并复制需要登录的公钥信息(一般为本地主机用户目录下的.ssh/id_rsd.pub文件,可由ssh-keygen -t rsa命令生成)到authorized_keys文件中
mkdir root/.ssh
apt-get install vim
vim /root/.ssh/authorized_keys
创建自动启动SSH服务的可执行文件run.sh,并添加可执行权限:
vim /run.sh
chmod +x run.sh
run.sh的内容如下:
#!/bin/bash/usr/sbin/sshd -D
最后退出容器:
exit
4:保存镜像
sudo docker commit fcl sshd:ubuntu
查看本地镜像,就会看到新生成的镜像
```[redhat@localhost ~]$ sudo docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe968f75ffc88 ubuntu:latest "/bin/bash" About an hour ago Exited (0) About a minute ago cranky_stallman[redhat@localhost ~]$ sudo docker commit e96 sshd:ubuntusha256:f52e07fa7accf437f52cb39cd36cdab9229ef88b2280129ff4d2c272fbb73aad[redhat@localhost ~]$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEsshd ubuntu f52e07fa7acc About a minute ago 255 MBubuntu latest f753707788c5 2 weeks ago 127.1 MB
使用镜像,并添加端口映射(10022–>22),其中10022是宿主主机的端口,22是容器SSH服务监听端口
sudo docker run -it -d -p 10022:22 sshd:ubuntu_new /run.sh
SSH测试登录
ssh you_ip -p 10022
[root@localhost .ssh]# ssh 192.168.10.179 -p 10022The authenticity of host '[192.168.10.179]:10022 ([192.168.10.179]:10022)' can't be established.ECDSA key fingerprint is 0b:ae:62:09:a2:18:4e:ef:16:e3:3f:b9:2d:15:fb:7a.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '[192.168.10.179]:10022' (ECDSA) to the list of known hosts.Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 3.10.0-229.el7.x86_64 x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantageThe programs included with the Ubuntu system are free software;the exact distribution terms for each program are described in theindividual files in /usr/share/doc/*/copyright.Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted byapplicable law.root@e498e20668d9:~# exitlogoutConnection to 192.168.10.179 closed.
使用Dockerfile创建
1:创建工作目录
首先创建一个sshd_ubuntu目录
mkdir ssh_ubuntu
在其中,创建Dockerfile和run.sh文件
[root@localhost mydockerfile]# cd ssh_ubuntu/[root@localhost ssh_ubuntu]# touch Dockerfile run.sh[root@localhost ssh_ubuntu]# lsDockerfile run.sh[root@localhost ssh_ubuntu]#
2:编写run.sh脚本和authorized_keys文件
脚本文件run.sh的内容与上面的内容一致:
#!/bin/bash/usr/sbin/sshd -D
在宿主主机上生成SSh密钥对,并创建authorized_keys文件:
cat ~/.ssh/id_rsa.pub > authorized_keys
3:编写Dockerfile
下面是Dockerfile内容及各部分注释,和上边commit镜像的步骤操作是一致的
#设置继承镜像FROM ubuntu:latest#运行命令RUN apt-get update#安装sshRUN apt-get install -y openssh-serverRUN mkdir -p /var/run/sshdRUN mkdir -p /root/.ssh#取消pam限制RUN sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd#复制配置文件到相应位置,并赋予脚本可执行权限ADD authorized_keys /root/.ssh/authorized_keysADD run.sh /run.shRUN chmod 755 /run.sh#开放端口EXPOSE 22#设置自启动命令CMD ["/run.sh"]
4:创建镜像
在sshd_ubuntu 目录下,使用docker build 命令来创建镜像,注意一下,在最后还有一个“.” ,表示使用当前目录中的Dockerfile
cd sshd_ubuntu
sudo docker build -t sshd:dockerfile .
这里有一点需要注意的是使用Dockerfile创建自定义镜像,docker会自动删除中间临时创建的层,还需要注意每一步的操作和编写的dockerfile中命令的对应关系
执行docker build命令的输出参考结果如下:
命令执行完毕后,如果可见 “successfully build XXX”字样,则说明镜像创建成功,可以看到,以上命令生成的镜像ID是
Step 11 : CMD /run.sh ---> Running in 69e4227186fb ---> f530a7a43fd7Removing intermediate container 69e4227186fbSuccessfully built f530a7a43fd7
在本地查看sshd:dokcerfile镜像已存在:
[root@localhost ssh_ubuntu]# sudo docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEsshd dockerfile f530a7a43fd7 About a minute ago 220.6 MBsshd ubuntu cc0c1d242d82 38 minutes ago 255 MB
5:测试镜像,运行容器
使用刚才创建的sshd:dockerfile镜像来运行一个容器,直接启动镜像,映射容器的22端口到本地10122端口:
sudo docker run -d -p 10122:22 sshd:dockerfile
ssh 192.168.10.179 -p 10122
显示:
[root@localhost .ssh]# ssh 192.168.10.179 -p 10122The authenticity of host '[192.168.10.179]:10122 ([192.168.10.179]:10022)' can't be established.ECDSA key fingerprint is 0b:ae:62:09:a2:18:4e:ef:16:e3:3f:b9:2d:15:fb:7a.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '[192.168.10.179]:10022' (ECDSA) to the list of known hosts.Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 3.10.0-229.el7.x86_64 x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantageThe programs included with the Ubuntu system are free software;the exact distribution terms for each program are described in theindividual files in /usr/share/doc/*/copyright.Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted byapplicable law.root@e498e20668d9:~# exitlogoutConnection to 192.168.10.179 closed.
Over!
- 【Docker江湖】之创建带有SSH服务的镜像
- Docker应用--创建带有sshd服务的Ubuntu镜像
- Docker创建支持ssh服务的容器和镜像
- Docker创建支持ssh服务的容器和镜像
- 创建支持SSH服务的Docker镜像
- 创建支持SSH服务的Docker镜像
- 创建ssh自启动服务的docker镜像
- <转>创建支持SSH服务的Docker镜像
- Docker- 创建支持SSH服务的容器镜像
- Docker之创建SSH服务功能的镜像(commit方法)
- docker实战之ssh服务镜像
- Docker实战:使用Dockerfile创建带Apache服务并支持ssh的Centos Docker镜像
- Docker实战:使用Dockerfile创建带nginx服务并支持ssh的Centos Docker镜像
- Docker实战:使用Dockerfile创建带tomcat服务并支持ssh的Centos Docker镜像
- 创建支持ssh的docker镜像
- Docker使用Dockerfile创建支持ssh服务自启动的容器镜像
- Docker使用Dockerfile创建支持ssh服务自启动的容器镜像
- Docker使用Dockerfile创建支持ssh服务自启动的容器镜像
- 帝国分类目录官网
- CISCO 接入公网配置
- DJANGO 服务器配置 hello world
- Ubuntu16.04 To Ruijie瑞捷客户端v4.99安装问题
- 【leetcode】414. Third Maximum Number【E】【85】
- 【Docker江湖】之创建带有SSH服务的镜像
- 层序生成二叉树
- 普通静态页面缓存
- titan gremlin rest api配置方法
- andorid自动化测试之Monkey(下)
- 欢迎使用CSDN-markdown编辑器
- [java学习8]uiRouter练习
- virtualbox的VERR_FILE_NOT_FOUND错误
- titan gremlin rest api配置方法