【Docker江湖】之创建带有SSH服务的镜像

本文涉及的产品
运维安全中心(堡垒机),免费版 6个月
运维安全中心(堡垒机),企业双擎版 50资产 7天
简介: 打开微信扫一扫,关注微信公众号【数据与算法联盟】 转载请注明出处:http://blog.csdn.net/gamer_gyt 博主微博:http://weibo.com/234654758 Github:https://github.

这里写图片描述
打开微信扫一扫,关注微信公众号【数据与算法联盟】

转载请注明出处: http://blog.csdn.net/gamer_gyt
博主微博: http://weibo.com/234654758
Github: https://github.com/thinkgamer


Docker江湖


写在前边的话

   一般情况下,linux操作系统的管理员通过SSH服务来管理操作系统,但是Docker的很多镜像都是不带SSH服务的,接下来我们就来看一下如何创建一个带有SSH服务的镜像

基于Commit命令创建

1:准备一个ubuntu的镜像

sudo docker pull ubuntu

默认安装最新版,查看镜像

sudo docker images

这个时候便可以看到我们pull的ubuntu镜像了

这里写图片描述

2:启动镜像,进入容器

[redhat@localhost ~]$ sudo docker run -it -d ubuntu:latest /bin/bash
[sudo] password for redhat:
e968f75ffc88881377ac0b5b74bd273c3c516544ff7d6270a1683aa676da3d6c
[redhat@localhost ~]$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
e968f75ffc88        ubuntu:latest       "/bin/bash"         18 seconds ago      Up 16 seconds                           cranky_stallman
[redhat@localhost ~]$ sudo docker exec -it e9 /bin/bash

尝试使用sshd命令,会发现容器中并没有安装此命令

root@e968f75ffc88:/# sshd
bash: sshd: command not found

尝试安装openssh-server

root@e968f75ffc88:/# apt-get install openssh-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package openssh-server

更新软件源

apt-get update

3:安装和配置ssh服务

apt-get install openssh-server

要正常启动ssh服务,需要目录/var/run/sshd存在,手动创建他,并启动服务

mkdir -p /var/run/sshd
/usr/sbin/sshd -D &

此时查看容器的22端口(SSH服务默认监听的端口),已经处于监听状态

apt-get install net-tools
netstat -tunlp

root@e968f75ffc88:/# 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      3551/sshd
tcp6       0      0 :::22                   :::*                    LISTEN      3551/sshd

修改SSH服务的安全登录配置,取消pam登陆限制

sed -ri ‘s/session required pam_loginuid.so/#session required pam_loginuid.so/g’ /etc/pam.d/sshd

root用户目录下创建.ssh目录,并复制需要登录的公钥信息(一般为本地主机用户目录下的.ssh/id_rsd.pub文件,可由ssh-keygen -t rsa命令生成)到authorized_keys文件中

mkdir root/.ssh
apt-get install vim
vim /root/.ssh/authorized_keys

创建自动启动SSH服务的可执行文件run.sh,并添加可执行权限:

vim /run.sh
chmod +x run.sh

run.sh的内容如下:

#!/bin/bash
/usr/sbin/sshd -D

最后退出容器:

exit

4:保存镜像

sudo docker commit fcl sshd:ubuntu

查看本地镜像,就会看到新生成的镜像

