添加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  都可以连上容器

版权声明:本文为zhoujun007原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhoujun007/p/11946123.html