标签:docker ssh
操作系统:vm centos6.5 x64 192.168.240.148
docker容器:centos6.5 x64 172.17.0.3
1.下载原始镜像
[root@localhost ~]#docker pull centos ###一般会拉最新版centos7和centos6.5过来,这里使用的是centos6.5###
2.安装openssh
[root@localhost ~]# docker run -i -t kw1073/centos-new4 /bin/bash bash-4.1#yum -y install openssh-server openssh-clients bash-4.1#chkconfig sshd on bash-4.1#service sshd start ######显示如下#####
Generating SSH1 RSA host key: [ OK ] Generating SSH2 RSA host key: [ OK ] Generating SSH2 DSA host key: [ OK ] Starting sshd: [ OK ]
3.ssh到宿主机中
bash-4.1#ssh 192.168.240.148 -p61618 root@192.168.240.148‘s password: Last login: Thu Jul 31 22:39:38 2014 from 192.168.240.1 [root@localhost ~]#
4.安装passwd
bash-4.1#yum install passwd ###docker官网上的centos6.5原始镜像中没有passwd,用于修改root用户的密码### bash-4.1#passwd root bash-4.1#useradd kw1073
5.使用commit命令保存镜像
[root@localhost ~]#docker ps -l ###查看docker的id###
[root@localhost ~]#docker commit 6b84dba2a9bb kw1073/centos-new4 ###做成可ssh的docker镜像###
6.在宿主机上映射端口
[root@localhost ~]#docker run -u -t -p 127.0.0.1:222:22 kw1073/centos-new4 /bin/bash ###做好docker镜像和宿主机的端口映射,这里会直接进入bash环境###
7.在docker容器中启动ssh服务
bash-4.1#service sshd start bash-4.1#ifconfig
eth0 Link encap:Ethernet HWaddr 82:E2:4F:F5:12:E9 inet addr:172.17.0.3 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::80e2:4fff:fef5:12e9/64 Scope:Link UP BROADCAST RUNNING MTU:1500 Metric:1 RX packets:4882 errors:0 dropped:0 overruns:0 frame:0 TX packets:4107 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:27592541 (26.3 MiB) TX bytes:342979 (334.9 KiB)
8.在宿主机上进行ssh连接
[root@localhost ~]#ssh 172.0.0.3 The authenticity of host ‘172.17.0.3 (172.17.0.3)‘ can‘t be established. RSA key fingerprint is 40:4f:f2:9a:55:07:9e:a0:cb:42:60:4f:92:fa:90:e7. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘172.17.0.3‘ (RSA) to the list of known hosts. root@172.17.0.3‘s password: Last login: Thu Jul 31 10:26:24 2014 from 172.17.42.1 Connection to 172.17.0.3 closed.
9.修改容器中ssh的配置
####从上一步可以看到,ssh连接错误,可以登录,但是一登录上去就马上断开####
bash-4.1# sed -i ‘s/UsePAM yes/UsePAM no/‘ /etc/ssh/sshd_config #修改sshd配置文件 bash-4.1# /etc/init.d/sshd reload bash-4.1# /etc/init.d/sshd restart #重启sshd服务 Stopping sshd: [ OK ] Starting sshd: [ OK ]
10.在宿主机上重试一次ssh连接
[root@localhost dist]# ssh 172.17.0.3 root@172.17.0.3‘s password: Last login: Thu Jul 31 11:48:51 2014 from 172.17.42.1 -bash-4.1# ls 或者如下连接: [root@localhost dist]# ssh root@127.0.0.1 -p222 root@127.0.0.1‘s password: Last login: Thu Jul 31 15:36:56 2014 from 172.17.42.1 #####因为做了端口映射,所以可以直接从映射的端口登陆###
11.设置docker容器中的环境变量
#######直接从宿主机上拷贝一份过去######## [root@localhost dist]# scp /root/.bash* 172.17.0.3:/root/
12.在宿主机上重新ssh到容器中
[root@localhost dist]# ssh 172.17.0.3 root@172.17.0.3‘s password: Last login: Thu Jul 31 15:07:00 2014 from 172.17.42.1 [root@89741e6f8e61 ~]#
13.保存配置好的docker容器为镜像
#######查看当前运行中的docker容器####### [root@localhost ~]# docker ps -a
#######保存容器为镜像####### [root@localhost ~]# docker commit 89741e6f8e61 kw1073/centos-new5 17a270866d48429d582aae50b6f3183a4e76c5b4813b1f6e96b5684cb222b100
#####查看当前的镜像######## [root@localhost ~]# docker images
本文出自 “运维小卒” 博客,谢绝转载!
创建可以ssh的docker容器,布布扣,bubuko.com
标签:docker ssh
原文地址:http://8721349.blog.51cto.com/8711349/1533295