Now most projects have begun to be deployed on Docker, but the deployment process is still a bit troublesome, so this article wants to explain how to use IDEA for one-click deployment. Docker configuration Modify the configuration file Open the Docker configuration file: vim /usr/lib/systemd/system/docker.service Comment out the following line: # ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock Write a new line: ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock Reload the configuration file and start: systemctl daemon-reload systemctl start docker As shown in the following figure: Configuring Docker with IDEA Install Docker plugin Configure Docker information To configure Docker in the settings, you need to configure the API URL. When Connection successful appears below: Project Construction Create a new springboot project To build a project through IDEA, you don’t need to choose anything, just keep clicking: Modify the pom file The most important two points are: 1. Add in the properties tag <docker.image.prefix>demo</docker.image.prefix> 2. Add a new plugin tag <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <dockerDirectory></dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> Here is the complete pom file: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <docker.image.prefix>demo</docker.image.prefix> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.2.1</version> <configuration> <imageName>${docker.image.prefix}/${project.artifactId}</imageName> <dockerDirectory></dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> </plugins> </build> </project> Create a new Dockerfile You need to create a new Dockerfile file in the root directory #Specify the base image and customize it FROM java:8 #Maintainer information MAINTAINER zhouzhaodong <[email protected]> #The /tmp directory here will be automatically mounted as an anonymous volume at runtime, and any information written to /tmp will not be recorded in the container storage layer VOLUME /tmp #Copy target/demo-1.0.0.jar in the context directory to the container COPY target/demo-0.0.1.jar demo-1.0.0.jar #Execute in bash mode to make demo-1.0.0.jar accessible. #RUNCreate a new layer and execute these commands on it. After the execution is completed, commit the changes of this layer to form a new image. RUN bash -c "touch /demo-1.0.0.jar" #Declare the service port provided by the runtime container. This is just a declaration. The application will not open the service of this port at runtime because of this declaration. EXPOSE 8080 #Specify the container startup program and parameters <ENTRYPOINT> "<CMD>" ENTRYPOINT ["java","-jar","demo-1.0.0.jar"] Create a new controller file Don't forget to add web dependency in pom file. @RestController public class testController { @RequestMapping("/") public String test(){ return "test Docker"; } } Maven packaging Install packaging: Generate jar package: New Configuration Create a new Dockerfile run configuration: Add the following information, find the Dockerfile file you wrote, and configure the port mapping: Generate a Docker image and run it Simply run the newly created Dockerfile run configuration: After the operation is successful, the log window will show the project operation information: Accessing the corresponding address will display the information we entered: The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Two ways to clear table data in MySQL and their differences
>>: Implementation of VUE infinite level tree data structure display
Table of contents Dockerfile pom.xml Jenkins Conf...
1. Performance schema: Introduction In MySQL 5.7,...
Must read before operation: Note: If you want to ...
1: Differences in speed and loading methods The di...
This article example shares the specific code of ...
Table of contents Preface difficulty Cross-domain...
There are significant differences between centos7...
ins and del were introduced in HTML 4.0 to help au...
Load one or more features <template> <di...
The first step is to create a mysql container doc...
When using justify-content:space-between layout, ...
Of course, there are many people who hold the oppo...
Table of contents topic analyze Basic solution Ba...
After three days of encountering various difficul...
This article shares the specific code of JavaScri...