首页 > IT职业 > IT职场 > 16-制作tomcat-gradle镜像
OpenShift Origin是以Docker作为容器引擎的,因此Docker上的镜像可以pull下来直接使用。但随着业务的发展,用户的需求是多样化的,开源的镜像仓库并不能满足用户的需求,此时就需要根据用户自身的需求定制镜像。以下以基于gradle构建的java项目为例,制作Tomcat-Gradle镜像。
软件版本:
tomcat:8.5.20
gradle:4.7
命令格式:s2i create
[root@master ~]# cd /opt[root@master opt]# s2i create tomcat-gradle tomcat-gradle
注:Builder镜像名只支持小写。
可以看到生成的默认文件。
[root@master opt]# cd tomcat-gradle[root@master tomcat-gradle]# ls -l . 总用量 16-rw-------. 1 root root 1351 4月 24 03:41 Dockerfile-rw-------. 1 root root 190 4月 24 03:41 Makefile-rw-------. 1 root root 4175 4月 24 03:41 README.mddrwx------. 3 root root 16 4月 24 03:41 s2idrwx------. 3 root root 31 4月 24 03:41 test
修改Dockerfile,内容如下。
FROM openshift/base-centos7# TODO: Put the maintainer name in the image metadataLABEL maintainer="Xianwen Zhang"# TODO: Rename the builder environment variable to inform users about application you provide themENV BUILDER_VERSION 1.0 ENV GRADLE_VERSION 4.7 ENV TOMCAT_VERSION 8.5.20# TODO: Set labels used in OpenShift to describe the builder imageLABEL io.openshift.s2i.scripts-url=image:///usr/libexec/s2i io.k8s.description="Tomcat ${TOMCAT_VERSION} Gradle${GRADLE_VERSION} " io.k8s.display-name="Tomcat ${TOMCAT_VERSION} Gradle${GRADLE_VERSION}" io.openshift.expose-services="8080:http" io.openshift.tags="builder,tomcat,gradle" name="centos/tomcat${TOMCAT_VERSION}-gradle${GRADLE_VERSION}" version="${TOMCAT_VERSION}" maintainer="openshift.aorise.org "WORKDIR /opt# TODO: Install required packages here:RUN yum update -y RUN yum install -y unzip RUN yum clean all -y# install jdkADD ./jdk-8u144-linux-x64.tar.gz /opt ENV JAVA_HOME /opt/jdk1.8.0_144 ENV JRE_HOME ${JAVA_HOME}/jre ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib ENV PATH ${JAVA_HOME}/bin:$PATHRUN java -version# install tomcatADD ./apache-tomcat-${TOMCAT_VERSION}.tar.gz /opt RUN useradd -m tomcat -u 1000 && chmod -R a+rw /opt && chmod a+rwx /opt/apache-tomcat-${TOMCAT_VERSION}/* && chmod +x /opt/apache-tomcat-${TOMCAT_VERSION}/bin/*.sh && rm -rf /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/* && echo "tomcat install successful!"# install gradleADD ./gradle-${GRADLE_VERSION}-bin.zip /opt RUN unzip -o gradle-${GRADLE_VERSION}-bin.zip ENV PATH /opt/gradle-${GRADLE_VERSION}/bin:$PATHRUN rm -rf /opt/gradle-${GRADLE_VERSION}-bin.zip && gradle --version# TODO: Copy the S2I scripts to /usr/libexec/s2i, since openshift/base-centos7 image# sets io.openshift.s2i.scripts-url label that way, or update that labelCOPY ./s2i/bin/ /usr/libexec/s2i
鉴于网络原因,可以将需要安装的软件下载到tomcat-gradle目录下。
关于Dockerfile文件命令,详细可见《015-Dockerfile详解》
[root@master tomcat-gradle]# find s2is2i s2i/bin s2i/bin/assemble s2i/bin/run s2i/bin/usage s2i/bin/save-artifacts
可以看到默认生成的脚本。assemble负责源代码的编译、构建及构建产出物的部署。修改如下。
#!/bin/bash -e## S2I assemble script for the 'tomcat7.0.81-gradle4.7' image.# The 'assemble' script builds your application source so that it is ready to run.## For more information refer to the documentation:# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md## If the 'tomcat7.0.81-gradle4.7' assemble script is executed with the '-h' flag, print the usage.if [[ "$1" == "-h" ]]; then
exec /usr/libexec/s2i/usagefi# Restore artifacts from the previous build (if they exist).#if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; then
echo "---> Restoring build artifacts..."
mv /tmp/artifacts/. ./fiecho "---> Installing application source..."cp -Rf /tmp/src/. ./echo "---> Building application from source..."# TODO: Add build steps for your application, eg npm install, bundle install, pip install, etc.gradle build
cp `find . -type f -name '*.war'` /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/cd /opt/apache-tomcat-${TOMCAT_VERSION}/webapps/
fullname=`find . -type f -name '*.war'`
basename=`basename $fullname`
mv $basename app.war
gradle clean all
S2I流程生产的最终镜像将以run这个脚本作为容器的启动命令。修改如下。
#!/bin/bash -e## S2I run script for the 'Tomcat-Gradle' image.# The run script executes the server that runs your application.## For more information see the documentation:# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md##exec asdf -p 8080bash -c "/opt/apache-tomcat-${TOMCAT_VERSION}/bin/catalina.sh run"
[root@master tomcat-gradle]# make
该过程的时间有点长,与主机所在的网络有关。
编译成功后,给镜像打上标签。
[root@master tomcat-gradle]# docker tag tomcat-gradle master.example.com:5000/tomcat-gradle:v8.5.20-4.7
如果不加上v8.5.20-4.7标签,则默认为latest。
[root@master tomcat-gradle]# docker push master.example.com:5000/tomcat-gradle:v8.5.20-4.7
openshift项目存放的是公共镜像,只有在这里的镜像会在Web控制窗口显示,才可以被用户引用。
[root@master tomcat-gradle]# oc import-image master.example.com:5000/tomcat-gradle:v8.5.20-4.7 -n openshift --confirm --insecure
此时,在Web界面上还无法识别。为了让OpenShift识别出这个镜像是S2I的Builder镜像,需要编辑刚导入的Image Stream。
[root@master tomcat-gradle]# oc edit is/tomcat-gradle -n openshift
添加注解"tags": "builder"。例如:
tags: - annotations: description: Tomcat 8.5.20 Gradle 4.7 builder iconClass: icon-tomcat openshift.io/display-name: Tomcat 8.5.20 Gradle 7 builder tags: builder,tomcat,java,gradle version: 8.5.20
此时,刷新Web界面,即可看到刚导入的镜像。也可以供用户使用了。
在Web控制台创建项目,选择刚导入的镜像,观察项目运行情况。
在Dockerfile文件中,FROM本可以引用Hub Docker上的gradle镜像,最为Builder镜像,但在执行make时,报如下错误:
useradd: Permission denied. useradd: cannot lock /etc/passwd; try again later.
从错误提示来看应该是权限问题,但几番更改无果,目前尚未查明原因,待后续研究解决。
参考地址:
Docker镜像仓库:https://hub.docker.com/
作者:Area2wen
链接:https://www.jianshu.com/p/147748d8e3d1
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4589/viewspace-2805914/ ,如需转载,请注明出处,否则将追究法律责任。