您的位置:首页 > 运维架构 > Docker

「一」创建一个带 ssh 服务的基础镜像(修订版)--使用「docker commit」创建

2014-10-27 12:18 1126 查看
在介绍如何创建带 ssh 服务的基础镜像之前,我们想回顾一下之前介绍过的内容,其中提到有三种创建镜像的常用办法:从文件系统导入
从现有容器使用「docker commit」提交
使用 dockerfile 文件 build
本章将主要介绍后面 2 种方法。步骤如下:
$ sudo docker run -ti ubuntu:14.04  /bin/bash
#首先,使用我们最熟悉的 「-ti」参数来创建一个容器。
root@fc1936ea8ceb:/# sshd
bash: sshd: command not found
#使用 sshd 开启 ssh server 服务,发现没有安装这个服务,注意,我们在使用 「-ti /bin/bash」 进入容器后,获得的是 root 用户的bash
root@fc1936ea8ceb:/# apt-get install openssh-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package openssh-server
#现有缓存里面也找不到该软件,看来官方精简的够彻底啊
root@fc1936ea8ceb:/# apt-get update
#使用「apt-get update」来更新 apt 软件源信息,如果觉得默认的国外源速度慢的话,也可以替换为国内 163\souhu 等镜像的源。
Ign http://archive.ubuntu.com trusty InRelease
Ign http://archive.ubuntu.com trusty-updates InRelease
Ign http://archive.ubuntu.com trusty-security InRelease
Ign http://archive.ubuntu.com trusty-proposed InRelease
...
Fetched 20.4 MB in 2min 55s (116 kB/s)
Reading package lists... Done
root@fc1936ea8ceb:/# apt-get install openssh-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
ca-certificates krb5-locales libck-connector0 libedit2 libgssapi-krb5-2
libidn11 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libwrap0 libx11-6
libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 ncurses-term
openssh-client openssh-sftp-server openssl python python-chardet
python-minimal python-requests python-six python-urllib3 python2.7
python2.7-minimal ssh-import-id tcpd wget xauth
Suggested packages:
krb5-doc krb5-user ssh-askpass libpam-ssh keychain monkeysphere rssh
molly-guard ufw python-doc python-tk python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
ca-certificates krb5-locales libck-connector0 libedit2 libgssapi-krb5-2
libidn11 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib libwrap0 libx11-6
libx11-data libxau6 libxcb1 libxdmcp6 libxext6 libxmuu1 ncurses-term
openssh-client openssh-server openssh-sftp-server openssl python
python-chardet python-minimal python-requests python-six python-urllib3
python2.7 python2.7-minimal ssh-import-id tcpd wget xauth
0 upgraded, 38 newly installed, 0 to remove and 29 not upgraded.
Need to get 7599 kB of archives.
After this operation, 35.3 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
#安装一个 ssh 服务,都需要下载安装这么多的依赖
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libedit2 amd64 3.1-20130712-2 [86.7 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty-proposed/main libkrb5support0 amd64 1.12+dfsg-2ubuntu5 [30.0 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty-proposed/main libk5crypto3 amd64 1.12+dfsg-2ubuntu5 [79.9 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libkeyutils1 amd64 1.5.6-1 [7318 B]
Get:5 http://archive.ubuntu.com/ubu ...
Updating certificates in /etc/ssl/certs... 164 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
Processing triggers for ureadahead (0.100.0-16) ...
root@fc1936ea8ceb:/# mkdir -p /var/run/sshd
#创建 ssh 服务运行需要的目录
root@fc1936ea8ceb:/# /usr/sbin/sshd -D &
#在后台运行 ssh 服务
[1] 3254
root@fc1936ea8ceb:/# 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      -
tcp6       0      0 :::22                   :::*                    LISTEN      -
root@fc1936ea8ceb:/# mkdir root/.ssh
root@fc1936ea8ceb:/# vi /root/.ssh/authorized_keys
#复制需要登陆到这个台机器的公钥到 authorized_keys 文件中
root@fc1936ea8ceb:/# sed -ri 's/session    required     pam_loginuid.so/#session    required     pam_loginuid.so/g' /etc/pam.d/sshd
#修改 ssh 服务的安全登陆配置
root@fc1936ea8ceb:/# vi /run.sh
#创建可执行文件run.sh,详细内容见后文
root@fc1936ea8ceb:/# chmod +x run.sh
#添加可执行权限
root@fc1936ea8ceb:/# exit
exit
$ sudo docker commit  fc1 sshd:ubuntu
#使用 「docker commit」 将现在这个容器保存为 sshd:ubuntu 镜像
7aef2cd95fd0c712f022bcff6a4ddefccf20fd693da2b24b04ee1cd3ed3eb6fc
$ sudo docker  images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
sshd                ubuntu              7aef2cd95fd0        10 seconds ago      255.2 MB
busybox             latest              e72ac664f4f0        3 weeks ago         2.433 MB
ubuntu              latest              ba5877dc9bec        3 months ago        192.7 MB
#验证一下,我们目前拥有的镜像
$ sudo docker  run -p 100:22  -d sshd:ubuntu /run.sh
#启动容器,并映射端口 100 -->22,100 是宿主主机的端口,22 是容器的 ssh 端口
3ad7182aa47f9ce670d933f943fdec946ab69742393ab2116bace72db82b4895
$ sudo docker ps
#启动成功,可以看到容器运行的详细信息
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                 NAMES
3ad7182aa47f        sshd:ubuntu         "/run.sh"           2 seconds ago       Up 2 seconds        0.0.0.0:100->22/tcp   focused_ptolemy
> ssh 192.168.1.200 -p 100
#在刚才复制公钥的机器上登陆容器
The authenticity of host '[192.168.1.200]:100 ([192.168.1.200]:100)' can't be established.
ECDSA key fingerprint is 5f:6e:4c:54:8f:c7:7f:32:c2:38:45:bb:16:03:c9:e8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.1.200]:100' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.2.0-37-generic x86_64)

* Documentation:  https://help.ubuntu.com/ 
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@3ad7182aa47f:~#
#成功登陆,镜像创建成功。
run.sh 脚本内容
#!/bin/bash
/usr/sbin/sshd -D


更多内容,欢迎关注 www.dockerpool.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息