添加ssh服务构建新镜像-docker commit 方式

1:docker commit构建自定义镜像---一般不推荐使用这种方式构建新镜像

1:ubuntu基础镜像下载
ubuntu@ubuntu:~$ docker pull ubuntu:18.04 2:运行镜像,启动一个容器
ubuntu@ubuntu:~$ docker run -it ubuntu:18.04 /bin/bash
root@92cf48876d4c:/# 3:配置软件源
#1:更新
root@92cf48876d4c:/# apt-get update     
#2:备份
root@92cf48876d4c:/# cp /etc/apt/sources.list /etc/apt/sources.list.bak
#3:安装vim.net-tools
root@92cf48876d4c:/# apt-get install vim net-tools
#4:将/etc/apt/sources.list下面的内容替换成阿里源
deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
#5:更新
root@92cf48876d4c:/# apt-get update 4:安装和配置ssh服务
#1:服务端安装
root@92cf48876d4c:/# apt-get install openssh-server #2:要正常启动SSH服务,所以手动创建/var/run/sshd目录
root@92cf48876d4c:/# mkdir -p /var/run/sshd #3:启动sshd服务
root@92cf48876d4c:~# /usr/sbin/sshd -D &
[1] 4122 #4:取消pam登录限制
root@92cf48876d4c:~# sed -ri 's/session required pam_loginuid.so/#&/g' /etc/pam.d/sshd
root@92cf48876d4c:~# cat /etc/pam.d/sshd | grep pam_loginuid.so
#session required pam_loginuid.so #5:配置登录秘钥
1:主机(client)生成公钥
ubuntu@ubuntu:~$ ssh-keygen -trsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cCJNE3EeRpucmyTAN4tJ1diGO9TVuaNJ4LkPsJisMOE ubuntu@ubuntu
The key's randomart image is:
+---[RSA 3072]----+
| ...*X=... . |
| o+***= o |
| ..==*Oo . |
|. o.=*oo. o |
|.. . o +So o . |
|oE + . o o |
| o . o |
| . . |
| |
+----[SHA256]-----+ 一直敲回车就行了 2:在容器中(server)中创建/root/.ssh目录
root@92cf48876d4c:~# mkdir /root/.ssh 3:将client的公钥复制到server的authorized_keys文件中
ubuntu@ubuntu:~$ cat /home/ubuntu/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCuqW0ncbixcQeCllHpudoguBjmxf9vf8GMdGkVrw25M0WlLbLj2gqR2MC6AdzGsPY5VThRkVeGG0UlOA382VeTj68HitC9GTS+goxl1QzPwfPWzgSJ7SNYvYRSnqx5uAuF6IUXiiv57o/CMJGJ/3JOc5jWOL+Kl3aUZExD7dxIkQd9C3BikjZLQa0IobFf5N0uZJNv+MPRwV5CH1+pNzWRo97jYJZ7aHoifT8NXyBN4Gug9Z4rYQWEMpa3B85V0nQRJLdJPiXtgAvlLmtqW8RfoHXXZlq05Oh71uRvhtHYjRi3oz9D/FxfwVBqQVipZuAb1wcYnhOW9xj8TEGDhe4QEK1Owc8spozMWPG4s0fhh3Zx6YsSwi4GAUncPKmPyHyh2MPcWpV0vRmo+Ro9L4jxZ0OCi3h/ALB9zmcVhuctin67mHSK/NX2jaKdXms+VlIJvQS0mDzY6Epr7wGZ+2mZUBq/5HCaZEfq2I68IGEQ3UjIDQ+MaZZ4mWlpesjhNIE= ubuntu@ubuntu root@92cf48876d4c:~# vi /root/.ssh/authorized_keys
root@92cf48876d4c:~# cat /root/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCuqW0ncbixcQeCllHpudoguBjmxf9vf8GMdGkVrw25M0WlLbLj2gqR2MC6AdzGsPY5VThRkVeGG0UlOA382VeTj68HitC9GTS+goxl1QzPwfPWzgSJ7SNYvYRSnqx5uAuF6IUXiiv57o/CMJGJ/3JOc5jWOL+Kl3aUZExD7dxIkQd9C3BikjZLQa0IobFf5N0uZJNv+MPRwV5CH1+pNzWRo97jYJZ7aHoifT8NXyBN4Gug9Z4rYQWEMpa3B85V0nQRJLdJPiXtgAvlLmtqW8RfoHXXZlq05Oh71uRvhtHYjRi3oz9D/FxfwVBqQVipZuAb1wcYnhOW9xj8TEGDhe4QEK1Owc8spozMWPG4s0fhh3Zx6YsSwi4GAUncPKmPyHyh2MPcWpV0vRmo+Ro9L4jxZ0OCi3h/ALB9zmcVhuctin67mHSK/NX2jaKdXms+VlIJvQS0mDzY6Epr7wGZ+2mZUBq/5HCaZEfq2I68IGEQ3UjIDQ+MaZZ4mWlpesjhNIE= ubuntu@ubuntu 6:创建自启动的脚本文件
root@92cf48876d4c:~# vi /run.sh
root@92cf48876d4c:~# cat /run.sh
#! /bin/bash
/usr/sbin/sshd -D
root@92cf48876d4c:~# chmod a+x /run.sh 7:最后退出容器
root@92cf48876d4c:~# exit
exit 8:docker commit 保存为一个新镜像
ubuntu@ubuntu:~$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
92cf48876d4c ubuntu:18.04 "/bin/bash" About an hour ago Exited (0) 2 minutes ago sad_kilby ubuntu@ubuntu:~$ docker commit 92c sshd:ubuntu
sha256:3475b858b5b3f822bbec87395f19206ff4d3a64540fba948bd58a32aad237bc6
ubuntu@ubuntu:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd ubuntu 3475b858b5b3 21 minutes ago 209MB
ubuntu 18.04 775349758637 3 weeks ago 64.2MB
training/webapp latest 6fae60ef3446 4 years ago 349MB 9:使用sshd:ubuntu镜像,添加端口映射10022-->22
ubuntu@ubuntu:~$ docker run -p 10022:22 -d sshd:ubuntu /run.sh
cb3b9c80c3b6049a20832a0772b9c9f5ab3815a5c18d7b1e57c66b2b1af8b45b ubuntu@ubuntu:~$ docker ps -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cb3b9c80c3b6 sshd:ubuntu "/run.sh" 21 minutes ago Up 21 minutes 0.0.0.0:10022->22/tcp mystifying_clarke 9:在宿主机登录容器
ubuntu@ubuntu:~$ ssh root@172.17.0.1 -p 10022
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 5.3.0-23-generic x86_64) * Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
This system has been minimized by removing packages and content that are
not required on a system that users do not log into. To restore this content, you can run the 'unminimize' command. The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law. root@cb3b9c80c3b6:~# ls -l
total 0
root@cb3b9c80c3b6:~# ls -al /root/
total 44
drwx------ 1 root root 4096 Nov 26 03:09 .
drwxr-xr-x 1 root root 4096 Nov 26 02:52 ..
-rw------- 1 root root 1215 Nov 26 02:48 .bash_history
-rw-r--r-- 1 root root 3106 Apr 9 2018 .bashrc
drwx------ 2 root root 4096 Nov 26 03:09 .cache
-rw-r--r-- 1 root root 148 Aug 17 2015 .profile
drwxr-xr-x 2 root root 4096 Nov 26 02:22 .ssh
-rw------- 1 root root 8614 Nov 26 02:24 .viminfo
root@cb3b9c80c3b6:~# ubuntu@ubuntu:~$ ssh root@192.168.43.97 -p 10022
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 5.3.0-23-generic x86_64) * Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage This system has been minimized by removing packages and content that are
not required on a system that users do not log into. To restore this content, you can run the 'unminimize' command.
Last login: Tue Nov 26 03:11:06 2019 from 192.168.43.97
root@cb3b9c80c3b6:~# 格式:ssh 远程用户root@本地主机ip -p 10022 都可以连上容器

最新文章

  1. Openstack Periodic Task
  2. Model-View-Controller(MVC) is an architectural pattern that frequently used in web applications. Which of the following statement(s) is(are) correct?
  3. jquery实现图片预加载
  4. SQL数据库基础(五)
  5. I.MX6 开机 闪红屏
  6. lua package.path的使用
  7. @import————————css代码内部链接另外css
  8. silverlight控件动画状态的过渡
  9. 模拟产生CBC LATCH与buffer busy wait等待事件
  10. 导出&导入
  11. windows 激活等命令
  12. MYSQL事务及存储引擎对比
  13. Manjaro安装后,应该做的操作,仅作为自己备份使用,如有参考不懂,请留言咨询,或Q609916691
  14. 批处理文件 bat 后台运行
  15. Qt编写调试日志输出类带网络转发(开源)
  16. 高可用Hadoop平台-集成Hive HAProxy
  17. Java 面向对象之构造方法
  18. Java多线程(九)之ReentrantLock与Condition
  19. pdb调试工具
  20. Mysql备份工具xtraback全量和增量测试

热门文章

  1. 百万年薪python之路 -- 面向对象之:类空间问题以及类之间的关系
  2. linq一般用法
  3. SpringBoot学习(一)基础篇
  4. vue.js 使用 vue-router 修改页面标题
  5. Stringbuilder常用方法
  6. java和python对比
  7. 正则表达式和python中的re模块
  8. Unity3-各个内置面板,对象说明
  9. hadoop2.6集群环境搭建
  10. JS 接口定义及实现的例子