思路:
(1)基于初始构建镜像脚本生成一个初始镜像A:
1)基于客户端打客户端镜像包client.tar.gz
#!/usr/bin/bash
base_dir=`cd $(dirname $0);pwd`
cd /opt
files=""
client_dir="client"
if [ -L $client_dir ];then
files="$client_dir"
client_dir="$(ls -al $client_dir|awk '{print $NF}')"
client_dir=$(basename $client_dir)
if [ -d $client_dir/HBase ];then
files="$files $client_dir/HBase"
fi
fi
if [ -d hbase_client -o -d client/HBase ];then
if [ -d hbase_client ];then
hbase_client_dir="hbase_client"
fi
if [ -L "hbase_client" ];then
files="$files hbase_client"
hbase_client_dir="$(ls -al hbase_client|awk '{print $NF}')"
hbase_client_dir=$(basename $hbase_client_dir)
files="$files $hbase_client_dir/HBase $hbase_client_dir/bigdata_env $hbase_client_dir/client.* $hbase_client_dir/*.sh $hbase_client_dir/*.py $hbase_client_dir/*.ini"
fi
else
echo "没找到 /opt/hbase_client"
echo "如果hbase 与hadoop 同一个集群,请手动执行一下: ln -s /opt/client /opt/hbase_client ,再重新执行脚本"
#exit -1
echo "hbase 文件将不打包"
fi
files="$files $client_dir/bigdata_env $client_dir/*.sh $client_dir/*.py $client_dir/*.ini"
files="$files $client_dir/HDFS $client_dir/JDK $client_dir/Yarn $client_dir/Spark2x $client_dir/KrbClient "
if [ -f $client_dir/client.* ];then files="$files $client_dir/client.*";fi;
echo 打包文件:$files
rm -f $base_dir/client.tar.gz
tar zcvf $base_dir/client.tar.gz $files && echo 打包 client.tar.gz 成功:$files
cd $base_dir
mkdir -p k8sbin && \cp /usr/bin/kubectl k8sbin/kubectl
2)构建基础镜像A
dir=$(dirname $0)
cd $dir
ver=hd_6.5.1_bj
if [ $1 ];then
ver=$1
fi
ver=hadoop-spark:$ver
docker build -t $ver .
FROM openjdk:8.1
RUN rm -fr /opt/jdk && rm -f /usr/local/java8 && rm -fr /usr/local/openjdk-8 && unset JAVA_HOME && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
ADD ./k8sbin/kubectl /usr/bin/kubectl
ADD client.tar.gz /opt/
RUN rm -f /opt/client/Spark2x/spark/jars/solr-solrj-*.jar
RUN if [ -d /opt/client/HBase -a ! -d /opt/hbase_client ];then ln -s /opt/client /opt/hbase_client;fi
#ENTRYPOINT ["/opt/client/bigdata_env"]
RUN echo "source /opt/client/bigdata_env" >> /etc/profile
RUN echo "source /opt/client/bigdata_env" >> /root/.bashrc
RUN echo "alias ls='ls --color=auto' " >> /root/.bashrc
RUN echo "alias ll='ls -l --color=auto' " >> /root/.bashrc
(2)基于初始镜像A构建新增普通用户的镜像B:
1) docker run -it --name X1 <镜像A镜像ID>;
2) useradd normalUserName //新增普通用户名,比如新增tom: useradd tom;
3)id -u tom //查看用户tom的UID
4)docker commit X1 镜像B
(3)构建带有普通用户名UID的镜像C
1) 创建DockerFile
FROM 镜像B的REPOSITORY:TAG
USER tom的UID
USER lisi的UID
2) 构建镜像C build-imageC.sh
dir=$(dirname $0)
cd $dir
ver=hdd-imageC
if [ $1 ];then
ver=$1
fi
ver=addNomaluser:$ver
docker build -t $ver .