All Products
Search
Document Center

Serverless App Engine:Create an image to deploy a Java application

Last Updated:Sep 03, 2024

If you want to deploy a WAR or JAR package that is developed and compiled from the Spring Cloud, Dubbo, or HSF framework in Serverless App Engine (SAE) as an image, you must use the package as an application image. This topic describes the usage notes for creating images, how to create images for Java applications that are compiled with different code packages, and how to upload images to an image repository.

Usage notes

When you create a custom image from a Dockerfile, SAE automatically injects the following environment variables during runtime.

Important
  • To ensure that your applications run as expected, do not overwrite the following environment variables.

  • Do not use the file in the following directory: /home/admin/.spas_key/default. This file is overwritten when the pod runs.

  • Do not use the VOLUMES or USER root field. Otherwise, files may be missing when applications are running.

Environment variable key

Description

POD_IP

The IP address of the pod.

EDAS_APP_ID

The ID of the application deployed in SAE.

EDAS_ECC_ID

The ID of the ECS instance that is imported to the cluster after it is deployed in the application.

EDAS_PROJECT_NAME

Similar to EDAS_ECC_ID, this parameter is used for trace parsing.

EDAS_JM_CONTAINER_ID

Similar to EDAS_ECC_ID, this parameter is used for trace parsing.

EDAS_CATALINA_OPTS

The CATALINA_OPTS parameter that is required during middleware runtime.

CATALINA_OPTS

Similar to EDAS_CATALINA_OPTS, this parameter is used as a default startup parameter of Tomcat.

CATALINA_HOME

The path where Tomcat is installed.

PANDORA_LOCATION

The Pandora path, which can be viewed in HSF applications.

Dockerfile example of Spring Cloud or Dubbo applications (based on a JAR package)

FROM centos:7
LABEL maintainer="SAE R&D team"

# Install the required software for compression. 
RUN yum -y install wget unzip telnet

# Prepare the JDK and Tomcat system variables. 
ENV JAVA_HOME /usr/java/latest
ENV PATH $PATH:$JAVA_HOME/bin
ENV ADMIN_HOME /home/admin

# Download and install OpenJDK. 
RUN yum -y install java-1.8.0-openjdk-devel

# Download the JAR package for SAE deployment. 
RUN mkdir -p /home/admin/app/ && \
         wget http://sae-demo-cn-shenzhen.oss-cn-shenzhen.aliyuncs.com/demo/1.0/hello-edas-0.0.1-SNAPSHOT.jar -O /home/admin/app/hello-edas-0.0.1-SNAPSHOT.jar

# Add support for the Chinese language in containers. 
ENV LANG="en_US.UTF-8"

# Enhance the web shell use experience. 
ENV TERM=xterm

# Include the startup command in the startup script start.sh. 
RUN mkdir -p /home/admin
RUN echo 'eval execjava -jar $CATALINA_OPTS /home/admin/app/hello-edas-0.0.1-SNAPSHOT.jar'> /home/admin/start.sh && chmod +x /home/admin/start.sh
WORKDIR $ADMIN_HOME
CMD ["/bin/bash", "/home/admin/start.sh"]            
Note
  • If you need to use microservice capabilities, you must add the $CATALINA_OPTS startup parameter when you configure the startup command. For more information, see Configure a startup command.

  • If you need to use a self-managed registry or an MSE registry, you must configure the startup parameters -Dnacos.use.endpoint.parsing.rule=false and -Dnacos.use.cloud.namespace.parsing=false. To use a non-SAE built-in registry, you must add the required startup parameters before -jar.For more information, see Use a self-manage Nacos service registry and Use an MSE Nacos registry.

Dockerfile example of Spring Cloud or Dubbo applications (based on a WAR package)

FROM centos:7
LABEL maintainer="SAE R&D team"

# Install the required software for compression. 
RUN yum -y install wget unzip telnet

# Prepare the JDK and Tomcat system variables. 
ENV JAVA_HOME /usr/java/latest
ENV CATALINA_HOME /home/admin/apache-tomcat-7.0.91
ENV ADMIN_HOME /home/admin
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
RUN mkdir -p /home/admin

# Download and install OpenJDK. 
RUN yum -y install java-1.8.0-openjdk-devel

