在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):spring-guides/gs-spring-boot-docker开源软件地址(OpenSource Url):https://github.com/spring-guides/gs-spring-boot-docker开源编程语言(OpenSource Language):Java 74.9%开源软件介绍(OpenSource Introduction):Table of Contents
This guide walks you through the process of building a Docker image for running a Spring Boot application. We start with a basic
What You Will BuildDocker is a Linux container management toolkit with a “social” aspect, letting users publish container images and consume those published by others. A Docker image is a recipe for running a containerized process. In this guide, we build one for a simple Spring boot application. What You Will NeedIf you are NOT using a Linux machine, you need a virtualized server. If you install VirtualBox, other tools like the Mac’s You also need Docker, which only runs on 64-bit machines. See https://docs.docker.com/installation/#installation for details on setting Docker up for your machine. Before proceeding further, verify you can run Starting with Spring InitializrYou can use this pre-initialized project and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial. To manually initialize the project:
Set up a Spring Boot ApplicationNow you can create a simple application:
link:complete/src/main/java/hello/Application.java[] The class is flagged as a Now we can run the application without the Docker container (that is, in the host OS): If you use Gradle, run the following command: ./gradlew build && java -jar build/libs/gs-spring-boot-docker-0.1.0.jar If you use Maven, run the following command: ./mvnw package && java -jar target/gs-spring-boot-docker-0.1.0.jar Then go to localhost:8080 to see your “Hello Docker World” message. Containerize ItDocker has a simple "Dockerfile" file format that it uses to specify the “layers” of an image. Create the following Dockerfile in your Spring Boot project: Example 1. Dockerfile
If you use Gradle, you can run it with the following command: docker build --build-arg JAR_FILE=build/libs/\*.jar -t springio/gs-spring-boot-docker . If you use Maven, you can run it with the following command: docker build -t springio/gs-spring-boot-docker . This command builds an image and tags it as This Dockerfile is very simple, but it is all you need to run a Spring Boot app with no frills: just Java and a JAR file. The build creates a spring user and a spring group to run the application. It is then copied (by the
Running applications with user privileges helps to mitigate some risks (see, for example, a thread on StackExchange).
So, an important improvement to the Example 2. Dockerfile
You can see the username in the application startup logs when you build and run the application: docker build -t springio/gs-spring-boot-docker .
docker run -p 8080:8080 springio/gs-spring-boot-docker Note the :: Spring Boot :: (v2.2.1.RELEASE)
2020-04-23 07:29:41.729 INFO 1 --- [ main] hello.Application : Starting Application on b94c86e91cf9 with PID 1 (/app started by spring in /)
... Also, there is a clean separation between dependencies and application resources in a Spring Boot fat JAR file, and we can use that fact to improve performance. The key is to create layers in the container filesystem. The layers are cached both at build time and at runtime (in most runtimes), so we want the most frequently changing resources (usually the class and static resources in the application itself) to be layered after the more slowly changing resources. Thus, we use a slightly different implementation of the Dockerfile: Example 3. Dockerfile
This Dockerfile has a mkdir -p build/dependency && (cd build/dependency; jar -xf ../libs/*.jar) To use the mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar) If we get that right, it already contains a
From a Gradle build, you need to add the explicit build arguments in the Docker command line: docker build --build-arg DEPENDENCY=build/dependency -t springio/gs-spring-boot-docker . To build the image in Maven, you can use a simpler Docker command line: docker build -t springio/gs-spring-boot-docker .
Instead of building with the Docker command line, you might want to use a build plugin. Spring Boot supports building a container from Maven or Gradle by using its own build plugin. Google also has an open source tool called Jib that has Maven and Gradle plugins. Probably the most interesting thing about this approach is that you do not need a
Build a Docker Image with GradleYou can build a tagged docker image with Gradle in one command: ./gradlew bootBuildImage --imageName=springio/gs-spring-boot-docker Build a Docker Image with MavenTo get started quickly, you can run the Spring Boot image generator without even changing your ./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=springio/gs-spring-boot-docker To push to a Docker registry, you need to have permission to push, which you do not have by default. Change the image prefix to your own Dockerhub ID and After the PushA You do NOT have to register with docker or publish anything to run a docker image that was built locally. If you built with Docker (from the command line or from Spring Boot), you still have a locally tagged image, and you can run it like this: $ docker run -p 8080:8080 -t springio/gs-spring-boot-docker
Container memory limit unset. Configuring JVM for 1G container.
Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -XX:MaxMetaspaceSize=86381K -XX:ReservedCodeCacheSize=240M -Xss1M -Xmx450194K (Head Room: 0%, Loaded Class Count: 12837, Thread Count: 250, Total Memory: 1073741824)
....
2015-03-31 13:25:48.035 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2015-03-31 13:25:48.037 INFO 1 --- [ main] hello.Application : Started Application in 5.613 seconds (JVM running for 7.293)
The application is then available on http://localhost:8080 (visit that and it says, “Hello Docker World”).
When it is running, you can see in the list of containers, similar to the following example: $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
81c723d22865 springio/gs-spring-boot-docker:latest "java -Djava.secur..." 34 seconds ago Up 33 seconds 0.0.0.0:8080->8080/tcp goofy_brown To shut it down again, you can run docker stop goofy_brown
81c723d22865 If you like, you can also delete the container (it is persisted in your filesystem somewhere under docker rm goofy_brown Using Spring ProfilesRunning your freshly minted Docker image with Spring profiles is as easy as passing an environment variable to the Docker run command (for the docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 8080:8080 -t springio/gs-spring-boot-docker You can do the same for the docker run -e "SPRING_PROFILES_ACTIVE=dev" -p 8080:8080 -t springio/gs-spring-boot-docker Debugging the Application in a Docker ContainerTo debug the application, you can use JPDA Transport. We treat the container like a remote server.
To enable this feature, pass Java agent settings in the docker run -e "JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n" -p 8080:8080 -p 5005:5005 -t springio/gs-spring-boot-docker SummaryCongratulations! You have created a Docker container for a Spring Boot application! By default, Spring Boot applications run on port 8080 inside the container, and we mapped that to the same port on the host by using See AlsoThe following guides may also be helpful:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论