环境准备(01) 创建启动后自动运行sshd的centos6.8容器

概述

        开发环境用的是Windows10,Hadoop的学习需要一个Linux的环境,故用Docker运行一个CentOS容器,在此容器中逐步完善Hadoop学习环境的搭建。

1. 运行官方centos:6.8镜像

  • 在此镜像上安装sshd,commit成新的镜像hadoop001,并run新的镜像hadoop001,使之成为一个run起来sshd就启动的容器hadoop001,从而可以从容器外部访问该容器;
  • 后面hadoop的学习环境就在该容器hadoop001中安装;
PS D:\dockerfile> docker run -it --name centos-ssh -h centos-ssh centos:6.8 /bin/bash

2. 安装openssh-server

[root@centos-ssh /]# yum -y install openssh-server

3. 生成2个秘钥,用以启动sshd

  • 让输密码一路回车;
[root@centos-ssh /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
47:63:a5:5b:66:85:28:ab:35:cd:82:60:47:38:1c:d6 root@centos-ssh
The key's randomart image is:
+--[ RSA 2048]----+
|   .o=.    ....  |
|   .* E . .o..   |
|   . + . == +    |
|      . =oo*     |
|       oSoo      |
|      .  .       |
|                 |
|                 |
|                 |
+-----------------+

[root@centos-ssh /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
a4:6c:ab:0f:64:c2:a7:01:e5:7b:e8:89:bc:2f:02:b9 root@centos-ssh
The key's randomart image is:
+--[ DSA 1024]----+
|  .              |
| o               |
|. .     .        |
| o o . o         |
| .* = + S        |
|+o X . .         |
|oo+ . .          |
|E..  o           |
|..o....          |
+-----------------+

4. 设置容器的root密码

[root@centos-ssh /]# echo "root:111111"|chpasswd

5. 启动sshd服务

[root@centos-ssh /]# /usr/sbin/sshd

[root@centos-ssh /]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      98/sshd
tcp        0      0 :::22

6. 退出容器centos-ssh

[root@centos-ssh /]# exit
exit

7. 将容器centos-ssh重新commit成一个新的镜像hadoop001

PS D:\dockerfile> docker commit centos-ssh hadoop001
sha256:21d4598b1d068c5d73ee93a9f160cdafc4aad803025a722472cd9bf775ed3004

PS D:\dockerfile> docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
hadoop001            latest              21d4598b1d06        8 seconds ago       281MB
nginx                latest              cd5239a0906a        4 weeks ago         109MB
centos               latest              49f7960eb7e4        4 weeks ago         200MB
rabbitmq             3.7.5-management    c51d1c73d028        8 weeks ago         149MB
mysql/mysql-server   5.7                 3cc9613ef3ba        2 months ago        244MB
centos               6.8                 6704d778b3ba        8 months ago        195MB
centos               7.4.1708            3afd47092a0e        8 months ago        197MB
mysql                5.6.35              a0f5d7301767        15 months ago       329MB

8. 运行镜像hadoop001为容器hadoop001

  • 容器hadoop001里的端口22映射成宿主机的9010;
  • 运行容器hadoop001的时候就启动sshd,/usr/sbin/sshd -D;
PS D:\dockerfile> docker run -itd --name hadoop001 -h hadoop001 -p 9010:22 hadoop001 /usr/sbin/sshd -D
280146223e2ee93bc3393d17f19880c877f0b4f47439bfb28edadcd8d23846de

PS D:\dockerfile> docker exec -it hadoop001 /bin/bash
[root@hadoop001 /]#

[root@hadoop001 /]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1/sshd
tcp        0      0 :::22

[root@hadoop001 /]# exit
exit

9. 从宿主机连进去

PS D:\dockerfile> ssh root@localhost -p 9010
The authenticity of host '[localhost]:9010 ([::1]:9010)' can't be established.
RSA key fingerprint is SHA256:xcND608ckilJni8LV7ClloBcwT860z8Ko6nhUpRUpYI.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:9010' (RSA) to the list of known hosts.
root@localhost's password:
[root@hadoop001 ~]#

你可能感兴趣的:(环境准备(01) 创建启动后自动运行sshd的centos6.8容器)