1. 程式人生 > >docker為centos映象新增sshd服務

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服務
根據連線輸入使用者密碼即可登入,這個過程比較慢。
docker為centos映象新增sshd服務