- 编写run.sh脚本
#!/bin/bash/usr/sbin/sshd -D
- 生成密钥对
-
ssh-keygen -t rsacat ~/.ssh/id_rsa.pub > authorized_keys
3.编写Dockerfile
-
# This is My sshd# Version 1.0# Author: Xupx# 设置继承镜像FROM ubuntu:14.04# 作者的信息MAINTAINER docker_user (923816843@qq.com)# 开始执行的命令RUN apt-get update# 安装ssh服务RUN apt-get install -y openssh-server RUN mkdir -p /var/run/sshd RUN 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_keysCOPY run.sh /run.shRUN chmod 755 /run.sh# 开放端口EXPOSE 22# 设置启动命令CMD ["/run.sh"]
4.创建镜像
-
docker build -t sshd:dockerfile .
-
结束会提示
-
Successfully built d4d5a8801283
-
Successfully tagged sshd:dockerfile
-
-
-
测试运行
-
docker run -d -p 10122:22 --name sshd sshd:dockerfile
-
-
连接测试
-
ssh root@192.168.1.100 -p 10122
-