1、创建相关目录
[[email protected]~]# mkdir /docker/sshd –p
2、编写dockerfile
[[email protected]]# vim Dockerfile
# This is a dockerfile
# Version 1
# By Kevin
# Base image
From centos
# Maintainer
MAINTAINER Kevin kevinhao.blog.51cto.com
# Commands
# Install and Configure sshd
RUN yum install -yopenssh-server
RUN sed -i ‘s/UsePAM yes/UsePAM no/g‘ /etc/ssh/sshd_config
# Add the user kevin and set it‘s password for kevin
RUN useradd kevin
RUN echo "kevin:kevin"|chpasswd
RUN echo "kevin ALL=(ALL) ALL">>/etc/sudoers
# Generate a keypair
RUN ssh-keygen -tdsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -trsa -f /etc/ssh/ssh_host_rsa_key
#Star the sshd service and expose the port 22
RUN mkdir /var/run/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]
[[email protected]]# cd
3、创建镜像
[[email protected]~]# docker build -t kevin/sshd /docker/sshd/
4、查看镜像
[[email protected]~]# docker images|grep sshd
kevin/sshd latest d0b5016b8463 7 minutes ago 259.1 MB
5、启动测试
[[email protected]~]# docker run --name sshdtest -d -P kevin/sshd
6、ssh远程
6.1、查看端口映射
[[email protected]~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da430c0d9f1b kevin/sshd "/usr/sbin/sshd -D" 38 seconds ago Up 37 seconds 0.0.0.0:32769->22/tcp sshdtest
6.2使用ssh远程登录
[[email protected]~]# ssh [email protected] -p32769
The authenticity of host ‘[192.168.56.3]:32769 ([192.168.56.3]:32769)‘ can‘t be established.
RSA key fingerprintis d4:52:73:db:4c:85:ff:d5:6a:78:23:ff:52:67:01:2f.
Are you sure you want to continue connecting (yes/no)? yes
Warning:Permanently added ‘[192.168.56.3]:32769‘ (RSA) to the list of known hosts.
[email protected]‘spassword:
[[email protected]da430c0d9f1b~]$ pwd
/home/kevin
成功ssh远程到容器
[[email protected]~]$ exit
logout
Connection to192.168.56.3 closed.
此外还可以使用 ssh [email protected]<container_ip>
Linux运维开发群:298324302
北京linux运维求职招聘群:153677549