1、创建相关目录

[root@linux-node1~]# mkdir /docker/sshd –p

2、编写dockerfile

[root@linux-node1 sshd]# vim Dockerfile

# This is a dockerfile

# Version 1

# By Kevin

# Base p_w_picpath

From centos

# Maintainer

MAINTAINER Kevin kevinhao.blog.51cto.com

 

# Commands

 

# Install and Configure sshd

RUN yum install -y openssh-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"]

[root@linux-node1sshd]# cd

3、创建镜像

[root@linux-node1~]# docker build -t kevin/sshd /docker/sshd/

4、查看镜像

[root@linux-node1~]# docker p_w_picpaths|grep sshd

kevin/sshd                     latest              d0b5016b8463        7 minutes ago       259.1 MB

5、启动测试

[root@linux-node1~]# docker run --name sshdtest -d -P kevin/sshd

6、ssh远程

6.1、查看端口映射

[root@linux-node1~]# 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远程登录

[root@linux-node1~]# ssh kevin@192.168.56.3 -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.

kevin@192.168.56.3'spassword:

[kevin@da430c0d9f1b~]$ pwd

/home/kevin

成功ssh远程到容器

[kevin@da430c0d9f1b~]$ exit

logout

Connection to192.168.56.3 closed.

此外还可以使用 ssh kevin@<container_ip>

Linux运维开发群:298324302

北京linux运维求职招聘群:153677549

本文出自 “黑夜路人” 博客,请务必保留此出处http://kevinhao.blog.51cto.com/5204735/1732782