获取最新的docker image 使用 docker pull centos即可。使用docker run -i -t centos /bin/bash就可以运行了。当初以为运行后直接配置个IP、启动SSH就行了,可搞了半天都不对,N多错误。后来找了下,Docker其实不是这个样子玩滴。
所以综合了下,还是自己根据Dockerfile来创建一个新的镜像吧,首先就是要编译Dockerfile,它的内容如下
[[email protected] shencj]# cat Dockerfile # centos:ssh # # VERSION 0.0.1 FROM centos MAINTAINER shencj "[email protected]" RUN yum install -y openssh openssh-server openssh-clients RUN mkdir /var/run/sshd RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key RUN sed -i ‘s/#UseDNS yes/UseDNS no/g‘ /etc/ssh/sshd_config RUN sed -i ‘s/GSSAPIAuthentication yes/GSSAPIAuthentication no/g‘ /etc/ssh/sshd_config RUN /bin/echo ‘root:123456‘ |chpasswd RUN /bin/sed -i ‘s/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g‘ /etc/pam.d/sshd RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local EXPOSE 22 CMD /usr/sbin/sshd -D
注意:
这里的两条ssh-keygen要加上,如果不加ssh启动就会报错。因为网上大多都是Ubuntu的,当初我照着U的系统来做,根本没成功。理论上来说/usr/sbin/sshd -D就会创建了主机的rsakey,但U系统可以C系统就没成
创建镜像:docker build -t shencj/centos-ssh:v1 .
运行容器:docker run -d --name centos_ssh -p 4423:22 shencj/centos-ssh:v1
[[email protected] shencj]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 677794c30ca9 shencj/centos-ssh:v1 "/bin/sh -c ‘/usr/sbi" 46 minutes ago Up 46 minutes 0.0.0.0:4423->22/tcp centos_ssh
登录: [[email protected] shencj]# ssh [email protected] -p 4423 The authenticity of host ‘[localhost]:4423 ([::1]:4423)‘ can‘t be established. RSA key fingerprint is 1e:f3:90:63:dc:b8:91:e4:f2:e4:26:c9:35:03:00:82. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘[localhost]:4423‘ (RSA) to the list of known hosts. [email protected]‘s password: #密码:123456 [[email protected] ~]# [[email protected] ~]# exit #退出 logout Connection to localhost closed. [[email protected] shencj]#
后续操作:
若在容器里修改ssh的配置文件/etc/ssh/sshd_config,要想使配置生效,需要重启容器
docker restart 677794c30ca9
因为运行容器时执行的命令cmd是/usr/sbin/sshd -D(启动sshd服务),这样修改的ssh配置就会生效
时间: 2024-11-08 20:43:24