有不少教程都是介绍如何使用Dockerfile的方法自定义创建镜像,下边介绍通过一个运行的Docker示例,安装必要的软件,再创建镜像的方式构建符合自己要求的镜像。
下面开始吧
交互式进入新安装的实例中:
docker run -i -t ubuntu /bin/bash
进入系统后就会看到:
root@b626da1aa663:~#
说明已经进入了交互式环境。
安装ssh终端服务器,便于我们从外部使用SSH客户端登录访问:
sed -i "s/archive.ubuntu.com/mirror.bit.edu.cn/g" /etc/apt/sources.list // 修改为国内常用的软件源。
apt-get update
apt-get install -y openssh-server
which sshd
/usr/sbin/sshd
mkdir /var/run/sshd
sed -i "s/without-password/yes/g" /etc/ssh/sshd_config // 修改成PermitRootLogin yes,使root用户可以通过SSH客户端登录
passwd root // 输入root用户的密码,便于SSH客户端登录使用
exit
查看当前最近创建的docker实例。
wiki@ubuntu:~$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40a835752b25 ubuntu "/bin/bash" 2 minutes ago Exited (0) 13 seconds ago amazing_spence
将刚才修改过的docker实例保存为一个镜像ubuntu-sshd
。
wiki@ubuntu:~$ docker commit 40a8 ubuntu-sshd
b353bea022dcd538f7a31282d11de27497eef3a3ae69302b545d54a716d59a35
查看docker镜像是否保存成功。
wiki@ubuntu:~$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu-sshd latest b353bea022dc 17 seconds ago 251.3 MB
...
启动实例ubuntu-sshd
,将宿主机的222端口映射为docker实例中的22端口,并且运行实例中的SSH服务。
wiki@ubuntu:~$ docker run -p 222:22 -d ubuntu-sshd /usr/sbin/sshd -D
b626da1aa663af485b0a4dc8e5bb240b54b6163c8854474de978e960f4854a1e
查看docker实例是否运行成功。
wiki@ubuntu:~$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b626da1aa663 ubuntu-sshd "/usr/sbin/sshd -D" 16 seconds ago Up 15 seconds 0.0.0.0:222->22/tcp drunk_kare
查看系统中是否监听了222端口。
wiki@ubuntu:~$ netstat -an |grep 222
tcp6 0 0 :::222 :::* LISTEN
通过SSH登录到docker实例中。
wiki@ubuntu:~$ ssh root@127.0.0.1 -p 222
The authenticity of host '[127.0.0.1]:222 ([127.0.0.1]:222)' can't be established.
ECDSA key fingerprint is 99:ac:cc:22:79:27:89:01:91:f3:d3:55:87:dd:b6:88.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:222' (ECDSA) to the list of known hosts.
root@127.0.0.1's password:
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.19.0-31-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@b626da1aa663:~#
登录成功,可以做其他想做的事情了。