docker为centos镜像添加sshd服务

使用centos镜像添加sshd服务,并用xshell进行连接。
1、拉取centos镜像

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a02a4930cb5d: Pull complete
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for centos:latest

2、运行centos镜像

[root@localhost ~]# docker run -it -d centos /bin/bash
a548d123a910278da945bc02d210cd4c6b49886d1da58582bcd9940153eeace0

3、进入容器安装sshd服务

[root@localhost ~]# docker exec -it a548d123a910 /bin/bash
[root@a548d123a910 /]# yum install openssh-server -y
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile

  • base: mirrors.aliyun.com
  • extras: mirrors.aliyun.com
  • updates: mirrors.aliyun.com
    Resolving Dependencies
    --> Running transaction check
    ---> Package openssh-server.x86_64 0:7.4p1-16.el7 will be installed
    --> Processing Dependency: openssh = 7.4p1-16.el7 for package: openssh-server-7.4p1-16.el7.x86_64
    .......

4、退出容器,使用commit命令创建镜像

[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a548d123a910 centos "/bin/bash" 21 minutes ago Up 20 minutes zen_mcclintock
[root@localhost ~]# docker commit a548d123a910 sshd:centos #根据容器创建镜像
sha256:5f5070d95518bf9a870cf1112f000b28eb7b066ce9a49952515f7f4e52b3101b
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd centos 5f5070d95518 7 seconds ago 330MB
centos latest 1e1148e4cc2c 6 days ago 202MB

5、启动并进入容器,启动sshd服务

[root@localhost ~]# docker run -it -d -p 10001:22 sshd:centos #使用-p映射本地10001端口给容器的sshd22端口
7a657a22a288fff7225ce31b43c4e8c460872507174f170b7c5a14dc09d742b2
[root@localhost ~]# docker exec -it 7a657a22a288 /bin/bash
[root@7a657a22a288 /]# /usr/sbin/sshd -D & #启动sshd服务
[1] 41

6、设置秘钥

[root@7a657a22a288 /]# ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair. #回车
/etc/ssh/ssh_host_rsa_key already exists. #回车
Overwrite (y/n)? y
[root@7a657a22a288 /]# pwsswd #设置容器root密码
[root@7a657a22a288 /]# exit
exit

6、使用xshell连接docker容器
docker为centos镜像添加sshd服务
根据连接输入用户密码即可登录,这个过程比较慢。
docker为centos镜像添加sshd服务

猜你喜欢

转载自blog.51cto.com/13760226/2329547