Docker实战-如何为镜像添加SSH服务?

在实际生产过程中,运维人员都是习惯通过SSH服务来远程登录并且管理服务器。但是对于Docker的很多镜像来讲,其实是不带有SSH服务的。那么运维人员是如何进入容器并且管理容器的的呢?

在之前的时候我们介绍过可以通过attach、exec等命令来进入到容器中,但是这些命令是无法解决通过远程进入容器的问题。所以如果我们要进行远程控制访问就必须要使用到SSH服务,下面我们就来介绍一下如何去自行创建一个带有SSH服务的镜像,以及相关的操作。

基于docker commit 命令来创建

Docker中提供了一个docker commit命令,主要用来让用户提交自己对定制容器的修改,并且生成新的镜像。这里我们就来用之前的Ubuntu18.04的镜像来添加SSH服务。

准备工作

首先需要获取一个Ubuntu18.04的镜像并且创建好一个容器。

[root@localhost ~]# docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
7c457f213c76: Pull complete 
Digest: sha256:152dc042452c496007f07ca9127571cb9c29697f42acbfad72324b2bb2e43c98
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04
[root@localhost ~]# docker run -it ubuntu:18.04 bash
root@ca1a8b78eda7:/# 

配置软件源

进入到容器中,然后使用apt-get update 命令来更新软件源信息

root@ca1a8b78eda7:/# apt-get update

安装SSH服务

完成了软件源更新之后,接下来就开始安装SSH服务。

root@ca1a8b78eda7:/# apt-get install openssh-server

如果需要SSH服务能够正常启动,那么就需要在/var/run/sshd 目录是必须存在的,查看对应路径下没有该目录需要通过如下的命令来创建

root@ca1a8b78eda7:~# mkdir -p /var/run/sshd
root@ca1a8b78eda7:~# /usr/sbin/sshd -D &
[1] 4029
root@ca1a8b78eda7:~# 

这个时候我们可以查看容器中的22端口,并且会看到这个端口已经处于监听状态。

root@ca1a8b78eda7:~# apt-get install net-tools

查看端口状态

root@ca1a8b78eda7:~# netstat -tunlp
Active Internet connections (only servers)
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      4029/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      4029/sshd           
root@ca1a8b78eda7:~# 

修改SSH服务的安全登录模式,首先取消pam登录限制,在root目录下创建.ssh目录,并且创建对应的额公钥文件。

root@ca1a8b78eda7:~# sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd
root@ca1a8b78eda7:~# mkdir /root/.ssh
root@ca1a8b78eda7:~# vi /root/.ssh/authorized_keys

生成秘钥文件,注意这个操作需要在宿主机中来完成。如果已经存在对应的文件则不需要再次生成。

[root@localhost .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:NwJiefJQkcvtvTLkAZ6fJQ9HyXNRour8bamr7HyDz8Q root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|      oo    ...  |
|     o.    ...   |
|    *.oo ... .   |
|   . *+...= .    |
|     ..+Sooo     |
|      oo*++.     |
|       +oXE. .   |
|       oB+=.o    |
|       .=**=.    |
+----[SHA256]-----+
[root@localhost .ssh]# ll
总用量 16
-rw------- 1 root root  381 8月   8 16:57 authorized_keys
-rw------- 1 root root 1675 8月  11 09:09 id_rsa
-rw-r--r-- 1 root root  408 8月  11 09:09 id_rsa.pub
-rw-r--r-- 1 root root  183 8月  11 08:40 known_hosts
[root@localhost .ssh]# 

这里为了保险起见,可以将主机的免密登录操作与容器中的免密登录操作对应的公钥私钥全部设置。

在容器中添加如下的文件内容,编辑run.sh文件内容如下

root@8a69c6d6e193:~# cd /
root@8a69c6d6e193:/# vim run.sh
root@8a69c6d6e193:/# chmod +777 run.sh 

"添加内容"
#!/bin/bash
/usr/sbin/sshd -D

到这里我们就可以退出容器

root@ca1a8b78eda7:~/.ssh# exit
exit
[root@localhost ~]#

保存镜像

退出之后,可以通过 docker commit 命令制作一个新的镜像

[root@localhost ~]# docker commit ca1a8b78eda7 sshd:ubuntu
sha256:c895b286854daed1e74cc72d9173d68685cc0a6d31828f2c14b12aa12954f258
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
sshd         ubuntu    c895b286854d   51 seconds ago   260MB
ubuntu       18.04     f9a80a55f492   2 months ago     63.2MB
[root@localhost ~]# 

使用镜像

启动容器,并且添加对应的端口映射,来访问容器

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
sshd         ubuntu    4c92764d9517   5 seconds ago   260MB
ubuntu       18.04     f9a80a55f492   2 months ago    63.2MB
[root@localhost ~]# docker run -p 10022:22 -d sshd:ubuntu /run.sh
946c9b38617b5fe6c161eb03cf2ba707025a400a6acf3f82d2f80b48233d1503
[root@localhost ~]# 

启动成功之后,就可以在docker ps中查看相关的运行容器信息。

[root@localhost ~]# docker run -p 10022:22 -d sshd:ubuntu /run.sh
b430f5149379326f80743deef44096712c5c37fd612d6b444c7160871d483dea
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE         COMMAND     CREATED         STATUS         PORTS                                     NAMES
b430f5149379   sshd:ubuntu   "/run.sh"   3 seconds ago   Up 2 seconds   0.0.0.0:10022->22/tcp, :::10022->22/tcp   suspicious_chaplygin

这个时候,我们就可以通过宿主机或者是其他的主机通过SSH来访问容器的10022端口来登录到容器中。

[root@localhost ~]# ssh 192.168.1.202 -p 10022
The authenticity of host '[192.168.1.202]:10022 ([192.168.1.202]:10022)' can't be established.
ECDSA key fingerprint is SHA256:ea/n4w8K7rYSlDj2xjCN7uAouq9mi2B1hHBFEaWc49k.
ECDSA key fingerprint is MD5:6a:da:09:12:d6:ed:f8:ff:26:35:e6:00:69:78:08:6b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.1.202]:10022' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 3.10.0-1160.an7.x86_64 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
inpidual files in /usr/share/doc/*/copyright.

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

root@b430f5149379:~# 

总结

上面我们介绍了如何通过已有镜像的方式来创建一个可以登录的容器并进行了连接以及运行,其中也遇到了很多的问题。需要开发者结合实际的操作过程来解决对应的问题。其中比较重要的就是对于容器与本地地址免密登录操作相关内容。关于免密操作的相关内容,需要读者自己去了解。

展开阅读全文

页面更新:2024-02-25

标签:宿主   端口   容器   实战   命令   状态   操作   文件   目录   内容   软件

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top