操作系统:vm centos6.5 x64 192.168.240.148
docker容器:centos6.5 x64 172.17.0.3
1.下载原始镜像
1
2
|
[root@localhost ~] #docker pull centos
###一般会拉最新版centos7和centos6.5过来,这里使用的是centos6.5###
|
2.安装openssh
1
2
3
4
5
6
7
|
[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
######显示如下#####
|
1
2
3
4
|
Generating SSH1 RSA host key: [ OK ]
Generating SSH2 RSA host key: [ OK ]
Generating SSH2 DSA host key: [ OK ]
Starting sshd: [ OK ]
|
3.ssh到宿主机中
1
2
3
4
5
|
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
1
2
3
4
|
bash -4.1 #yum install passwd
###docker官网上的centos6.5原始镜像中没有passwd,用于修改root用户的密码###
bash -4.1 #passwd root
bash -4.1 #useradd kw1073
|
5.使用commit命令保存镜像
1
|
[root@localhost ~] #docker ps -l ###查看docker的id###
|
1
2
|
[root@localhost ~] #docker commit 6b84dba2a9bb kw1073/centos-new4
###做成可ssh的docker镜像###
|
6.在宿主机上映射端口
1
2
|
[root@localhost ~] #docker run -u -t -p 127.0.0.1:222:22 kw1073/centos-new4 /bin/bash
###做好docker镜像和宿主机的端口映射,这里会直接进入bash环境###
|
7.在docker容器中启动ssh服务
1
2
|
bash -4.1 #service sshd start
bash -4.1 #ifconfig
|
1
2
3
4
5
6
7
8
|
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连接
1
2
3
4
5
6
7
8
9
|
[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连接错误,可以登录,但是一登录上去就马上断开####
1
2
3
4
5
|
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连接
1
2
3
4
5
6
7
8
9
10
|
[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容器中的环境变量
1
2
3
|
#######直接从宿主机上拷贝一份过去########
[root@localhost dist] # scp /root/.bash* 172.17.0.3:/root/
|
12.在宿主机上重新ssh到容器中
1
2
|
[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容器为镜像
1
2
|
#######查看当前运行中的docker容器#######
[root@localhost ~] # docker ps -a
|
1
2
3
|
#######保存容器为镜像#######
[root@localhost ~] # docker commit 89741e6f8e61 kw1073/centos-new5
17a270866d48429d582aae50b6f3183a4e76c5b4813b1f6e96b5684cb222b100
|
1
2
|
#####查看当前的镜像########
[root@localhost ~] # docker images
|
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/108571.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】:
Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】:
官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...