1.概述
OpenShift Origin是以Docker作为容器引擎的,因此Docker上的镜像可以pull下来直接使用。但随着业务的发展,用户的需求是多样化的,开源的镜像仓库并不能满足用户的需求,此时就需要根据用户自身的需求定制镜像。以下以基于gradle构建的java项目为例,制作Tomcat-Gradle镜像。
软件版本:
tomcat:8.5.20
gradle:4.7
2.制作步骤
2.1.创建S2I Builder镜像
命令格式:s2i create <imageName> <destination> [flags]
[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.md
drwx------. 3 root root 16 4月 24 03:41 s2i
drwx------. 3 root root 31 4月 24 03:41 test
2.2.编辑Dockerfile文件
修改Dockerfile,内容如下。
FROM openshift/base-centos7
# TODO: Put the maintainer name in the image metadata
LABEL maintainer="Xianwen Zhang <xianwen.zhang@aorise.org>"
# TODO: Rename the builder environment variable to inform users about application you provide them
ENV 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 image
LABEL 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 <xianwen.zhang@aorise.org>"
WORKDIR /opt
# TODO: Install required packages here:
RUN yum update -y
RUN yum install -y unzip
RUN yum clean all -y
# install jdk
ADD ./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:$PATH
RUN java -version
# install tomcat
ADD ./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 gradle
ADD ./gradle-${GRADLE_VERSION}-bin.zip /opt
RUN unzip -o gradle-${GRADLE_VERSION}-bin.zip
ENV PATH /opt/gradle-${GRADLE_VERSION}/bin:$PATH
RUN 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 label
COPY ./s2i/bin/ /usr/libexec/s2i
鉴于网络原因,可以将需要安装的软件下载到tomcat-gradle目录下。
关于Dockerfile文件命令,详细可见《015-Dockerfile详解》
2.3.修改编译脚本assemble
[root@master tomcat-gradle]# find s2i
s2i
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/usage
fi
# Restore artifacts from the previous build (if they exist).
#
if [ "$(ls /tmp/artifacts/ 2>/dev/null)" ]; then
echo "---> Restoring build artifacts..."
mv /tmp/artifacts/. ./
fi
echo "---> 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
2.4.修改启动脚本run
<meta content=”text/html; charset=utf-8″ http-equiv=”Content-Type”>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 8080
bash -c "/opt/apache-tomcat-${TOMCAT_VERSION}/bin/catalina.sh run"
2.5.编译
[root@master tomcat-gradle]# make
该过程的时间有点长,与主机所在的网络有关。
2.6.打上标签
编译成功后,给镜像打上标签。
[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。
2.7.推送至镜像仓库
[root@master tomcat-gradle]# docker push master.example.com:5000/tomcat-gradle:v8.5.20-4.7
2.8.导入openshift项目
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界面,即可看到刚导入的镜像。也可以供用户使用了。
3.检验镜像
在Web控制台创建项目,选择刚导入的镜像,观察项目运行情况。
4.遇到的问题
在Dockerfile文件中,FROM本可以引用Hub Docker上的gradle镜像,最为Builder镜像,但在执行make时,报如下错误:
useradd: Permission denied.
useradd: cannot lock /etc/passwd; try again later.
从错误提示来看应该是权限问题,但几番更改无果,目前尚未查明原因,待后续研究解决。
参考地址:
Docker镜像仓库:https://hub.docker.com/