docker
2017-09-05

一、下载镜像

[root@localhost ~]# docker pull centos

二、创建并运行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 ''

四、给ssh用户设置密码,实际生产中不建议在容器使用root账户

[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
文章链接:http://www.viper.im/archives/docker-centos-sshd-install.html
本文由viper.im 原创编译,转载请保留链接:docker制作带ssh服务端的centos镜像