Container technology, Docker, virtualization, these terms have been around for a long time, but they are still hot in the field of cloud native development. Even ABAP Netweaver, which SAP is famous for, has now embarked on the road of containerized cloud exploration, such as the following blog from the SAP community:
One of the common requirements for customers in the SAP ecosystem is the ability to migrate any existing business application that uses one or more supporting services (database/messaging, etc.) to the cloud. One way to do this is to run it on a virtual machine or create a container image and deploy it in a cloud environment where containers can run.
On the other hand, when the customer's priority is to maximize the number of applications running on a minimum number of servers, and their applications don't actually need to access OS-level resources or functionality, container technology is a better choice. The SAP Cloud Platform Cloud Foundry environment with Diego (Cloud Foundry's container management system) enabled by default provides the possibility to deploy Docker images as Cloud Foundry applications. If there is a Java application that can run normally, can it be deployed and run on the SAP cloud platform in the form of a Docker container? Of course you can, and it only takes a few simple command lines.
The next steps in this article are for those of you who have heard of Docker container technology, but haven't tried it yet. By reading this article, you can understand the general steps to containerize and deploy Java applications to the SAP cloud platform. You can also try it out according to the steps in this article. You only need to set up a simple Java development environment locally, install the Docker client and apply for a trial account of the SAP cloud platform.
(1) First, there must be a SpringBoot application that can run normally locally. If not, Baidu will then make one yourself.
If you really don't want to do it yourself, just clone one from the author's github repository .
After configuring maven and JDK locally, start it directly from the command line:
mvn spring-boot:run
You will see a prompt of Tomcat started on port 8000
, which means that the application has been successfully started and is listening on the local port 8000.
Enter localhost:8000/product in the browser, and see Hello World. At this point, we have a local SpringBoot application running normally, the next step is to package it into a Docker image.
(2) Create a new file named Dockerfile in the SpringBoot project and enter the following:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
VOLUME /log
EXPOSE 8080
ADD target/prolikeService.jar app.jar
ENV JAVA_OPTS="-Dserver.port=8080"
ENTRYPOINT exec java $JAVA_OPTS -jar /app.jar
This is actually an instruction file, and the Docker client will create a Docker image based on the content inside.
Briefly describe the meaning of each line.
Line 1: Specifies that the image to be created is based on an image named openjdk:8-jdk-alpine. More information about this image can be found on the Docker Hub website:
Lines 2 and 3: Define two persistent stores named tmp and log. The data generated when the container is running is destroyed with the destruction of the container, but sometimes we want the data to be persisted. For example, we need to analyze the log files generated when a container is running. In this case, we can use the VOLUME keyword in the Dockerfile to provide It creates a so-called "volume", which maps the directory where the container application runs to write data to a directory on the host.
If a container is still running, we can use the command line to enter the container and view the running log files in the log folder
docker exec -it 8302db78f838 /bin/sh
If the container has been destroyed, we can go to the host's /var/lib/docker/volumes directory to view the persistent log files:
Line 5: Copy the jar package packaged by maven of the current project into the container and rename it to app.jar
Lines 6 to 7: Set the JVM startup parameters to expose port 8080 to the outside world.
After writing the Dockerfile, use the command line to create the image:
docker build -t i042416/springbootexample:v1 .
v1 represents the label of the image, and the period at the end of the command line represents the current directory.
After the image is created, use the command line to push the created image to the Docker Hub website (a bit like submitting code to Github using the git client locally):
docker push i042416/springbootexample:v1
After success, you can see the pushed image on Docker Hub :
In this way, the SAP cloud platform can pull the image from Docker Hub later.
(3) Log in to the SAP cloud platform CloudFoundry environment, use the command line to deploy, use the parameter --docker-image to specify the image name we just uploaded to Docker Hub, and the application generated by deployment is called jerryjavadocker.
cf push jerryjavadocker --docker-image i042416/springbootexample:v6
Because my container image has been modified several times, the tags have been raised from v1 to v6.
Successful deployment, the status of the application shows running:
This successfully deployed application can also be seen on the SAP cloud platform, and it is running:
Running this successfully deployed Docker application has the same effect as our local mvn spring-boot:run. So far, the containerization of the SpringBoot application and the deployment of the SAP cloud platform have been successful.
Summarize
This article introduces in detail the steps based on a developed SpringBoot application, how to make it into a Docker image and deploy it to run on the SAP cloud platform.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。