In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Spring Boot simplifies the development process of Spring applications and provides all kinds of out-of-the-box (out-of-the-box) framework configuration in accordance with the principle of agreement priority configuration. On the other hand, Spring Boot also has the ability to build code directly into an executable jar package, which is a deployment unit that can run independently. Based on the above features, it is now widely believed that Spring Boot provides the ability to quickly construct micro services (Micro-Service).
Docker and Spring Boot
Docker is an implementation of Linux container. Linux container is a lightweight resource isolation technology based on processes. Each container corresponds to a process in the operating system, but it has its own cyberspace, file system, PID and so on. In addition to implementing the Linux container, Docker also makes the container "social". Users can publish container images on Docker Hub to share and collaborate with other developers. For tutorials on installing Docker, please refer to the official documentation
Spring Boot applications are usually built as a separate executable jar package, via Java-jar. Run, but the framework itself does not provide a way to run in the background as a service, usually with the help of process management tools such as Systemd, Supervisord and so on. On the other hand, although the application running environment is very simple, it is very advantageous for automated deployment and operation and maintenance to use them as Docker container images and run them.
This article will take the simplest Web application developed by Spring Boot as an example to explain how to run it as a container. This article focuses on building a Docker image and running a Docker container
Build and Spring Boot applications
After the application code has been written, you can package it and run it directly:
Maven: mvn package & & java-jar target/spring-boot-docker-0.1.0.jarGradle: gradle build & & java-jar build/libs/gs-spring-boot-docker-0.1.0.jar
Enter [http://localhost:8080/](http://localhost:8080/)] in the browser address bar to access the application.
Dockerfile builds an image
Docker provides files in Dockerfile format to build an application image. Now start to create a Dockerfile for Spring Boot application:
FROM java:8VOLUME / tmpADD spring-boot-docker-0.1.0.jar app.jarRUN bash-c 'touch / app.jar'ENTRYPOINT ["java", "- Djava.security.egd=file:/dev/./urandom", "- jar", "/ app.jar"]
Java:8 refers to the official java image provided on Docker Hub. The version number is 8, that is, jdk1.8. With this basic image, Dockerfile can obtain its status directly through the FROM instruction-that is, java is already installed in the container, and then run the Spring Boot application with a custom command:
VOLUME / tmp creates the / tmp directory and persists it to the Docker data folder because the embedded Tomcat container used by Spring Boot uses / tmp as the working directory ADD spring-boot-docker-0.1.0.jar app.jar to copy the application jar package to / app.jarENTRYPOINT represents the command executed by default after the container is run
Dockerfile is very simple. Run the docker build image command after editing:
Docker build-t tmy/spring-boot-app.
Then run the Docker container:
Docker run-d-p 8080 8080-- name sample-app tmy/spring-boot-app
Where-d means to run the container in the background, which naturally solves the problem that Spring Boot does not support running applications in the background. -p 8080 means that port 8080 inside the container is mapped to port 8080 of the host machine, so that the application can be accessed directly through the host machine. -- name sample-app gives the container an easy-to-remember name for future management.
Create a Docker image using Maven/Gradle
In order to make it easier for Java developers to create Docker images, Maven/Gradle provides response plug-ins.
Maven
Springio com.spotify docker-maven-plugin 0.2.3 ${docker.image.prefix} / ${project.artifactId} src/main/docker / ${project.build.directory} ${project.build.finalName} .jar
The above pom.xml contains the configuration of docker-maven-plugin:
ImageName specifies the name of the image dockerDirectory specifies the location of the Dockerfile resources refers to those files that need to be put together with Dockerfile and used when building the image, and the general application jar package needs to be included
After the above configuration, run the following command to create an image in the local Docker:
$mvn package docker:build
Gradle
Gradle also has plug-ins that support Docker:
Buildscript {... Dependencies {... Classpath ('se.transmode.gradle:gradle-docker:1.2')} group =' springio'...apply plugin: 'docker'task buildDocker (type: Docker, dependsOn: build) {push = true applicationName = jar.baseName dockerfile = file (' src/main/docker/Dockerfile') doFirst {copy {from jar into stageDir}
The above configuration is basically similar to Maven. Execute the following command to build an image:
$gradle build buildDocker
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.