Centos7建立支持ssh服务器的docker容器

一、启动一个docker容器:web

# docker run -it centos:latest /bin/bash
  • 1

这样就会新建一个docker容器,而且进入容器的bash中 
二、安装sshd:docker

# yum -y install openssh-server
# yum -y install openssh-clients
  • 1
  • 2

三、启动sshd服务:shell

# /usr/sbin/sshd -D
  • 1

个人报一下错误vim

Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
  • 1
  • 2
  • 3

此时,依次执行下列命令: 
一路按回车键确认centos

# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ""
# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
# ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
  • 1
  • 2
  • 3

再启动sshd服务,一切正常。 
四、编辑sshd_config配置文件bash

# vim /etc/ssh/sshd_config
  • 1

找到UsePAM yes这一段配置,将其改为UsePAM noapp

UsePAM no
#UsePAM yes
  • 1
  • 2

五、修改root的密码,若是不能passwd,执行:# yum -y install passwdssh

# passwd root
Changing password for user root.
New password:
  • 1
  • 2
  • 3

两次输入密码 
六、改完密码执行exit命令退出,这时会回到宿主机器的shell,执行下列命令将容器提交到镜像:webapp

# docker commit containerid imagename
  • 1

这里的containerid是容器的id,imagename就是提交时候镜像的名称,第一次提交的时候最好使用一个新的名称,不要覆盖了原有的干净的centos镜像。 
容器id能够经过docker ps -l命令查看到,启动容器后默认的主机名其实就是容器id。 
例如:# docker commit 67bb1912a373 sshd-images 
七、经过docker run启动一个新的容器,参数-d表示后台运行,-p表示docker到主机的端口的映射测试

# docker run -d -p 10022:22 imagename /usr/sbin/sshd -D
  • 1

若是启动没问题的话,就能够登陆到容器了:

# ssh root@localhost -p 10022
  • 1

挂载一个主机目录做为数据卷 
使用-v标记也能够指定挂载一个本地的已有目录到容器中去做为数据卷:

# docker run -d -p 10022:22 --name web -v /usr/webapp:/opt/webapp sshd-images:latest /usr/sbin/sshd -D
  • 1

上面的命令加载主机的/usr/webapp目录到容器的/opt/webapp目录:

这个功能在进行测试的时候十分方便,好比用户能够放置一些程序或数据到本地目录中,而后在容器内运行和使用。另外,本地目录的

相关文章
相关标签/搜索