环境准备
1台centos 6.5镜像虚拟机 febootstrap、docker
febootstrap 安装
yum install -y yum-priorities && rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm && rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
yum -y install febootstrap
docker 安装
yum -y install docker-io
service docker start
使用root账号制作镜像,普通用户制作后会有各种问题:
febootstrap -i bash -i wget -i yum -i iputils -i iproute -i man -i vim -i openssh-server -i openssh-clients -i tar -i gzip centos6 centos6-image http://mirrors.aliyun.com/centos/6/os/x86_64/
(-i 安装package, centos6 操作系统版本,centos6-doc安装目录,最后是源地址)
执行完成后 你当前目录会生成centos6-image 目录
cd centos6-image
cp etc/skel/.bash* root/ (不拷贝,登录后Linux提示行会显示 bash-41#,而不是我们正常看到的[root@localhost ~]#)
tar -c .|docker import - centos6-base (centos6-base 为docker 镜像名称)
查看镜像
docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos6-base latest 8f94e0bebd19 6 hours ago 399.4 MB
启动进入镜像
[root@192-168-0-151 build]# docker run -itd 8f94e0bebd19 bash
2e943abb130a32830dd8580ab9345dd514b7b39597559cd08a790a8754ef56f1
[root@192-168-0-151 build]# docker exec -it 84f48b314bfa bash
[root@2e943abb130a /]#
进入镜像后,你可以安装自己的应用,我这里安装的是php 和nginx
我在/etc/init.d/ 下加入了nginx 和php-fpm 的启停脚本(可以省略)
容器里应用开机启动
容器下面多个应用启动的方法有很多种,比如supervisord、shell脚本等
我这里用的是shell 脚本的方法
mkdir /usr/local/scripts [root@2e943abb130a scripts]# cat init-bashrc.sh #!/bin/sh function service_start() { for SERVICE in nginx php-fpm sshd do if ! (ps ax | grep -v grep | grep $SERVICE > /dev/null) then /etc/init.d/$SERVICE start; fi done } service_start
编辑 ~/.bashrc
[root@2e943abb130a scripts]# cat ~/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias ll='ls -l'
alias vi='vim'
/usr/local/scripts/init-bashrc.sh
# User specific aliases and functions
记得source ~/.bashrc
退出容器 ctrl +d
提交容器为新的镜像
docker commit 2e943abb130a centos6-nginx-php56:v1
d85f477d9e00d12c380ebc442d544e0d8c9dd8ee86441e0885bb54f636c18ec1
提交成功后,docker images 会发现多出一个镜像
启动新镜像
[root@192-168-0-151 build]# docker run -it centos6-nginx-php56:v1
Starting nginx: [ OK ]
Starting php-fpm: [ OK ]
Starting sshd: [ OK ]
[root@36abc5805a89 /]#
你还可以启动时暴漏端口,修改root 密码等
这里我们可以用Dockerfile 来做,下面只是个示例,关于Dockerfile 这里就不说明了。
cat Dockerfile
FROM centos6-nginx-php56:v1 MAINTAINER Tempted RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config RUN echo "root:123456" | chpasswd EXPOSE 22 80 CMD ["bash"]
build 新的镜像
[root@192-168-0-151 build]# docker build -t centos6-lnp:v1 . Sending build context to Docker daemon 15.87 kB Sending build context to Docker daemon Step 0 : FROM centos6-nginx-php56:v1 ---> d85f477d9e00 Step 1 : MAINTAINER Tempted ---> Running in 46ec3f0fab12 ---> 4ed3d31476d0 Removing intermediate container 46ec3f0fab12 Step 2 : RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config ---> Running in 48ece83ad435 ---> c02f08cf9b19 Removing intermediate container 48ece83ad435 Step 3 : RUN echo "root:123456" | chpasswd ---> Running in 22d1086800ca ---> 18db13e52113 Removing intermediate container 22d1086800ca Step 4 : EXPOSE 22 80 ---> Running in 8a65e01a0e84 ---> 63a5bd61ba88 Removing intermediate container 8a65e01a0e84 Step 5 : CMD bash ---> Running in 671572d70abf ---> d0c9c51584ee Removing intermediate container 671572d70abf Successfully built d0c9c51584ee
这时你执行docker images 会看到三个image
启动新的镜像
[root@192-168-0-151 build]# docker run -itd d0c9c51584ee bash
bc10a9e77a8c8b9eb12a7da16d6513ffa11a686d29dc86e050e702fac96a6109
[root@192-168-0-151 build]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc10a9e77a8c d0c9c51584ee "bash" 20 seconds ago Up 17 seconds 22/tcp, 80/tcp cocky_morse
[root@192-168-0-151 build]# docker exec -it bc10a9e77a8c ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:51
inet addr:172.17.0.81 Bcast:0.0.0.0 Mask:255.255.0.0
inet6 addr: fe80::42:acff:fe11:51/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:41 errors:0 dropped:0 overruns:0 frame:0
TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5060 (4.9 KiB) TX bytes:5179 (5.0 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
连接
[root@192-168-0-151 build]# ssh 172.17.0.81
root@172.17.0.81's password:
Last login: Fri Nov 3 17:23:16 2017 from 172.17.42.1
[root@bc10a9e77a8c ~]#
导出镜像
docker save centos6-lnp:v1 >centos6-lnp.tar