# Download and install Tomcat. 
RUN wget http://edas-hz.oss-cn-hangzhou.aliyuncs.com/apache-tomcat-7.0.91.tar.gz -O /tmp/apache-tomcat-7.0.91.tar.gz && \
    tar -xvf /tmp/apache-tomcat-7.0.91.tar.gz -C /home/admin && \
    rm /tmp/apache-tomcat-7.0.91.tar.gz && \
    chmod +x ${CATALINA_HOME}/bin/*sh

RUN mkdir -p ${CATALINA_HOME}/deploy/

# Add support for the Chinese language in containers. 
ENV LANG="en_US.UTF-8"

# Enhance the web shell use experience. 
ENV TERM=xterm

# Download the JAR package for SAE deployment. 
RUN wget http://sae-demo-cn-shenzhen.oss-cn-shenzhen.aliyuncs.com/demo/1.0/hello-edas.war -O /tmp/ROOT.war && \
    rm -rf ${CATALINA_HOME}/webapps/ROOT &&\
    unzip /tmp/ROOT.war -d ${CATALINA_HOME}/deploy/ROOT/ && \
    rm -rf /tmp/ROOT.war

# Set the Tomcat installation directory as the container startup directory. Start Tomcat in the run mode, and the catalina log is displayed in the standard command line. 
WORKDIR $ADMIN_HOME
CMD ["catalina.sh", "run"]            

Dockerfile example of HSF applications (based on a JAR package)

FROM centos:7
LABEL maintainer="SAE R&D team"

# Install the required software for compression. 
RUN yum install -y wget unzip telnet lsof net-tools bind-utils

# Prepare the JDK, Tomcat system variables, and Tomcat path. 
ENV JAVA_HOME /usr/java/latest
ENV CATALINA_HOME /home/admin/taobao-tomcat
ENV PATH ${JAVA_HOME}/bin:${PATH}
ENV ADMIN_HOME /home/admin

# Set the versions of EDAS-Container and Pandora. 
ENV EDAS_CONTAINER_VERSION V3.5.4
LABEL pandora V3.5.4

# Download and install OpenJDK. 
RUN yum -y install java-1.8.0-openjdk-devel

# Create a JAVA_HOME symbolic link. 
RUN if [ ! -L "${JAVA_HOME}" ]; then mkdir -p `dirname ${JAVA_HOME}` && ln -s `readlink -f /usr/lib/jvm/java` ${JAVA_HOME}; fi

# Download and install EDAS-Container and Pandora based on environment variables. 
RUN mkdir -p ${CATALINA_HOME}/deploy
RUN wget http://edas-hz.oss-cn-hangzhou.aliyuncs.com/edas-plugins/edas.sar.${EDAS_CONTAINER_VERSION}/taobao-hsf.tgz -O /tmp/taobao-hsf.tgz && \
    tar -xvf /tmp/taobao-hsf.tgz -C ${CATALINA_HOME}/deploy/ && \
    rm -rf /tmp/taobao-hsf.tgz

# Download the JAR package for SAE deployment. 
RUN mkdir -p ${ADMIN_HOME}/app && wget http://edas.oss-cn-hangzhou.aliyuncs.com/demoapp/fatjar-test-case-provider-0.0.1-SNAPSHOT.jar -O ${ADMIN_HOME}/app/provider.jar

# Include the startup command in the startup script start.sh. 
RUN echo 'eval execjava -jar $CATALINA_OPTS /home/admin/app/hello-edas-0.0.1-SNAPSHOT.jar'> /home/admin/start.sh && chmod +x /home/admin/start.sh
WORKDIR $CATALINA_HOME
CMD ["/bin/bash", "/home/admin/start.sh"]            
Note
  • If you need to use microservice capabilities, you must add the $CATALINA_OPTS startup parameter when you configure the startup command. For more information, see Configure a startup command.

  • If you need to use a self-managed registry or an MSE registry, you must configure the startup parameters -Dnacos.use.endpoint.parsing.rule=false and -Dnacos.use.cloud.namespace.parsing=false. To use a non-SAE built-in registry, you must add the required startup parameters before -jar.For more information, see Use a self-manage Nacos service registry and Use an MSE Nacos registry.

Dockerfile example of HSF applications (based on a WAR package)

FROM centos:7
LABEL maintainer="SAE R&D team"

# Install the required software for compression. 
RUN yum install -y wget unzip telnet lsof net-tools bind-utils

# Prepare the JDK, Tomcat system variables, and Tomcat path. 
ENV JAVA_HOME /usr/java/latest
ENV CATALINA_HOME /home/admin/taobao-tomcat
ENV PATH ${JAVA_HOME}/bin:${CATALINA_HOME}/bin:${PATH}

# Set the versions of EDAS-Container and Pandora. 
ENV EDAS_CONTAINER_VERSION V3.5.4
LABEL pandora V3.5.4

# Download and install OpenJDK. 
RUN yum -y install java-1.8.0-openjdk-devel

# Create a JAVA_HOME symbolic link. 
RUN if [ ! -L "${JAVA_HOME}" ]; then mkdir -p `dirname ${JAVA_HOME}` && ln -s `readlink -f /usr/lib/jvm/java` ${JAVA_HOME}; fi

# Download and install Ali-Tomcat 7.0.92 to the directory: /home/admin/taobao-tomcat. 
RUN wget http://edas-hz.oss-cn-hangzhou.aliyuncs.com/edas-container/7.0.92/taobao-tomcat-production-7.0.92.tar.gz -O /tmp/taobao-tomcat.tar.gz && \
    mkdir -p ${CATALINA_HOME} && \
    tar -xvf /tmp/taobao-tomcat.tar.gz -C ${CATALINA_HOME} && \
    mv ${CATALINA_HOME}/taobao-tomcat-production-7.0.59.3/* ${CATALINA_HOME}/ && \
    rm -rf /tmp/taobao-tomcat.tar.gz ${CATALINA_HOME}/taobao-tomcat-production-7.0.59.3 && \
    chmod +x ${CATALINA_HOME}/bin/*sh

# Download and install EDAS-Container and Pandora based on environment variables. 
RUN wget http://edas-hz.oss-cn-hangzhou.aliyuncs.com/edas-plugins/edas.sar.${EDAS_CONTAINER_VERSION}/taobao-hsf.tgz -O /tmp/taobao-hsf.tgz && \
    tar -xvf /tmp/taobao-hsf.tgz -C ${CATALINA_HOME}/deploy && \
    rm -rf /tmp/taobao-hsf.tgz

# Download the WAR package for SAE deployment. 
RUN wget http://edas.oss-cn-hangzhou.aliyuncs.com/demo/hello-edas.war -O /tmp/ROOT.war && \
    unzip /tmp/ROOT.war -d ${CATALINA_HOME}/deploy/ROOT && \
    rm -rf /tmp/ROOT.war

# Set the Tomcat installation directory as the container startup directory. Start Tomcat in the run mode, and the catalina log is displayed in the standard command line. 
WORKDIR ${CATALINA_HOME}
CMD ["catalina.sh", "run"]           

Customize a Dockerfile

Modify the runtime environment configurations in a Dockerfile, such as the JDK version, Tomcat configurations, and runtime environment.

  • Change the JDK version.

    The following example describes how to change the JDK version in a standard Dockerfile:

    # Download and install JDK 8. 
    RUN wget http://edas-hz.oss-cn-hangzhou.aliyuncs.com/agent/prod/files/jdk-8u65-linux-x64.rpm -O /tmp/jdk-8u65-linux-x64.rpm && \
        yum -y install /tmp/jdk-8u65-linux-x64.rpm && \
        rm -rf /tmp/jdk-8u65-linux-x64.rpm                            
  • Add the SAE runtime environment to the Tomcat startup parameters.

    SAE provides the JVM environment variable EDAS_CATALINA_OPTS that contains the minimum parameters required during runtime. Ali-Tomcat provides the customized JVM configuration option JAVA_OPTS for setting parameters such as Xmx and Xms.

    # Configure JVM parameters. JVM parameters for the SAE application. 
    ENV CATALINA_OPTS ${EDAS_CATALINA_OPTS}
    # Configure JVM parameters. 
    ENV JAVA_OPTS="\
         -Xmx3550m \
         -Xms3550m \
         -Xmn2g"                            
  • Use a self-managed registry.

    If you want to use a self-managed registry in a microservice, append the following parameters to the Java command:

    • -Dnacos.use.endpoint.parsing.rule=false
    • -Dnacos.use.cloud.namespace.parsing=false

    Sample code:

    RUN echo 'eval exec java -Dnacos.use.endpoint.parsing.rule=false -Dnacos.use.cloud.namespace.parsing=false -jar $CATALINA_OPTS /home/admin/app/hello-edas-0.0.1-SNAPSHOT.jar'> /home/admin/start.sh && chmod +x /home/admin/start.sh

Build an image

Go to the directory where the Dockerfile is located by using a local command line tool. Run the following docker build command to build an image:

docker build -t <Tag name (application name recommended)>:<Version> .
or
docker build -t <Tag name (application name recommended>:<Version> -f /path/to/custom_dockerfile_name.# Run the command if the created Dockerfile resides in another directory or is not named Dockerfile. 

Example:

docker build -t hsf-provider:1.0.0 .

Run the docker images | grep <Image tag name> command to view the local compiled image.

Upload the image to an image repository

You can upload the image that is locally created for your Java application to Container Registry provided by Alibaba Cloud. For more information, see Manage images.

To upload the local image to the image repository, run the commands that are used to push images to Container Registry. You can view these commands on the Details page of the specified image repository.

docker login --username=<Current username> registry.<regionId>.aliyuncs.com     # Enter the fixed or temporary password that is used as the access credential for the default instance in Container Registry rather than the password to your Alibaba Cloud account. 
docker tag <Local image ID> registry.<regionId>.aliyuncs.com/<Namespace name>/<Image repositry name>:<Image version>
docker push registry.<regionId>.aliyuncs.com/<Namespace name>/<Image repositry name>:<Image version>            

Example:

docker login --username=****@188077086902**** registry.cn-hangzhou.aliyuncs.com
docker tag <ImageId> registry.cn-hangzhou.aliyuncs.com/webapps/hsf-provider:1.0.0
docker push registry.cn-hangzhou.aliyuncs.com/webapps/hsf-provider:1.0.0            

Alibaba Cloud Container Registry V2

References

After you create the image, you can deploy the image in SAE. For more information, see the following topics: