docker commit 创建基于ubuntu的ssh镜像

Ubuntu 创建ssh容器方式

1.容器准备工作
ubuntu
[root@test-devops ~]# docker run -it ubuntu:14.04 /bin/bash

2.更新安装软件
root@f7b000275a50:/# apt-get update  带宽如果慢 可以忽略
root@f7b000275a50:/# apt-get install openssh-server vim

3.创建目录启动服务
root@fb02b31b8ef1:/# mkdir -p /var/run/sshd
root@fb02b31b8ef1:/# /usr/sbin/sshd -D &
[1] 3237
root@fb02b31b8ef1:/#

4.查看端口
root@fb02b31b8ef1:/# netstat -atlnp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3237/sshd
root@fb02b31b8ef1:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  18112  1788 ?        Ss   09:32   0:00 /bin/bash
root      3237  0.0  0.3  61312  3112 ?        S    09:37   0:00 /usr/sbin/sshd -D
root      3240  0.0  0.1  15512  1164 ?        R+   09:37   0:00 ps aux
root@fb02b31b8ef1:/#

5.修改启动脚本以及key文件
root@fb02b31b8ef1:/# mkdir /root/.ssh
root@fb02b31b8ef1:/# vi /root/.ssh/authorized_keys  客户端的公钥放到里面,那台机器需要登陆就需要登陆的public放到里面
root@fb02b31b8ef1:/# chmod 400 /root/.ssh/authorized_keys
root@fb02b31b8ef1:/# cat /ssh.sh
#!/bin/bash
/usr/sbin/sshd -D
root@fb02b31b8ef1:/#
root@fb02b31b8ef1:/# chmod +x /ssh.sh

6.保存镜像
[root@test-devops ~]# docker commit fb0 ubuntu_sshd:14.04
52ccf5e28f920bd7ec0cb73352e72c0d15ecb0dfb6020f3b0794cf3ef82f317c

7.查看镜像
[root@test-devops ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu_sshd         14.04               52ccf5e28f92        22 seconds ago      261.5 MB

8.启动镜像,本地的10022端口绑定到docker容器的22端口
[root@test-devops ~]# docker run -p 10022:22 -d ubuntu_sshd:14.04 /ssh.sh
4c6130da1b14e51595512bae1a85c842c6467d613e7fb2b65695853d3da8bf80
[root@test-devops ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                   NAMES
4c6130da1b14        ubuntu_sshd:14.04   "/ssh.sh"           4 seconds ago       Up 3 seconds        0.0.0.0:10022->22/tcp   romantic_nobel
[root@test-devops ~]#

9.连接镜像
zhangkeyuan@opsnotes:~ » cat .ssh/id_rsa.pub                                                                                                           
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC0OaHTmgsdKIrPCgULUk1XyRGhicQbPPJgQN85QTZNfA2iuK7PmkhoPnXLaRtMZl4se6xsBEuCn0Yougx4vFRzszDv/62XxjErqqC0vQBEWL4KaGCmDgEtwaE3oOBPA+kfIU0XITZuZx7KdPsaX2pngrv+EK3urs5kYUbwfQzE8G9JcqhWmGUxNWx+SvyspH5GaEMvxdpmZtitevEDfAGTnfp0DvR/C/5EcieS9MBHOAMEwm5mzVzvTV+HEN2GMmZAuooEUGlzTVyCRzJvddzzPDsVKZzLPhA8QYWYGg/bSDkrh5IFYBc5T/yDhD6xBbbFG+R4vT98iOqB7m9vXst9 zhangkeyuan@zhangkeyuandeMacBook-Air.local
zhangkeyuan@opsnotes:~ » ssh -p 10022 root@121.41.118.184
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.2.0-77-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

The programs included with the Ubuntu system are free software;
e described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

root@4c6130da1b14:~#
OpsNotes wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!