在docker里面安装部署应用

  最近一直在做docker的安装打包工作,学到不少东西,在博客里记一下。

  环境centos6 ,docker 基础镜象centos6

  1、创建本地基础镜象,安装基础命令包

  (1)Dockerfile,Dockerfile用来创建一个自定义的image,包含用户指定的软件依赖。使用build命令来创建新的images。

  Dockerfile内容:

  

FROM centos:centos6

ADD CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo
RUN     yum clean all && \
        yum -y install vim \
        tar \
        gcc-c++ \
        openssh-server \
        openssh-clients \
        tcpdump \
         passwd \
        screen \
        telnet \
        sudo \
        vixie-cron \
        pcre-devel \
        gd-devel \
        curl-devel \
        ncurses-devel \
        bison \
        gcc \
        mysql  \
        openssl-devel \
        openssl \
        python-dateutil \
        svn

FROM centos:centos6,是指定基础镜象

ADD 是把当前文件夹下面的CentOS-Base.repo映射 到容器里面的/etc/yum.repos.d/CentOS-Base.repo

RUN 指定容器启动后要执行的命令。

(2)执行build.sh脚本

docker build -t=video-base:cloud-v4 .

创建仓库为video-base,tag为cloud-v4的本地镜象。

2、执行install,把工程拷到docker里面

(1)Dockerfile内容

FROM video-base:cloud-v4

ADD
system_install /opt/system_install
 ENTRYPOINT cd /opt/system_install && sh ./install_all.sh && sh ./install_setup.s

a.从第一步生成的video-base:cloud-v4生成新镜象

b.把当前文件夹下的system_install映射成容器里面的 /opt/system_install

c.执行install_all.sh

PWD_DIR=$(cd "$(dirname "$0")"; pwd)
echo "current dir path:"$PWD_DIR
cd $PWD_DIR

chmod -R 755 .

mkdir -p /opt/srv

echo "step 1: java"
sh ./java_install.sh

echo "step 2: nginx"
sh ./nginx_install.sh

echo "step 3: resin "
sh resin_install.sh

echo "step 4: server"
sh server_install.sh

echo "step 5: av_convert"
sh av_convert_install.sh

echo "step 6: monitor"
sh ./monitor_install.sh
cp monitor.conf /opt/service_check/monitor.conf 


echo "all installed."

d.java install.sh内容

echo "java install"
rpm -ivh /opt/src/jdk-7u65-linux-x64.rpm

echo 'export JAVA_HOME=/usr/java/jdk1.7.0_65' >> /etc/profile
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /etc/profile
echo 'export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' >> /etc/profile

source /etc/profile

mv /usr/bin/java{,.bak}
ln -s -f $JAVA_HOME/jre/bin/java   /usr/bin/java

e.av_convert install.sh

#!/bin/sh
PWD_DIR=$(cd "$(dirname "$0")"; pwd)

#NAME="resin-4.0.37"
#FROM=$NAME".tar.gz"

if [ $# == 0 ]; then
    SRC_PATH=/opt/src (此处是映射自docker外的/opt/src_3.4)
elif [ $# == 1 ]; then
    SRC_PATH=$1
else
    echo "error parameter"
    exit
fi
echo "source path:"$SRC_PATH

mkdir -p /opt/srv
cd  /opt/srv

cp -rpf $SRC_PATH/av_convert ./

 

e.执行build.sh,生成video-install-s1:$TAG .的镜象

PWD_DIR=$(cd "$(dirname "$0")"; pwd)
cd $PWD_DIR
. ../../config/env.conf
TAG=$image_tag
docker build -t=video-install-s1:$TAG .

f.执行run.sh启动s1的容器,如果有变动,就生成s2的镜象

PWD_DIR=$(cd "$(dirname "$0")"; pwd)
cd $PWD_DIR
. ../../config/env.conf
TAG=$image_tag
 docker run -ti \
-v /opt/src_3.4:/opt/src \
video-install-s1:$TAG

CID=$(docker ps -a | grep video-install-s1:$TAG | awk '{print $1}'|head -n 1)
docker commit ${CID} video-install-s2:$TAG

if [ "$1" = "Y" ]; then
        echo "export"
    IMAGE_PATH=~/image_3.4_tmp    
    mkdir -p $IMAGE_PATH
        docker export ${CID} | gzip -c > $IMAGE_PATH/video-install-s2.tar.gz
fi

其中

docker run -ti \
-v /opt/src_3.4:/opt/src \
video-install-s1:$TAG这句话是把docker外面的/opt/src_3.4映射为内部的/opt/src

CID=$(docker ps -a | grep video-install-s1:$TAG | awk '{print $1}'|head -n 1)是获取s1版本容器的id

docker commit ${CID} video-install-s2:$TAG是把上一步获取的id提交成创库为video-all tag为s2的镜象

3、保存镜象
(1)Dockerfile
FROM video-install-s2:pangu-20150612094957
MAINTAINER  Cao Song <caosong1@lenovo.com>

ADD file /opt/file
ENV SHELL /bin/bash
ENTRYPOINT ["/opt/file/startup.sh"]
CMD []

(2)build.sh

PWD_DIR=$(cd "$(dirname "$0")"; pwd)
cd $PWD_DIR
ROOT=$(cd $PWD_DIR; cd ../..; pwd)
. ../../config/env.conf
. $ROOT/lib/constant.sh
TAG=$image_tag
sed -i "s/FROM.*video-install.*$/FROM video-install-s2:$TAG/g" Dockerfile
chmod +x $PWD_DIR/file/startup.sh
docker build -t=${REGISTRY_PREFIX}video-all:$TAG .

if [ "$1" = "Y" ]; then
    echo "save docker images"
    IMAGE_PATH=~/image_3.4_tmp    
    mkdir -p $IMAGE_PATH
    time docker save ${REGISTRY_PREFIX}video-all:$TAG | gzip -9 -c > $IMAGE_PATH/video-all.tar.gz
fi

docker save > video-all.tar.gz

生成docker镜象完成。要注意av_convert工程是如何安装的。

posted @ 2015-10-26 16:27  浮白斋主人  阅读(1763)  评论(0编辑  收藏  举报