The first six articles of this tutorial:
- Docker practical tutorial from entry to improvement (1)
- Docker practical tutorial from entry to improvement (2)
- Docker practical tutorial from entry to improvement (3)
- Docker practical tutorial from entry to improvement (4)
- Docker practical tutorial from entry to improvement (5)
- Docker practical tutorial from entry to improvement (6)
And through the introduction of this article, we deeply learned the three main points of Docker technology.
Starting with this article, we will learn how to deploy and run a Docker image on a cloud platform.
Exercise 1: Dockerizing the Spring Boot application
First, you need to package the SpringBoot application into docker. The content of the dockerfile I use is:
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
Use the following command line package:
docker build -t i042416/springbootexample:v4 .
docker login Log in to docker hub and upload the image:
docker push i042416/springbootexample:v4
Finally, use the command to deploy to Cloud Platform:cf push jerryjavadocker --docker-image i042416/springbootexample:v4
After the command is executed, you can see the successfully deployed Docker application in the console of the SAP cloud platform:
Get the application url from the console:
It can be successfully accessed in the browser:
Exercise 2: Possible error messages and solutions for deploying Docker images to Cloud Platform
The first time I tried it, I encountered the error message pictured below:
You have exceeded your organization's memory limit: app requested more memory than available
FAILED
To solve this error, increase the memory quota of the Subaccount:
Error message encountered on second attempt:
Failed getting docker image manifest by tag: errors:
denied: requested access to the resource is denied
unauthorized: authentication required
Log in to docker hub and confirm that the docker image and tag used are correct:
https://hub.docker.com/repository/docker/i042416/ui5-nginx
Finally successfully deployed:
See the successfully deployed Docker application on the SAP Cloud Platform:
Get the url of the application:
Can successfully access:
Exercise 3: Deploy the Docker image containing the SpringBoot application to the CloudFoundry environment and run it successfully
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:
Proof of Concept: Deploying ABAP in Kubernetes
If I have a Java application that is already working properly, can I deploy it as a Docker container and run it on the CloudFoundry environment of the SAP cloud platform? Of course you can, and it only takes a few simple command lines.
Through the practical exercise of this step, you can understand the general steps of containerizing and deploying Java applications to the SAP cloud platform CloudFoundry environment to run. 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.
After configuring maven and JDK locally, start it directly from the command line:
mvn spring-boot:run
You will see the promptTomcat 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 locally running SpringBoot application, and 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 built is based on an image named openjdk:8-jdk-alpine
, which can be found on the Docker Hub website for more information.
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, if we need to analyze the log files generated when a container is running, we can use the Dockerfile VOLUME
The container persistence technology provided by the keyword creates a so-called "volume", and maps the directory where the container application runs 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 file 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 file:
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:https://hub.docker.com/repository/docker/i042416/springbootexample
In this way, SAP Cloud Platform can pull this image from Docker Hub later.
(3) Log in to the SAP cloud platform CloudFoundry environment, deploy using the command line, specify the name of the image we just uploaded to Docker Hub with the parameter --docker-image
, and the generated application 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 in the SAP cloud platform CloudFoundry environment and is running:
Running this successfully deployed Docker application has the same effect as we did locally mvn spring-boot:run
. So far, the containerization of the SpringBoot application and the deployment of the SAP cloud platform CloudFoundry environment have been successful.
Summarize
This article reviews the process of how to make the SpringBoot application into a Docker image in the previous step of this tutorial, and then introduces the detailed steps of how to deploy the Docker image to the cloud platform CloudFoundry environment and run it, and gives common error messages during the deployment process. Corresponding error analysis and solutions are provided.
The first six articles in this series:
- Docker practical tutorial from entry to improvement (1)
- Docker practical tutorial from entry to improvement (2)
- Docker practical tutorial from entry to improvement (3)
- Docker practical tutorial from entry to improvement (4)
- Docker practical tutorial from entry to improvement (5)
- Docker practical tutorial from entry to improvement (6)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。