```
[redhat@localhost ~]$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                          PORTS               NAMES
e968f75ffc88        ubuntu:latest       "/bin/bash"         About an hour ago   Exited (0) About a minute ago                       cranky_stallman
[redhat@localhost ~]$ sudo docker commit e96 sshd:ubuntu
sha256:f52e07fa7accf437f52cb39cd36cdab9229ef88b2280129ff4d2c272fbb73aad
[redhat@localhost ~]$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
sshd                ubuntu              f52e07fa7acc        About a minute ago   255 MB
ubuntu              latest              f753707788c5        2 weeks ago          127.1 MB

使用镜像,并添加端口映射(10022–>22),其中10022是宿主主机的端口,22是容器SSH服务监听端口

sudo docker run -it -d -p 10022:22 sshd:ubuntu_new /run.sh

SSH测试登录

ssh you_ip -p 10022

[root@localhost .ssh]# ssh 192.168.10.179 -p 10022
The authenticity of host '[192.168.10.179]:10022 ([192.168.10.179]:10022)' can't be established.
ECDSA key fingerprint is 0b:ae:62:09:a2:18:4e:ef:16:e3:3f:b9:2d:15:fb:7a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.10.179]:10022' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 3.10.0-229.el7.x86_64 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

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@e498e20668d9:~# exit
logout
Connection to 192.168.10.179 closed.

使用Dockerfile创建

1:创建工作目录

首先创建一个sshd_ubuntu目录

mkdir ssh_ubuntu

在其中,创建Dockerfile和run.sh文件

[root@localhost mydockerfile]# cd ssh_ubuntu/
[root@localhost ssh_ubuntu]# touch Dockerfile run.sh
[root@localhost ssh_ubuntu]# ls
Dockerfile  run.sh
[root@localhost ssh_ubuntu]#

2:编写run.sh脚本和authorized_keys文件

脚本文件run.sh的内容与上面的内容一致:

#!/bin/bash
/usr/sbin/sshd -D

在宿主主机上生成SSh密钥对,并创建authorized_keys文件:

cat ~/.ssh/id_rsa.pub > authorized_keys

3:编写Dockerfile

下面是Dockerfile内容及各部分注释,和上边commit镜像的步骤操作是一致的

#设置继承镜像
FROM ubuntu:latest

#运行命令
RUN apt-get update

#安装ssh
RUN apt-get install -y openssh-server
RUN mkdir -p /var/run/sshd
RUN mkdir -p /root/.ssh

#取消pam限制
RUN sed -ri 's/session required pam_loginuid.so/#session required pam_loginuid.so/g' /etc/pam.d/sshd

#复制配置文件到相应位置,并赋予脚本可执行权限
ADD authorized_keys /root/.ssh/authorized_keys
ADD run.sh /run.sh
RUN chmod 755 /run.sh

#开放端口
EXPOSE 22

#设置自启动命令
CMD ["/run.sh"]

4:创建镜像

在sshd_ubuntu 目录下,使用docker build 命令来创建镜像,注意一下,在最后还有一个“.” ,表示使用当前目录中的Dockerfile

cd sshd_ubuntu
sudo docker build -t sshd:dockerfile .

这里有一点需要注意的是使用Dockerfile创建自定义镜像,docker会自动删除中间临时创建的层,还需要注意每一步的操作和编写的dockerfile中命令的对应关系
执行docker build命令的输出参考结果如下:

命令执行完毕后,如果可见 “successfully build XXX”字样,则说明镜像创建成功,可以看到,以上命令生成的镜像ID是

Step 11 : CMD /run.sh
 ---> Running in 69e4227186fb
 ---> f530a7a43fd7
Removing intermediate container 69e4227186fb
Successfully built f530a7a43fd7

在本地查看sshd:dokcerfile镜像已存在:

[root@localhost ssh_ubuntu]# sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
sshd                dockerfile          f530a7a43fd7        About a minute ago   220.6 MB
sshd                ubuntu              cc0c1d242d82        38 minutes ago       255 MB

5:测试镜像,运行容器

使用刚才创建的sshd:dockerfile镜像来运行一个容器,直接启动镜像,映射容器的22端口到本地10122端口:

sudo docker run -d -p 10122:22 sshd:dockerfile
ssh 192.168.10.179 -p 10122

显示:

[root@localhost .ssh]# ssh 192.168.10.179 -p 10122
The authenticity of host '[192.168.10.179]:10122 ([192.168.10.179]:10022)' can't be established.
ECDSA key fingerprint is 0b:ae:62:09:a2:18:4e:ef:16:e3:3f:b9:2d:15:fb:7a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.10.179]:10022' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 3.10.0-229.el7.x86_64 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

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@e498e20668d9:~# exit
logout
Connection to 192.168.10.179 closed.


Over!

相关文章
|
13天前
|
消息中间件 Kafka 流计算
docker环境安装kafka/Flink/clickhouse镜像
通过上述步骤和示例,您可以系统地了解如何使用Docker Compose安装和配置Kafka、Flink和ClickHouse,并进行基本的验证操作。希望这些内容对您的学习和工作有所帮助。
95 28
|
26天前
|
网络协议 Linux 网络安全
docker centos镜像 npm安装包时报错“npm ERR! code ECONNRESET”
通过上述步骤,您可以有效解决在 Docker 中使用 CentOS 镜像安装 npm 包时遇到的 "npm ERR! code ECONNRESET" 错误。希望这些方法能帮助您顺利进行 npm 包的安装。
122 26
|
1月前
|
Linux Docker 容器
Linux 中停止 Docker 服务报 warning 导致无法彻底停止问题如何解决?
在 Linux 系统中,停止 Docker 服务时遇到警告无法彻底停止的问题,可以通过系统管理工具停止服务、强制终止相关进程、检查系统资源和依赖关系、以及重置 Docker 环境来解决。通过以上步骤,能够有效地排查和解决 Docker 服务停止不彻底的问题,确保系统的稳定运行。
151 19
|
1月前
|
存储 Docker 容器
Docker-基础(数据卷、自定义镜像、Compose)
通过数据卷实现持久化存储,通过自定义镜像满足特定需求,通过Docker Compose方便地管理多容器应用
93 27
|
1月前
|
Ubuntu NoSQL Linux
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
182 6
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
|
1月前
|
存储 Docker 容器
Docker-基础(数据卷、自定义镜像、Compose)
通过数据卷实现持久化存储,通过自定义镜像满足特定需求,通过Docker Compose方便地管理多容器应用。掌握这些Docker基础概念和操作,可以显著提高开发和部署效率,确保应用程序的可移植性和可扩展性。
80 22
|
6月前
|
存储 安全 Ubuntu
Docker 镜像与 Docker 容器的区别
【8月更文挑战第27天】
515 5
|
6月前
|
存储 Ubuntu 应用服务中间件
在Docker中,怎么快速查看本地的镜像和容器?
在Docker中,怎么快速查看本地的镜像和容器?
|
7月前
|
Shell Linux Docker
docker常用命令大全(基础、镜像、容器、数据卷)
这些命令仅仅是 Docker 命令行工具的冰山一角,但对于日常操作来说已经非常全面。通过熟练地使用这些基础命令,用户可以有效地管理 Docker 的镜像、容器、数据卷和网络。随着用户对 Docker 的深入使用,更高级的命令和选项将会变得必需,但上面列出的命令已经为用户提供了一个坚实的起点。对于初学者来说,理解和掌握这些常用命令是深入学习 Docker 的基础。
585 5
docker常用命令大全(基础、镜像、容器、数据卷)
|
7月前
|
Shell 应用服务中间件 nginx
docker 服务,镜像,容器命令总结
docker 服务,镜像,容器命令总结
202 4