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服務

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章