docker为centos镜像添加sshd服务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker为centos镜像添加sshd服务相关的知识,希望对你有一定的参考价值。
使用centos镜像添加sshd服务,并用xshell进行连接。
1、拉取centos镜像
[[email protected] ~]# 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镜像
[[email protected] ~]# docker run -it -d centos /bin/bash
a548d123a910278da945bc02d210cd4c6b49886d1da58582bcd9940153eeace0
3、进入容器安装sshd服务
[[email protected] ~]# docker exec -it a548d123a910 /bin/bash
[[email protected] /]# 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命令创建镜像
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a548d123a910 centos "/bin/bash" 21 minutes ago Up 20 minutes zen_mcclintock
[[email protected] ~]# docker commit a548d123a910 sshd:centos #根据容器创建镜像
sha256:5f5070d95518bf9a870cf1112f000b28eb7b066ce9a49952515f7f4e52b3101b
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd centos 5f5070d95518 7 seconds ago 330MB
centos latest 1e1148e4cc2c 6 days ago 202MB
5、启动并进入容器,启动sshd服务
[[email protected] ~]# docker run -it -d -p 10001:22 sshd:centos #使用-p映射本地10001端口给容器的sshd22端口
7a657a22a288fff7225ce31b43c4e8c460872507174f170b7c5a14dc09d742b2
[[email protected] ~]# docker exec -it 7a657a22a288 /bin/bash
[[email protected] /]# /usr/sbin/sshd -D & #启动sshd服务
[1] 41
6、设置秘钥
[[email protected] /]# 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
[[email protected] /]# pwsswd #设置容器root密码
[[email protected] /]# exit
exit
6、使用xshell连接docker容器
根据连接输入用户密码即可登录,这个过程比较慢。
以上是关于docker为centos镜像添加sshd服务的主要内容,如果未能解决你的问题,请参考以下文章