9786756434 发表于 2016-10-12 14:24:46

Docker之配置Centos_ssh

获取最新的docker image 使用 docker pull centos即可。使用docker run -i -t centos /bin/bash就可以运行了。当初以为运行后直接配置个IP、启动SSH就行了,可搞了半天都不对,N多错误。后来找了下,Docker其实不是这个样子玩滴。    所以综合了下,还是自己根据Dockerfile来创建一个新的镜像吧,首先就是要编译Dockerfile,它的内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# cat Dockerfile
# centos:ssh
#
# VERSION               0.0.1
FROM centos
MAINTAINER shencj "732233048@qq.com"
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
1
2
3
# 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





1
2
3
4
5
6
7
8
9
10
11
12
登录:
# ssh root@localhost -p 4423
The authenticity of host ':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 ':4423' (RSA) to the list of known hosts.
root@localhost's password:         #密码:123456
#
# exit          #退出
logout
Connection to localhost closed.
#










后续操作:
    若在容器里修改ssh的配置文件/etc/ssh/sshd_config,要想使配置生效,需要重启容器

docker restart 677794c30ca9    因为运行容器时执行的命令cmd是/usr/sbin/sshd -D(启动sshd服务),这样修改的ssh配置就会生效




页: [1]
查看完整版本: Docker之配置Centos_ssh