浸泡了几天的官方网站,正确docker有了更好的理解。准备着手建立一个公司的开发和测试环境,包含java、python。
环境介绍
首先说明一下我的环境
2物理server(以后简称为主机)
主机A配置,如下面:
[[email protected] ~]# lsb_release -a LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch Distributor ID: CentOS Description: CentOS release 6.5 (Final) Release: 6.5 Codename: Final [[email protected] ~]# uname -a Linux opnvz 3.10.52-1.el6.elrepo.x86_64 #1 SMP Fri Aug 8 11:40:18 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux [[email protected] ~]# docker version Client version: 1.0.0 Client API version: 1.12 Go version (client): go1.2.2 Git commit (client): 63fe64c/1.0.0 Server version: 1.0.0 Server API version: 1.12 Go version (server): go1.2.2 Git commit (server): 63fe64c/1.0.0
主机B 今天还用不到,后面再介绍
目标
创建一个基础镜像,要求具备下面功能:
- 使用国内软件源
- 安装进程管理工具supervisor管理启动进程
- 能够使用ssh服务
安装步骤
从一个文件系统创建base镜像
创建镜像的方法有非常多,官方推荐的是直接从官网仓库pull一个,但因为官网被墙的比較厉害。所以这里我选择从文件系统创建一个base镜像。具体方法见我的另外一片博文http://blog.csdn.net/smallfish1983/article/details/38555827
这里以ubuntu14.04 为例。从openvz下载一个ubuntu14.04的模板
wget http://download.openvz.org/template/precreated/ubuntu-14.04-x86_64.tar.gz
cat ubuntu-14.04-x86_64-minimal.tar.gz |docker import - ubuntu:base
然后用docker images看下:
docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu base 05ac7c0b9383 17 seconds ago 215.5 MB
这样我们就建了一个基于openvz模板的ubuntu基础镜像。(尽管官方的镜像可能比这个更小,但一些基础的如ping这种工具都没有安装,不是十分方便,推荐openvz的模板)
更改软件源、安装supervisor服务
能够使用docker的dockerfile来完毕整个过程,只是个人觉的还是自己手工进去配置完之后更方便。dockerfile更适合分享你的创建过程给其它人。
这里我们运行 docker run -t -i ubuntu:base /bin/bash 进入虚拟机
编辑apt软件源vi /etc/apt/sources.list,删除里面的内容,粘帖上国内163的源
deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse
运行apt-get update更新
然后安装supervisor服务
apt-get supervisor cp supervisord.conf conf.d/ cd conf.d/ vi supervisord.conf
改动该文件例如以下:
; supervisor config file [unix_http_server] file=/var/run/supervisor.sock ; (the path to the socket file) chmod=0700 ; sockef file mode (default 0700) [supervisord] logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP) nodaemon=true 。(改动该软件的启动模式为非daemon,否则docker 在运行的时候会直接退出) [include] files = /etc/supervisor/conf.d/*.conf [program:sshd] command = /usr/sbin/sshd -D ;(加入ssh服务)<span style="font-family:Arial;"><span style="font-size: 14px; line-height: 26px;"> </span></span>
配置ssh服务
创建ssh的执行文件夹
mkdir /var/run/sshd
更改rootpassword passwd
vi /etc/ssh/sshd_config 改动以下2个參数同意root登陆
PermitRootLogin yes
UsePAM no
退出 docker
[email protected]:/etc/supervisor/conf.d# exit exit [[email protected] start]# docker commit f3c8 ubuntu 5c21b6cf7ab3f60693f9b6746a5ec0d173fd484462b2eb0b23ecd2692b1aff6b [[email protected] start]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest 5c21b6cf7ab3 6 seconds ago 512 MB<span style="font-family:Arial;"><span style="font-size: 14px; line-height: 26px;"> </span></span>
提交刚才的改变到新的image ubuntu上。假设不加冒号tag。默认的tag就是latest
如今新建一个文件夹
mkdir ubuntu touch ubuntu/Dockerfile vi ubuntu/Dockerfile
加入例如以下内容
FROM ubuntu EXPOSE 22 CMD ["/usr/bin/supervisord"]<span style="font-family:Arial;"><span style="font-size: 14px; line-height: 26px;"> </span></span>
然后从这个文件创建image
[[email protected] start]# docker build -t ubuntu ubuntu/ Sending build context to Docker daemon 2.56 kB Sending build context to Docker daemon Step 0 : FROM ubuntu ---> 5c21b6cf7ab3 Step 1 : EXPOSE 22 ---> Running in 00a75d649b65 ---> 0429a60c040d Removing intermediate container 00a75d649b65 Step 2 : CMD ["/usr/bin/supervisord"] ---> Running in 49d505f56418 ---> c5d51fba9226 Removing intermediate container 49d505f56418 Successfully built c5d51fba9226<span style="font-family:Arial;"><span style="font-size: 14px; line-height: 26px;"> </span></span>
再看下images
[[email protected] start]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest c5d51fba9226 About a minute ago 512 MB <none> <none> 66604abb62b1 38 minutes ago 570.3 MB<span style="font-family:Arial;"><span style="font-size: 14px; line-height: 26px;"> </span></span>
刚才创建的ubuntu 被新的image给替代了,而之前的镜像我们假设要删掉的话。得先删除刚才我们运行的容器,然后再删除image
docker ps -a<span style="font-family:Arial;"><span style="font-size: 14px; line-height: 26px;"> </span></span>
找到刚才的容器
docker rm f3c8005aa252
docker rmi 66604abb62b1
再看下images
[[email protected] start]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE ubuntu latest c5d51fba9226 About a minute ago 512 MB
docker run -p 301:22 -d --name test ubuntu /usr/bin/supervisord
然后我们就能够ssh 到301port了
这里,粉底ubuntu基本的图像创建完毕,这背后图像的基础上创造其他服务