[root@localhost ~]# docker pull centos
[root@localhost ~]# docker run -i -t centos
[root@cb4af6e76a01 ~]# yum install openssh-server openssl -y
执行/usr/sbin/sshd -D后会报错如下
[root@cb4af6e76a01 ~]# /usr/sbin/sshd -D
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
需要生成相应的key文件如下:
[root@cb4af6e76a01 ~]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@cb4af6e76a01 ~]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@cb4af6e76a01 ~]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
[root@cb4af6e76a01 ~]# passwd
Changing password for user root.
New password:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
[root@cb4af6e76a01 ~]#
exit退出镜像,然后
[root@localhost ~]# docker commit cb4a centos-ssh
sha256:b8c3c20fce048885b7d526a4ce5fb107d4f35be5fb09ddfdcf01ef1a9e9ee350
可以查看到刚保存的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-ssh latest b8c3c20fce04 54 seconds ago 302.5 MB
docker.io/centos latest 328edcd84f1b 4 weeks ago 192.5 MB
[root@localhost ~]# docker run -d -p 19888:22 centos-ssh /usr/sbin/sshd -D
584f4d4d9d99197270c2ca91071e3260ffdab3678c5a7c07e020748c0d0eed57
查看端口映射情况
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
584f4d4d9d99 centos-ssh "/usr/sbin/sshd -D" 26 seconds ago Up 24 seconds 0.0.0.0:19888->22/tcp clever_shirley